最小生成树中的最长边,地址: http://acm.pku.edu.cn/JudgeOnline/problem?id=1861
#include <stdio.h> #include <stdlib.h>
const int LEN = 15005;
struct segment { int b; int e; int len; }seg[LEN];//线段
int used[LEN];
void init ( int n ) { for ( int i=0; i<n; i++ ) { used[i] = 0; } }
//并查集部分 int fa[LEN/10];
void make ( int n ) {
for ( int i=0; i<n; i++ ) { fa[i] = -1; } }
int find ( int a ) {
if ( fa[a]<0 ) { return a; }
int r = find ( fa[a] ); fa[a] = r; return r; }
void un ( int a, int b ) {
int ra = find ( a ); int rb = find ( b );
if ( fa[ra]<fa[rb] ) { fa[ra] += fa[rb]; fa[rb] = ra; } else { fa[rb] += fa[ra]; fa[ra] = rb; } }
//主要处理部分
int cmp ( const void *a, const void *b ) {
return ( (segment *)a )->len - ( (segment *)b )->len; }
void kul ( int n, int m ) {
qsort ( seg, m, sizeof ( segment ), cmp );
int max = -1; int count = 0; init ( n ); make ( n ); for ( int i=0; i<m; i++ ) { if ( find ( seg[i].b )!=find ( seg[i].e ) ) { un ( seg[i].b, seg[i].e );
used[i] = 1; count ++; if ( max < seg[i].len ) { max = seg[i].len; } } }
printf ( "%d\n%d\n", max, count ); for ( int i=0; i<m; i++ ) { if ( used[i] ) { printf ( "%d %d\n", seg[i].b+1, seg[i].e+1 ); } } }
int main () {
int n, m; int b, e, len;
while ( scanf ( "%d%d", &n, &m ) != EOF ) { for ( int i=0; i<m; i++ ) { scanf ( "%d%d%d", &b, &e, &len ); seg[i].b = b-1<e-1 ? b-1:e-1; seg[i].e = b-1>e-1 ? b-1:e-1; seg[i].len = len; }
kul ( n, m ); } return 0; }
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
29 | 30 | 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 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|
公告
决定从线程开始!!
常用链接
留言簿(6)
随笔档案
搜索
最新评论
阅读排行榜
评论排行榜
|
|