-
Notifications
You must be signed in to change notification settings - Fork 0
/
W6_ShortestAllPath.cpp
67 lines (61 loc) · 1.58 KB
/
W6_ShortestAllPath.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
#include <bits/stdc++.h>
using namespace std;
const int N = 10001;
const int intmax = 1e9;
int n,m,s,t;
vector<pair<int, int>> Adj[N];
int d[N], p[N];
int SP[N][N];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> JISOO;
void input(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
int a, b, c;
for(int i = 0; i<m; i++){
cin >> a >> b >> c;
Adj[a].push_back(make_pair(b,c)); // Directed edge a to b has weight c;
}
}
int Dijkstra(){
for(int s = 1; s<=n; s++){
for(int i = 1; i<=n; i++){
if(i == s) SP[s][i] = 0;
else SP[s][i] = -10;
d[i] = intmax;
}
for(pair<int, int> sx: Adj[s]){
d[sx.first] = sx.second;
p[sx.first] = s;
}
for(int v = 1; v<=n; v++){
if(v!=s) JISOO.push(make_pair(d[v], v));
}
int a = 1;
while(!JISOO.empty()){
int u = JISOO.top().second;
JISOO.pop();
SP[s][u] = d[u];
for(pair<int, int> ux: Adj[u]){
if(d[u] + ux.second < d[ux.first]){
d[ux.first] = d[u] + ux.second;
p[ux.first] = u;
JISOO.push(make_pair(d[ux.first], ux.first));
}
}
}
}
}
int main(){
freopen("inp.inp", "r", stdin);
input();
Dijkstra();
for(int i = 1; i<=n; i++){
SP[i][i] = 0;
for(int j =1; j<=n; j++){
cout << SP[i][j] << " ";
}
cout << endl;
}
return 0;
}