-
Notifications
You must be signed in to change notification settings - Fork 308
/
dijkstra.cpp
81 lines (62 loc) · 1.38 KB
/
dijkstra.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
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>
using namespace std;
#define MAXN 105
#define INF 1<<30
typedef struct node{
int t, c;
node(){}
node(int T, int C) : t(T), c(C){}
bool operator < (const node &x) const{
return c < x.c;
}
}No;
vector<No> adj[MAXN];
int mark[MAXN];
int d[MAXN];
priority_queue<No> pq;
int dijkstra(int s, int t){
for(int i = 0; i < MAXN; i++) {
mark[i] = 0;
d[i] = INF;
}
printf("%d %d", s, t);
pq.push(No(s,0));
while(!pq.empty()){
No top = pq.top(); pq.pop();
if(top.t == t){
return top.c;
}
if(mark[top.t] == 1) continue;
mark[top.t] = 1;
for(int i = 0; i < adj[top.t].size(); i++){
int v = adj[top.t][i].t;
int cost = adj[top.t][i].c;
if (top.c + cost < d[v]){
d[v] = top.c + cost;
pq.push(No(v, top.c + cost));
}
}
}
return -1;
}
int main(){
int T; // Number of edges
scanf("%d", &T);
//Nodes are numbered from 0 to MAXN-1
while(T-- > 0){
int a,b,c;
scanf("%d %d %d", &a, &b, &c);
adj[a].push_back(No(b,c)); //Unidirectional adj
}
int source, destination;
scanf("%d %d", &source, &destination);
int result = dijkstra(source, destination);
if(result == -1){
printf("There is no path to destination\n");
}else{
printf("The path from %d to %d has cost %d\n", source, destination, result);
}
}