Problem Description Problem Analysis Given an n*n matrix the j-th column of the i-th row represents the distance between i and j Find the largest distance among the edges of the minimum-total-distance network connecting the n points
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
#include<stdio.h>
#include<algorithm>
using namespace std ;
struct edge {
int u , v , dis ;
} e [ 520 * 520 ];
int par [ 520 ];
int ran [ 520 ];
int find ( int m ) {
if ( m == par [ m ])
return m ;
else
return par [ m ] = find ( par [ m ]);
}
void unite ( int x , int y ) {
x = find ( x );
y = find ( y );
if ( x == y )
return ;
if ( ran [ x ] < ran [ y ])
par [ x ] = y ;
else {
par [ y ] = x ;
if ( ran [ x ] == ran [ y ])
ran [ x ] ++ ;
}
}
bool cmp ( edge A , edge B ) {
return A . dis < B . dis ;
}
int main () {
int T ;
scanf ( "%d" , & T );
while ( T -- ) {
int N ;
scanf ( "%d" , & N );
for ( int i = 1 ; i <= N ; i ++ ) {
par [ i ] = i ;
ran [ i ] = 0 ;
}
int cnt = 0 ;
for ( int i = 1 ; i <= N ; i ++ ) {
for ( int j = 1 ; j <= N ; j ++ ) {
scanf ( "%d" , & e [ cnt ]. dis );
e [ cnt ]. u = i ;
e [ cnt ++ ]. v = j ;
}
}
sort ( e , e + cnt , cmp );
int res = 0 ;
for ( int i = 0 ; i < cnt ; i ++ ) {
if ( find ( e [ i ]. u ) != find ( e [ i ]. v )) {
if ( res < e [ i ]. dis )
res = e [ i ]. dis ;
unite ( e [ i ]. u , e [ i ]. v );
}
}
printf ( "%d \n " , res );
}
}
This post is licensed under CC BY-NC-SA 4.0 . If it helped, or if you have anything to say, the comment box below is open. Page views
—
Unique visitors
—