Advertisement
Rana_093

Uva-10986(Dijkstra)

Mar 26th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef pair<int,int> pii;
  6.  
  7. int arrDis[20010];
  8. vector<pii> graph[20010];
  9.  
  10.  
  11. void ans(int st)
  12. {
  13.     arrDis[st] = 0;
  14.     priority_queue< pii,vector<pii>,greater<pii> > pQ;
  15.     pQ.push(make_pair(0,st));
  16.     while(!pQ.empty())
  17.     {
  18.         pii temp = pQ.top();
  19.         int val = temp.first,curPos = temp.second;
  20.         pQ.pop();
  21.         if(arrDis[curPos]<val) continue;
  22.         int sz = graph[curPos].size();
  23.         for(int i = 0; i<sz; i++)
  24.         {
  25.             int nxPos = graph[curPos][i].first;
  26.             int nxVal = graph[curPos][i].second;
  27.             if(arrDis[nxPos]>(nxVal+val))
  28.             {
  29.                 arrDis[nxPos] = nxVal+val;
  30.                 pQ.push(make_pair(arrDis[nxPos],nxPos));
  31.             }
  32.         }
  33.     }
  34. }
  35.  
  36. void clr()
  37. {
  38.     for(int i = 0; i<20010; i++)
  39.     {
  40.         arrDis[i] = INT_MAX;
  41.     }
  42.     for(int i = 0; i<20010; i++)
  43.     {
  44.         graph[i].clear();
  45.     }
  46. }
  47. int main()
  48. {
  49.     int test;
  50.     scanf("%d",&test);
  51.     for(int i = 1; i<=test; i++)
  52.     {
  53.         clr();
  54.         int node,edge,from,to;
  55.         scanf("%d%d%d%d",&node,&edge,&from,&to);
  56.         for(int j = 1; j<=edge; j++)
  57.         {
  58.             int a,b,c;
  59.             scanf("%d%d%d",&a,&b,&c);
  60.             graph[a].push_back(make_pair(b,c));
  61.             graph[b].push_back(make_pair(a,c));
  62.         }
  63.  
  64.         ans(from);
  65.         if(arrDis[to] != INT_MAX)printf("Case #%d: %d\n",i,arrDis[to]);
  66.         else printf("Case #%d: unreachable\n",i);
  67.     }
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement