zoukankan      html  css  js  c++  java
  • [POJ2485]Highways

    题目大意:Flatopia岛要修路,这个岛上有n个城市,要求修完路后,任意城市都可以到达其他城市,且修的总长度最短。

    解题思路:很明显裸的最小生成树,只不过是求最大的边的权值。我用的是Kruskal。

    C++ Code:

     1 #include<algorithm>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 struct bian{
     6     int u,v,d;
     7     bool operator <(const bian& rhs)const{return d<rhs.d;}
     8 }a[500*500+25];
     9 int m=0,n,dad[505],s;
    10 int fa(int x){
    11     return(dad[x]==x)?(x):(dad[x]=fa(dad[x]));
    12 }
    13 int main(){
    14     scanf("%d",&s);
    15     while(s--){
    16         memset(a,0,sizeof(a));
    17         scanf("%d",&n);
    18         for(int i=1;i<=n;i++)
    19         for(int j=1;j<=n;j++){
    20             int x;
    21             scanf("%d",&x);
    22             if(x!=0){
    23                 m++;
    24                 a[m].u=i,a[m].v=j,a[m].d=x;
    25             }
    26         }
    27         sort(a+1,a+m+1);
    28         for(int i=1;i<=n;i++)dad[i]=i;
    29         int now=0,T=1;
    30         while(T!=n){
    31             now++;
    32             int x=fa(a[now].u),y=fa(a[now].v);
    33             if(x!=y){
    34                 dad[y]=x;
    35                 T++;
    36                 if(T==n){//排过序后最后一个就是最大的。
    37                     printf("%d
    ",a[now].d);
    38                 }
    39             }
    40         }
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    codevs2606 约数和问题
    UOJ150 运输计划
    codevs1279 Guard 的无聊
    codevs1997 守卫者的挑战
    codevs1291 火车线路
    codevs1217 借教室
    codevs1281 Xn数列
    codevs1218 疫情控制
    codevs1199 开车旅行
    BZOJ1941 [Sdoi2010]Hide and Seek
  • 原文地址:https://www.cnblogs.com/Mrsrz/p/6885664.html
Copyright © 2011-2022 走看看