-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_1.cpp
More file actions
72 lines (68 loc) · 1.56 KB
/
Copy path07_1.cpp
File metadata and controls
72 lines (68 loc) · 1.56 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
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int MaxNode = 100;
const int INF = 101;
struct Graph {
int M;
int N;
int value[MaxNode][MaxNode];
};
void floyd(Graph &G, int D[][MaxNode])
{
for (int i=0; i<G.N; ++i)
for (int j=0; j<G.N; ++j) {
if (i==j) D[i][j] = 0;
else D[i][j] = G.value[i][j];
}
for (int k=0; k<G.N; ++k)
for (int i=0; i<G.N; ++i)
for (int j=0; j<G.N; ++j)
if ( D[i][j] > D[i][k] + D[k][j] )
D[i][j] = D[i][k] + D[k][j];
}
int findLong( int array[], int n )
{
int max(0),i;
if ( array[max] == INF ) return INF;
for (i=1; i<n; ++i) {
if ( array[i] == INF ) return INF;
if ( array[max] < array[i] )
max = i;
}
return array[max];
}
int findShort( int array[], int n)
{
int min(0), i;
for (i=1; i<n; ++i) {
if ( array[min] > array[i] )
min = i;
}
if ( array[min] == INF ) cout << 0;
else cout << min+1 << " " << array[min];
}
int main()
{
int D[MaxNode][MaxNode], i, j;
Graph G;
cin >> G.N >> G.M;
for (i=0; i<G.N; ++i)
for (int j=0; j<G.N; ++j)
G.value[i][j] = INF;
int x, y;
for (i=0; i<G.M; ++i) {
cin >> x >> y;
--x, --y;
cin >> G.value[x][y];
G.value[y][x] = G.value[x][y];
}
floyd(G, D);
int longlength[G.N];
for (i=0; i<G.N; ++i) {
longlength[i] = findLong( D[i], G.N );
}
findShort( longlength, G.N);
return 0;
}