-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCapitalCity676.cpp
More file actions
59 lines (58 loc) · 1.25 KB
/
CapitalCity676.cpp
File metadata and controls
59 lines (58 loc) · 1.25 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
#include <iostream>
#define INF 0xFFFFFFF
using namespace std;
int dist[101][101];
int num = 0;
int n, m;
/*for this program you only need to calculate the upper diagonal elements because the dist[i][j]==dist[j][i]*/
void floydwarshall() {
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
if(dist[i][k]<INF)
for (int j = i+1; j <= n; j++) {//only upper diagonal i+1
if (dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
dist[j][i] = dist[i][j];//same distance as the other diagonal
}
}
}
}
}
int main() {
int n1 = 100;
while (cin >> n >> m) {
for (int i = 1; i <= n1; i++) {
for (int j = 1; j <= n1; j++) {
if (i == j)
dist[i][j] = 0;
else
dist[i][j] = INF;
}
}
n1 = n;
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
dist[u][v] = w;
dist[v][u] = w;
}
floydwarshall();
int sum = 0;
int index = 0;
for (int i = 1; i <= n; i++) {
int s = 0;
for(int j=1;j<=i;j++){
s += dist[j][i];//for elements before the diagonal
}
for (int j = i + 1; j <= n; j++) {
s += dist[i][j];// for elements after the diagonal
}
if (sum > s || sum == 0) {
sum = s;
index = i;
}
}
cout << index << endl;
}
return 0;
}