Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef pair<int,int> pii;
- int arrDis[20010];
- vector<pii> graph[20010];
- void ans(int st)
- {
- arrDis[st] = 0;
- priority_queue< pii,vector<pii>,greater<pii> > pQ;
- pQ.push(make_pair(0,st));
- while(!pQ.empty())
- {
- pii temp = pQ.top();
- int val = temp.first,curPos = temp.second;
- pQ.pop();
- if(arrDis[curPos]<val) continue;
- int sz = graph[curPos].size();
- for(int i = 0; i<sz; i++)
- {
- int nxPos = graph[curPos][i].first;
- int nxVal = graph[curPos][i].second;
- if(arrDis[nxPos]>(nxVal+val))
- {
- arrDis[nxPos] = nxVal+val;
- pQ.push(make_pair(arrDis[nxPos],nxPos));
- }
- }
- }
- }
- void clr()
- {
- for(int i = 0; i<20010; i++)
- {
- arrDis[i] = INT_MAX;
- }
- for(int i = 0; i<20010; i++)
- {
- graph[i].clear();
- }
- }
- int main()
- {
- int test;
- scanf("%d",&test);
- for(int i = 1; i<=test; i++)
- {
- clr();
- int node,edge,from,to;
- scanf("%d%d%d%d",&node,&edge,&from,&to);
- for(int j = 1; j<=edge; j++)
- {
- int a,b,c;
- scanf("%d%d%d",&a,&b,&c);
- graph[a].push_back(make_pair(b,c));
- graph[b].push_back(make_pair(a,c));
- }
- ans(from);
- if(arrDis[to] != INT_MAX)printf("Case #%d: %d\n",i,arrDis[to]);
- else printf("Case #%d: unreachable\n",i);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement