Guest User

Untitled

a guest
Mar 31st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <map>
  7. #include <queue>
  8. #include <vector>
  9. #include <algorithm>
  10. #include <cmath>
  11. using namespace std;
  12. #define INF 1LL<<60
  13. int par[20010];
  14. long long int dist[20100];
  15. struct edge
  16. {
  17.     int u;
  18.     long long int cost;
  19.     edge(int a,int b)
  20.     {
  21.         u=a;
  22.         cost=b;
  23.     }
  24.     bool operator <(const edge &p)const
  25.     {
  26.         return cost>p.cost;
  27.     }
  28. };
  29. int n;
  30. int grid[2010][2010];
  31. long long int cost[2010][2010];
  32. void dijkstra(int start)
  33. {
  34.     memset(par,-1,sizeof(par));
  35.     for(int i=0; i<=n; i++)
  36.         dist[i]=INF;
  37.     priority_queue<edge>Q;
  38.     Q.push(edge (start,0));
  39.     dist[start]=0;
  40.     while(!Q.empty())
  41.     {
  42.         edge top=Q.top();
  43.         Q.pop();
  44.         int u=top.u;
  45.         for(int v=0; v<n; v++)
  46.         {
  47.             if(grid[u][v]==1)
  48.             {
  49.                 if(dist[u]+cost[u][v]<dist[v])
  50.                 {
  51.                     dist[v]=dist[u]+cost[u][v];
  52.                     par[v]=u;
  53.                     Q.push(edge(v,dist[v]));
  54.                 }
  55.             }
  56.         }
  57.     }
  58.     return;
  59. }
  60. int main()
  61. {
  62.     int t,m,st,end,u,v;
  63.     long long int w;
  64.     scanf("%d %d %d",&n,&m,&st);
  65.     for(int i=0; i<m; i++)
  66.     {
  67.         scanf("%d %d %lld",&u,&v,&w);
  68.         grid[u][v]=1;
  69.         cost[u][v]= w;
  70.     }
  71.     dijkstra(st);
  72.     for(int i=0;i<n;i++)
  73.     printf(" %lld\n", dist[i]);
  74.     return 0;
  75. }
  76. /*
  77. O bssed index
  78. 3 6 2
  79. 0 1 100
  80. 1 0 100
  81. 0 2 200
  82. 2 0 200
  83. 1 2 50
  84. 2 1 50
  85. */
Advertisement
Add Comment
Please, Sign In to add comment