-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.cpp
93 lines (86 loc) · 1.88 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
82
83
84
85
86
87
88
89
90
91
92
93
/**
**
** Dijkstra's algorithm
** Complexity : O((E+V)logV)
** Implemented using priority queue. (It can also be implemented using heaps.)
**
** Sample Problem : http://www.spoj.com/problems/EZDIJKST/
**
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAXN = 1e6 + 10;
const LL MOD = 1e9 + 7;
const LL INF = 1e15;
typedef vector<pair<LL, LL> > Graph;
//---------------------------------------------------------------
LL dis[MAXN];
inline void dijkstra(Graph G[],LL N,LL src){
for(LL i = 1;i <= N;++i) dis[i] = INF;
dis[src] = 0;
priority_queue<pair<LL, LL>, vector<pair<LL, LL> >, greater<pair<LL, LL> > > PQ;
PQ.push(make_pair(0, src));
while(!PQ.empty()){
cout << PQ.top().first << " " << PQ.top().second << "\n";
LL cur = PQ.top().second; PQ.pop();
LL sz = G[cur].size();
for(LL i = 0;i < sz;++i){
pair<LL, LL> curPair = G[cur][i];
LL next = curPair.first;
LL w = curPair.second;
if(dis[next] > dis[cur] + w){
//This condition will always make algorithm in negative weights but not in negative cycle.
dis[next] = dis[cur] + w;
PQ.push(make_pair(dis[next], next));
}
}
}
}
//---------------------------------------------------------------
int main(){
LL tc; cin >> tc;
while(tc--){
LL N, K; cin >> N >> K;
Graph G[N + 1];
while(K--){
LL u, v, c; cin >> u >> v >> c;
G[u].push_back(make_pair(v,c)); // Directed Graph
// G[v].push_back(make_pair(u,c));
}
LL A;cin >> A;
dijkstra(G, N, A);
/*cout << "Final result\n";
for(LL i = 1;i <= N;++i){
cout << i << " " << dis[i] << "\n";
}*/
LL B;cin >> B;
if(dis[B] == INF){
printf("NO\n");
}else{
printf("%lld\n",dis[B]);
}
}
return 0;
}
/**
Fail Test Case
1
4 4
1 2 5
1 3 2
2 3 -10
3 4 10
1 4
Sample Test Case :
1
5 7
1 2 2
1 3 1
1 4 6
2 5 1
4 5 5
3 4 4
3 5 2
1 5
*/