Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define D(x) cout<<#x<<" = "<<x<<endl
  4. #define inf 1e9
  5. #define mx 500
  6.  
  7. int n, m, root, t, cs;
  8.  
  9. vector<int> edge[mx+5];
  10. int par[mx+5], dis[mx+5], cost[mx+5][mx+5], vis[mx+5];
  11.  
  12.  
  13. struct node
  14. {
  15.     int x, d;
  16. }ND;
  17.  
  18. bool operator < (node A, node B)
  19. {
  20.     if(A.d < B.d) return false;
  21.     return true;
  22. }
  23.  
  24. int path(int x)
  25. {
  26.     if(x == root) return 0;
  27.     int p, mn = -1;
  28.     while(x != root)
  29.     {
  30.         p = par[x];
  31.         if(p == -1) return -1;
  32.         mn = max(mn, cost[p][x]);
  33.         x = p;
  34.     }
  35.     return mn;
  36. }
  37.  
  38. void print()
  39. {
  40.     for(int i= 0; i< n;i++)
  41.     {
  42.         int ck = path(i);
  43.         if(ck == -1) printf("Impossible\n");
  44.         else printf("%d\n",ck);
  45.     }
  46. }
  47.  
  48. void solve(int src)
  49. {
  50.     memset(vis, 0, sizeof vis);
  51.     memset(par, -1, sizeof par);
  52.     for(int i = 0;i< n;i++) dis[i] = inf;
  53.  
  54.     node u;
  55.     par[src] = src;
  56.     dis[src] = 0;
  57.  
  58.     u.x = src;
  59.     u.d = dis[src];
  60.  
  61.     priority_queue<node> q;
  62.     q.push(u);
  63.     while(!q.empty())
  64.     {
  65.         u = q.top();
  66.         vis[u.x] = 1;
  67.         q.pop();
  68.  
  69.         for(int i= 0;i<edge[u.x].size();i++)
  70.         {
  71.             node v;
  72.             v.x = edge[u.x][i];
  73.             v.d = cost[u.x][v.x];
  74.             if(!vis[v.x] && dis[v.x] > v.d)
  75.             {
  76.                 par[v.x] = u.x;
  77.                 dis[v.x] = v.d;
  78.                 q.push(v);
  79.             }
  80.         }
  81.     }
  82.     print();
  83.  
  84. }
  85.  
  86. void init(int x)
  87. {
  88.     for(int i = 0;i<=x;i++)
  89.     {
  90.         for(int j = 0;j<=x ;j++)
  91.             cost[i][j] = inf;
  92.         edge[i].clear();
  93.     }
  94. }
  95.  
  96.  
  97. int main()
  98. {
  99.     int u, v, w;
  100.     scanf("%d",&t);
  101.     while(t--)
  102.     {
  103.         scanf("%d%d",&n,&m);
  104.         init(n);
  105.         while(m--)
  106.         {
  107.             scanf("%d%d%d",&u,&v,&w);
  108.             edge[u].push_back(v);
  109.             edge[v].push_back(u);
  110.  
  111.             cost[u][v] = min(cost[u][v], w);
  112.             cost[v][u] = cost[u][v];
  113.         }
  114.         scanf("%d",&root);
  115.         printf("Case %d:\n",++cs);
  116.         solve(root);
  117.  
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement