-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf80789_9a.cpp
More file actions
87 lines (78 loc) · 1.16 KB
/
f80789_9a.cpp
File metadata and controls
87 lines (78 loc) · 1.16 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
76
77
78
79
80
81
82
83
84
85
86
87
#include <cstdio>
using namespace std;
const int N = 1 << 10;
int a[N][N], m;
int INF = 1 << 20;
int n = 0;
void input()
{
for (int i = 0; i < m; i++)
{
int u, v, d;
scanf("%d%d%d", &u, &v, &d);
a[u][v] = d;
if (u > n) n = u;
if (v > n) n = v;
}
}
void floyd()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (a[i][j] == 0) a[i][j] = INF;
}
}
for (int k = 1; k <= n; k++)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (a[i][j] > (a[i][k] + a[k][j]))
{
a[i][j] = a[i][k] + a[k][j];
}
}
}
}
for (int i = 1; i <= n; i++)
{
a[i][i] = 0;
}
}
void print_graph()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (a[i][j] == INF) printf("-1 ");
else printf("%d ", a[i][j]);
}
printf("\n");
}
}
void clear_graph()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
a[i][j] = 0;
}
}
n = 0;
}
int main()
{
while (scanf("%d", &m) != EOF)
{
input();
floyd();
print_graph();
clear_graph();
}
return 0;
}