poj 1797(最大生成树)

题目链接:http://poj.org/problem?id=1797

题意: 给处一个图,n个顶点和m条边,每个边都有最大承载量,现在我要从1点运送货物到n点,求能运送货物的最大重量。

注意: 只需要从 1  到  n  , 不需要构建出完全的最大生成树,   故 用coun 计数 加入最大生成树的, 当 coun == n - 1 时才break 会WA.  最后输出的时候要输出两个空行

AC代码:

#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn = 1010;
int fa[maxn];
int maxx;
struct xx{
    int l,r,w;
    xx(){}
    xx(int l,int r,int w):l(l),r(r),w(w){}
}edge[maxn*100];
int coun = 0;

void init(){
    for(int i = 0;i < maxn;i ++)
        fa[i] = i;
    maxx = 1e9; coun = 0;
}

bool cmp(xx A,xx B){
    return A.w > B.w;
}

int _find(int x){
    if(fa[x] == x) return x;
    return fa[x] = _find(fa[x]);
}

void _union(int x,int y,int w){
    int fx = _find(x), fy = _find(y);
    if(fx == fy) return ;
    maxx = min(w,maxx);
    fa[fx] = fy;
    coun ++;
}

int main()
{
    int n,m; int x = 1;
    int t; scanf("%d",&t);
    while(t --){
        init();
        scanf("%d%d",&n,&m);
        int a,b,c;
        for(int i = 0;i < m;i ++){
            scanf("%d%d%d",&a,&b,&c);
            edge[i] = xx(a,b,c);
        }
        sort(edge,edge + m,cmp);
        for(int i = 0;i < m;i ++){
            _union(edge[i].l,edge[i].r,edge[i].w);
            if(_find(1) == _find(n)) break;
        }
        printf("Scenario #%d:\n",x ++);
        printf("%d\n\n",maxx);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值