-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharry_porte_floyd.cpp
69 lines (66 loc) · 1.5 KB
/
harry_porte_floyd.cpp
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
#include <iostream>
#include <vector>
using namespace std;
#define INF 101
int graph[101][101];
void floyd(int N)
{
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= N; ++j)
for (int k = 1; k <= N; ++k)
{
if (graph[j][i] + graph[i][k] < graph[j][k])
graph[j][k] = graph[j][i] + graph[i][k];
}
}
int main()
{
int N, M;
cin >> N >> M;
//init graph
for (int i = 1; i <= N; ++i)
{
for (int j = 1; j <= N; ++j)
{
if (i == j)
graph[i][j] =0;
else
graph[i][j] = INF;
}
}
for (int i = 1; i <= M; ++i)
{
int from, to, val;
cin >>from >>to >>val;
graph[from][to] = graph[to][from] = val;
}
//process
floyd(N);
vector<int> maxdis;
// find the max distance for each point
for (int i = 1; i <= N; ++i)
{
int temp = 0;
for (int j = 1; j <= N; ++j)
if (graph[i][j] > temp)
temp = graph[i][j];
maxdis.push_back(temp);
}
//find the min of maxdistance and markdown the index
int minmaxdis = 10001;
int mintag = -1;
for (int i = 0; i < N; ++i)
{
if (minmaxdis > maxdis[i])
{
mintag = i +1;
minmaxdis = maxdis[i];
}
}
//output
if (minmaxdis == INF) // in case of no solve way
cout <<'0'<<endl;
else
cout <<mintag<<' '<<minmaxdis<<endl;
return 0;
}