MAGCARI

Travelling

Feb 4th, 2023
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. /*
  2.     Task    : _example
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 05 February 2023 [10:23]
  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. priority_queue<A > heap;
  19. int dis[20][33000],floydWarshall[210][210],item[210],newItem[20];
  20. int convertNum[210];
  21. vector<B > g[20];
  22. int main(){
  23.     cin.tie(0)->sync_with_stdio(0);
  24.     cin.exceptions(cin.failbit);
  25.     int n,m,k,num;
  26.     cin >> n >> m >> k;
  27.     memset(item,-1,sizeof item);
  28.     for(int i=0;i<k;i++){
  29.         cin >> num;
  30.         item[num] = i;
  31.     }
  32.    
  33.     // Floyd Warshall (minimize graph)
  34.     for(int i=1;i<=n;i++)
  35.         for(int j=1;j<=n;j++)
  36.             floydWarshall[i][j] = 1e9;
  37.     while(m--){
  38.         int u,v,w;
  39.         cin >> u >> v >> w;
  40.         floydWarshall[u][v] = floydWarshall[v][u] = min(floydWarshall[u][v],w);
  41.     }
  42.     for(int k=1;k<=n;k++)
  43.         for(int i=1;i<=n;i++)
  44.             for(int j=1;j<=n;j++)
  45.                 floydWarshall[i][j] = min(floydWarshall[i][j],floydWarshall[i][k]+floydWarshall[k][j]);
  46.    
  47.     // create new graph
  48.     vector<int > nodeList;
  49.     int cnt = 0;
  50.     memset(newItem,-1,sizeof newItem);
  51.     for(int i=1;i<=n;i++){
  52.         if(i == 1 || i == n || item[i] != -1){
  53.             nodeList.push_back(i);
  54.             convertNum[i] = ++cnt;
  55.             if(item[i] != -1)   newItem[cnt] = item[i];
  56.         }
  57.     }
  58.     for(auto x:nodeList){
  59.         for(auto y:nodeList){
  60.             if(x == y)  continue;
  61.             g[convertNum[x]].push_back({convertNum[y],floydWarshall[x][y]});
  62.             g[convertNum[y]].push_back({convertNum[x],floydWarshall[x][y]});
  63.         }
  64.     }
  65.  
  66.     // Dijkstra
  67.     for(int i=1;i<=cnt;i++)
  68.         for(int j=0;j<(1<<k);j++)
  69.             dis[i][j] = 1e9;
  70.     if(newItem[1] == -1)    dis[1][0] = 0,heap.push({1,0,0});
  71.     else                    dis[1][1<<item[1]] = 0,heap.push({1,0,1<<newItem[1]});
  72.     while(!heap.empty()){
  73.         A now = heap.top();
  74.         heap.pop();
  75.         if(now.currentNode == cnt && now.mask == (1<<k)-1){
  76.             cout << now.weight << '\n';
  77.             return 0;
  78.         }
  79.         for(auto x:g[now.currentNode]){
  80.             int nextMask = now.mask;
  81.             if(newItem[x.nextNode]  != -1)  nextMask |= (1<<newItem[x.nextNode]);
  82.             if(dis[x.nextNode][nextMask] <= now.weight+x.edgeWeight)    continue;
  83.             dis[x.nextNode][nextMask] = now.weight+x.edgeWeight;
  84.             heap.push({x.nextNode,now.weight+x.edgeWeight,nextMask});
  85.         }
  86.     }
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment