-
Notifications
You must be signed in to change notification settings - Fork 0
/
10986 - Sending email.cpp
71 lines (67 loc) · 1.4 KB
/
10986 - Sending email.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
#include<climits>
#define pp pair<int,int>
#define MX 10000+5
using namespace std;
int const MAX=50001;
int dis[MAX];
vector<pp>adj[MAX];
bool dijkastra(int s,int t)
{
memset(dis,10010,sizeof(dis));
int i,u,v,w;
priority_queue<pp,vector<pp>,greater<pp> >pq;
pq.push(pp(0,s));
dis[s]=0;
while(!pq.empty())
{
u=pq.top().second;
pq.pop();
if(u==t)
{
while(!pq.empty())pq.pop();
return true;
}
for(i=0;i<adj[u].size();i++)
{
v=adj[u][i].first;
w=adj[u][i].second;
if(dis[u]+w<dis[v]){
dis[v]=dis[u]+w;
pq.push(pp(dis[v],v));
}
}
}
while(!pq.empty())
pq.pop();
return false;
}
int main()
{
int test,k=1;
int i,u,v,w;
int n,e,s,t;
cin>>test;
while(test--)
{
cin>>n>>e>>s>>t;
for(i=0; i<e; i++)
{
cin>>u>>v>>w;
adj[u].push_back(pp(v,w));
adj[v].push_back(pp(u,w));
}
cout<<"Case #"<<k<<":";
if(e!=0 && dijkastra(s,t))
cout<<dis[t];
else
cout<<"unreachable";
cout<<endl;
memset(adj,0,sizeof(adj));
k++;
}
return 0;
}