-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1507.cpp
More file actions
77 lines (71 loc) · 1.29 KB
/
1507.cpp
File metadata and controls
77 lines (71 loc) · 1.29 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
// 1507. 궁금한 민호
// 2019.05.18
// 플로이드 와샬 알고리즘
#include<iostream>
#include<queue>
using namespace std;
int map[21][21];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> map[i][j];
}
}
queue<pair<int, int>> q;
bool flag = true;
for (int i = 1; i <= n; i++)
{
for (int from = 1; from <= n; from++)
{
for (int to = 1; to <= n; to++)
{
if (from == to || from == i || i == to)
{
continue;
}
// from에서 i를 거쳐 to로 가는게 from에서 to로 가는 거리와 같다면
// from에서 to로 가는 간선 삭제(이미 최소라 필요가 없다)
if (map[from][to] == map[from][i] + map[i][to])
{
q.push({ from,to });
}
else if (map[from][to] > map[from][i] + map[i][to])
{
// 불가능한 경우
flag = false;
}
}
}
}
// 삭제해야될 간선들을 0으로 만들어서 삭제
while (!q.empty())
{
int first = q.front().first;
int second = q.front().second;
q.pop();
map[first][second] = 0;
}
// 간선의 합을 구함
int sum = 0;
for (int i = 1; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
sum += map[i][j];
}
}
if (flag)
{
cout << sum << endl;
}
else
{
cout << -1 << endl;
}
return 0;
}