-
Notifications
You must be signed in to change notification settings - Fork 0
/
10986.cpp
100 lines (94 loc) · 1.75 KB
/
10986.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
10986 - Sending email
*/
#include<bits/stdc++.h>
using namespace std;
#define INF 1<<30
struct node
{
int u;
int cost;
node(int _u,int _cost)
{
u =_u;
cost =_cost;
}
bool operator<(const node& p)const
{
return cost>p.cost;
}
};
void dijkstra(int n,vector<int>graph[],vector<int>cost[],int source,int dest)
{
int distance[n+1];
for(int i=0; i<=n; i++)
{
distance[i]=INF;
}
priority_queue<node>q;
q.push(node(source,0));
distance[source]=0;
while(!q.empty())
{
node top = q.top();
q.pop();
int u = top.u;
for(int i=0; i<(int)graph[u].size(); i++)
{
int v = graph[u][i];
if(distance[u]+cost[u][i]<distance[v])
{
distance[v]=distance[u]+cost[u][i];
q.push(node(v,distance[v]));
}
}
}
if(distance[dest]==INF)
{
cout<<"unreachable"<<endl;
}
else
{
cout<<distance[dest]<<endl;
}
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int tc,cas=0;
cin>>tc;
while(tc--)
{
int node,edge,s,t;
vector<int>graph[20000+5],cost[20000+5];
cin>>node>>edge>>s>>t;
for(int i=0; i<edge; i++)
{
int u,v,c;
cin>>u>>v>>c;
graph[u].push_back(v);
graph[v].push_back(u);
cost[u].push_back(c);
cost[v].push_back(c);
}
cout<<"Case #"<<(++cas)<<": ";
dijkstra(node,graph,cost,s,t);
}
return 0;
}
/*
Sample Input
3
2 1 0 1
0 1 100
3 3 2 0
0 1 100
0 2 200
1 2 50
2 0 0 1
Sample Output
Case #1: 100
Case #2: 150
Case #3: unreachable
*/