keymasterviriya1150

Untitled

Mar 29th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<queue>
  5. #include<utility>
  6. #define infinity 1000
  7. using namespace std;
  8. typedef pair<int,int > ii;
  9. int main ()
  10. {
  11.     int cases;
  12.    
  13.     cin>>cases;
  14.  
  15.     for(int thiscase=1;thiscase<=cases;++thiscase)
  16.     {      
  17.         int N,M,S,T;
  18.         cin >> N >> M >> S >> T;
  19.         // node,connect,sever s, sever T
  20.         vector<ii> edge[N];
  21.        
  22.         for(int m=0;m<M;m++)
  23.         {
  24.             int u,v,w;
  25.             // u=vertex u, v= vertex v
  26.             cin>> u >> v >> w;
  27.             edge[u].push_back(make_pair(v,w));
  28.             edge[v].push_back(make_pair(u,w));     
  29.         }
  30.         //Dijkstra’s Algorithm Code
  31.             priority_queue<ii , vector<ii>, greater<ii> > q;
  32.            
  33.             vector<int> dist;
  34.             dist.assign(N, INT_MAX);  // all vertex  weight  infinity
  35.    
  36.             dist[S] = 0; //start vertex      
  37.             q.push(make_pair(dist[S], S)); //weight frist node=0
  38.        
  39.             while (!q.empty())
  40.             {
  41.                 int current_vertex = q.top().second;
  42.    
  43.                 q.pop();
  44.  
  45.                 for (size_t i = 0; i < edge[current_vertex].size(); i++)
  46.                 {
  47.                      int next_vertex = edge[current_vertex][i].first;
  48.                      int w = edge[current_vertex][i].second;
  49.      
  50.                     if (dist[next_vertex] > dist[current_vertex] + w)
  51.                      {
  52.                         dist[next_vertex] = dist[current_vertex] + w;
  53.                         q.push(make_pair(dist[next_vertex], next_vertex));
  54.                      } 
  55.                  }
  56.                  if()
  57.                     cout<<"Case #"<< ;
  58.                 else
  59.                     cout<<"Case #"<<"unreachable";
  60.                  
  61.             }
  62.     }
  63.        
  64.        
  65.    
  66.    
  67.  
  68.  
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment