-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUVA_10986.cpp
73 lines (66 loc) · 1.94 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
70
71
72
73
#include <bits/stdc++.h>
using namespace std;
#define sz 100000
#define inf 100000000
typedef pair<int,int> pii;
void dijkstra(int startingNode,int vertex);
vector <int> adj[sz];
vector <int> cost[sz];
int distant[sz];
int visited[sz];
int parent[sz];
int main() {
int testcase;
scanf("%d",&testcase);
int caseno = 0 ;
while(testcase--) {
int n,m,s,t ;
scanf("%d %d %d %d",&n,&m,&s,&t);
for ( int i = 0 ; i < m ; i++) {
int node1,node2,weight;
scanf("%d %d %d",&node1,&node2,&weight);
adj[node1].push_back(node2);
adj[node2].push_back(node1);
cost[node1].push_back(weight);
cost[node2].push_back(weight);
}
dijkstra(s,n);
if ( parent[t] == -1) printf("Case #%d: unreachable\n",++caseno);
else printf("Case #%d: %d\n",++caseno,distant[t]);
for ( int i = 0 ; i < sz ; i++) {
adj[i].clear();
cost[i].clear();
}
}
return 0 ;
}
void dijkstra(int startingNode, int vertex) {
for ( int i = 0 ; i < vertex ; i++) {
if ( i != startingNode) {
distant[i] = inf ;
parent[i] = -1 ;
visited[i] = false;
}
}
distant[startingNode] = 0 ;
parent[startingNode] = startingNode;
visited[startingNode] = false;
priority_queue < pii , vector <pii> , greater <pii > > Q ;
Q.push(make_pair(distant[startingNode],startingNode));
while(!Q.empty()) {
pii p = Q.top();
Q.pop();
int v = p.second;
if ( visited[v] == false) {
for ( int i = 0 ; i < adj[v].size() ; i++) {
int x = adj[v][i];
if ( cost[v][i] + distant[v] < distant[x]) {
distant[x] = cost[v][i] + distant[v];
parent[x] = v ;
Q.push(make_pair(distant[x],x));
}
}
}
visited[v] = true;
}
}