码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2485 Highways

时间:2014-07-20 23:16:08      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   for   re   c   

链接:poj 2485

题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度

这个也是最小生成树的题,只不过要求的不是最小价值,而是最小生成树中的最大权值,只需要加个判断

比较最小生成树每条边的大小就行

#include<cstdio>
#include<algorithm>
using namespace std;
int f[510],n,m;
struct stu
{
    int a,b,c;
}t[20100];
int cmp(struct stu x,struct stu y)
{
    return x.c<y.c;
}
int find(int x)
{
    if(x!=f[x])
        f[x]=find(f[x]);
    return f[x];
}
int krus()
{
    int i,k=0,s=0,x,y;
    for(i=1;i<m;i++){
        x=find(t[i].a);
        y=find(t[i].b);
        if(x!=y){
            if(t[i].c>s)           //比较最小生成树中权值的大小
                s=t[i].c;
            k++;
            if(k==n-1)
                break;
            f[x]=y;
        }
    }
    return s;
}
int main()
{
    int T,i,j,c,s=0;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        m=1;
        for(i=1;i<=n;i++){
            f[i]=i;
            for(j=1;j<=n;j++){
                scanf("%d",&c);
                if(j<i){
                    t[m].a=i;
                    t[m].b=j;
                    t[m++].c=c;
                }
            }
        }
        sort(t+1,t+m,cmp);
        s=krus();
        printf("%d\n",s);
    }
    return 0;
}


poj 2485 Highways,布布扣,bubuko.com

poj 2485 Highways

标签:style   http   io   for   re   c   

原文地址:http://blog.csdn.net/acm_code/article/details/37992469

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!