-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAll_Pairs_Shortest_Path.cpp
More file actions
74 lines (64 loc) · 1.86 KB
/
All_Pairs_Shortest_Path.cpp
File metadata and controls
74 lines (64 loc) · 1.86 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
#include <bits/stdc++.h>
using namespace std;
typedef long long llong;
constexpr int INFY = 2000000000;
// ワーシャルフロイド法
void Warshall_Floyd_Algorithm(vector<vector<llong>>& dp, int size)
{
for (int k = 0; k < size; k++)
{
for (int i = 0; i < size; i++)
{
if(dp[i][k] == INFY) continue;
for (int j = 0; j < size; j++)
{
if(dp[k][j] == INFY) continue;
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}
}
}
}
int main() {
int num_vertex(0),num_edge(0);
cin >> num_vertex >> num_edge;
vector<vector<llong>> dp(num_vertex, vector<llong>(num_vertex, INFY));
// ある頂点から自分に移動するコストは0
for (int i = 0; i < dp.size(); i++)
{
dp[i][i] = 0;
}
for (int i = 0; i < num_edge; i++)
{
llong edge1, edge2, cost;
cin >> edge1 >> edge2 >> cost;
// 辺の重みを記録する
// 有向グラフなので逆方向は記録しない
dp[edge1][edge2] = min(dp[edge1][edge2], cost);
}
// ワーシャルフロイド法でコストを求める
Warshall_Floyd_Algorithm(dp, num_vertex);
// 結果を表示する
// 負の閉路があるかチェック
for (int i = 0; i < dp.size(); i++)
{
if (dp[i][i] < 0) {
cout << "NEGATIVE CYCLE" << "\n";
return 0;
}
}
// 負の経路がなければ各コストを表示する
for (int i = 0; i < dp.size(); i++)
{
for (int j = 0; j < dp.size(); j++)
{
string tmp = dp[i][j] == INFY? "INF" : to_string(dp[i][j]);
if (j == dp.size() - 1)
{
cout << tmp << "\n";
} else {
cout << tmp << " ";
}
}
}
return 0;
}