-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1096.cpp
More file actions
46 lines (46 loc) · 979 Bytes
/
1096.cpp
File metadata and controls
46 lines (46 loc) · 979 Bytes
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
#include <bits/stdc++.h>
#define INF 9999999
using namespace std;
struct WE {
int v, w;
};
const int _N = 110;
int N;
vector<WE> G[_N];
int dpt[_N][_N] = {INF};
inline void init_G_dpt() {
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
int w;
cin >> w;
if (w == 0) continue;
G[i].push_back({j, w});
dpt[i][j] = w;
}
}
inline void dp() {
for (int k = 0; k < N; k++)
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
dpt[i][j] = min(dpt[i][k] + dpt[k][j], dpt[i][j]);
}
}
main(void) {
cin.tie(0);
ios_base::sync_with_stdio(0);
while (cin >> N && N) {
for (int i = 0; i < N; i++) G[i].clear();
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
dpt[i][j] = INF;
init_G_dpt();
dp();
int res = INF;
for (int i = 0; i < N; i++) res = min(res, dpt[i][i]);
if (res == INF)
cout << "-1\n";
else
cout << res << '\n';
}
return 0;
}