-
Notifications
You must be signed in to change notification settings - Fork 1
/
UVa 10986.cpp
69 lines (55 loc) · 1.32 KB
/
UVa 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
#include <bits/stdc++.h>
#define mem(x,y) memset(x,y,sizeof(x));
using namespace std;
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define MAXX 20001
#define INF 538976288
vector< pair<int, int> > adj_l[MAXX];
int dist[MAXX];
int V, E;
void init(){
for(int i = 0; i < MAXX; i++)
adj_l[i].clear();
mem(dist, INF);
}
void input(int u, int v, int w){
adj_l[u].push_back(make_pair(w, v));
adj_l[v].push_back(make_pair(w, u));
}
void dijkstra(int src){
priority_queue<pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > > pq;
pq.push(make_pair(0, src));
dist[src] = 0;
while(!pq.empty()){
int u = pq.top().second;
pq.pop();
for(int i = 0; i < adj_l[u].size(); i++){
int v = adj_l[u][i].second;
int weight = adj_l[u][i].first;
if(dist[v] > (dist[u] + weight)){
dist[v] = (dist[u] + weight);
pq.push(make_pair(dist[v], v));
}
}
}
}
int main(){
READ("UVa 10986.txt");
int tc, case_no = 0, u, v, w, src, dest;
scanf("%d", &tc);
while(tc--){
init();
scanf("%d %d %d %d", &V, &E, &src, &dest);
for(int i = 0; i < E; i++){
scanf("%d %d %d", &u, &v, &w);
input(u, v, w);
}
dijkstra(src);
if(dist[dest] == INF)
printf("Case #%d: unreachable\n", ++case_no);
else
printf("Case #%d: %d\n", ++case_no, dist[dest]);
}
return 0;
}