-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
75 lines (69 loc) · 1.23 KB
/
dijkstra.cpp
File metadata and controls
75 lines (69 loc) · 1.23 KB
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
#include<stdio.h>
#include<queue>
#include<vector>
using namespace std;
#define SZ 100
#define MAX 2147483647
int cost[SZ], wght[SZ][SZ], seen[SZ][SZ], init, pi[SZ];
class comp{
public:
bool operator()(const int &a, const int &b)const{
return cost[a] > cost[b];
}
};
priority_queue<int, vector<int>, comp> q;
vector<int> edge[SZ];
int dijkstra(int S, int E){
int u, v, i;
cost[S] = 0;
pi[S] = -1;
q.push(S);
while(!q.empty()){
u = q.top();
for(i=0; i<edge[u].size(); i++){
v = edge[u][i];
if(seen[u][v] == init)continue;
if(cost[v] > wght[u][v] + cost[u]){
cost[v] = wght[u][v] + cost[u];
pi[v] = u;
q.push(v);
}
}
q.pop();
}
return cost[E];
}
void print_path(int i){
if(pi[i] == -1){
printf(" %d", i);
return;
}
print_path(pi[i]);
printf(" %d", i);
}
int main(){
int n, i, j, t, w, v, S, E, C=1;
init = 1;
while(scanf("%d", &n)){
if(!n)break;
for(i=1; i<=n; i++)
{
scanf("%d", &t);
edge[i].clear();
cost[i] = MAX;
for(j=0; j<t; j++)
{
scanf("%d%d", &v, &w);
edge[i].push_back(v);
wght[i][v] = w;
}
}
scanf("%d%d", &S, &E);
w = dijkstra(S, E);
printf("Case %d: Path =", C++);
print_path(E);
printf("; %d second delay\n", w);
init++;
}
return 0;
}