iamyeasin

Dijkstra

Apr 3rd, 2018
56
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. #define     sf      scanf
  3. #define     pf      printf
  4. #define     inf     INT_MAX
  5. #define     dbg     cout << "Debug" << endl;
  6.  
  7. using namespace std;
  8.  
  9. void clr();
  10. void printGrid();
  11.  
  12. vector < int > adjList[1024];
  13. vector < int > ans;
  14. priority_queue  < int > q;
  15.  
  16. int nodes,start,edges,n,a,b,c ;
  17. int visited[1025] ,d[1024];
  18. int cost[1024][1024];
  19.  
  20. void BFS( int s ){ /// Implementation of Dijsktra Algorithm
  21.  
  22.     for( int i=0; i<=nodes; i++ ) d[i] = inf;
  23.  
  24.     q.push( s );
  25.     d[s] = 0;
  26.  
  27.     while ( !q.empty() ){
  28.         int u = q.top(); q.pop();
  29.         int ucost = d[u];
  30.         int sz = adjList[u].size();
  31.  
  32.         for( int i=0; i<sz; i++ ){
  33.             int v = adjList[u][i];
  34.             int vcost = ucost + cost[u][v];
  35.  
  36.             if( vcost < d[v] ){
  37.                 d[v] = vcost;
  38.                 q.push( v );
  39.             }
  40.         }
  41.  
  42.     }
  43.  
  44. }
  45.  
  46.  
  47. int main(){
  48.     #ifndef ONLINE_JUDGE
  49.         freopen("in.txt","rt",stdin);
  50.         freopen("out.txt","wt",stdout);
  51.     #endif
  52.     clr();
  53.     int kase = 0;
  54.  
  55.     while( sf ("%d %d",&nodes, &edges) ,nodes + edges ){
  56.         for( int i=1; i<=edges; i++ ){
  57.             sf("%d %d %d",&a, &b, &c);
  58.             adjList[a].push_back( b );
  59.             adjList[b].push_back( a );
  60.             cost[a][b] = cost[b][a] = c;
  61.         }
  62.  
  63.         BFS(1);
  64.         int last = nodes,counter = 0 ;
  65.         // for( int i=1; i<=nodes; i++ ) cout << d[i] << endl;
  66.  
  67.  
  68.         if( d[nodes-1] == d[nodes] ){
  69.             printf("System #%d\nThe last domino falls after %.1lf seconds, between key dominoes %d and %d.\n\n", ++kase, (( cost[nodes][nodes-1] * 1.00 ) / 2.00) + d[nodes] , nodes-1, nodes );
  70.         }
  71.         else if( d[nodes-1] != d[nodes] ){
  72.             printf("System #%d\nThe last domino falls after %.1lf seconds, at key domino %d.\n\n", ++kase, d[nodes]*1.00, nodes );
  73.         }
  74.  
  75.         clr();
  76.     }
  77.  
  78.  
  79.     return 0;
  80. }
  81.  
  82. void clr(){
  83.     for( int i=0; i<=nodes; i++ ){
  84.         adjList[i].clear();
  85.         visited[i] = 0;
  86.     }
  87.     memset( cost, 0, sizeof cost );
  88.     for( int i=0; i<=nodes; i++ )d[i] = inf;
  89.  
  90. }
  91.  
  92. void printGrid(){
  93.     for( int i=1; i<=nodes; i++ ){
  94.         for( int j=1; j<=nodes; j++ ){
  95.             printf("%d ", cost[i][j] );
  96.         }
  97.         puts("");
  98.     }
  99.  
  100. }
Add Comment
Please, Sign In to add comment