MAGCARI

Travelling

Feb 15th, 2023
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. /*
  2.     Task    : _example
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 15 February 2023 [21:10]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. struct A{
  10.     int currentNode,weight,mask;
  11.     bool operator < (const A&o) const{
  12.         return weight>o.weight;
  13.     }
  14. };
  15. struct B{
  16.     int nextNode,edgeWeight;
  17. };
  18. vector<B > g[20];
  19. int dis[20][33000];
  20. priority_queue<A > heap;
  21.  
  22. int floydWarshall[210][210];
  23. int item[210],newItem[20];
  24. int convertNum[210];
  25. int main(){
  26.     int n,m,k;
  27.     scanf("%d %d %d",&n,&m,&k);
  28.     memset(item,-1,sizeof item);
  29.     for(int i=0;i<k;i++){
  30.         int num;
  31.         scanf("%d",&num);
  32.         item[num] = i;
  33.     }
  34.  
  35.     // Minimize graph
  36.     for(int i=1;i<=n;i++)
  37.         for(int j=1;j<=n;j++)
  38.             floydWarshall[i][j] = 1e9;
  39.     while(m--){
  40.         int u,v,w;
  41.         scanf("%d %d %d",&u,&v,&w);
  42.         floydWarshall[u][v] = floydWarshall[v][u] = min(floydWarshall[u][v],w);
  43.     }
  44.     for(int k=1;k<=n;k++)
  45.         for(int i=1;i<=n;i++)
  46.             for(int j=1;j<=n;j++)
  47.                 floydWarshall[i][j] = min(floydWarshall[i][j],floydWarshall[i][k]+floydWarshall[k][j]);
  48.  
  49.     // create new graph
  50.     vector<int > nodeList;
  51.     int cnt = 0;
  52.     memset(newItem,-1,sizeof newItem);
  53.     for(int i=1;i<=n;i++){
  54.         if(i == 1 || i == n || item[i] != -1){
  55.             nodeList.push_back(i);
  56.             convertNum[i] = ++cnt;
  57.             if(item[i] != -1)   newItem[cnt] = item[i];
  58.         }
  59.     }
  60.     for(auto x:nodeList){
  61.         for(auto y:nodeList){
  62.             if(x == y)  continue;
  63.             g[convertNum[x]].push_back({convertNum[y],floydWarshall[x][y]});
  64.             g[convertNum[y]].push_back({convertNum[x],floydWarshall[x][y]});
  65.         }
  66.     }
  67.  
  68.     // Dijkstra
  69.     for(int i=1;i<=cnt;i++)
  70.         for(int j=0;j<(1<<k);j++)
  71.             dis[i][j] = 1e9;
  72.     if(newItem[1] == -1)    dis[1][0] = 0,heap.push({1,0,0});
  73.     else                    dis[1][1<<newItem[1]] = 0,heap.push({1,0,1<<newItem[1]});
  74.     while(!heap.empty()){
  75.         A now = heap.top();
  76.         heap.pop();
  77.         if(now.currentNode == cnt && now.mask == (1<<k)-1){
  78.             printf("%d\n",now.weight);
  79.             return 0;
  80.         }
  81.         for(auto x:g[now.currentNode]){
  82.             int nmask = now.mask;
  83.             if(newItem[x.nextNode] != -1)   nmask |= (1<<newItem[x.nextNode]);
  84.             if(dis[x.nextNode][nmask] > now.weight + x.edgeWeight){
  85.                 dis[x.nextNode][nmask] = now.weight + x.edgeWeight;
  86.                 heap.push({x.nextNode,now.weight + x.edgeWeight,nmask});
  87.             }
  88.         }
  89.     }
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment