Sicily 1090. Highways

1090. Highways

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system. 

Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. 

The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.

Input

The first line is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. 

Output

You should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.
This problem contains multiple test cases!
The first line of a multiple input is an integer T, then a blank line followed by T input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of T output blocks. There is a blank line between output blocks.

Sample Input

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

        题意解析:给出N个点,以及两两之间的距离。一开始只有点而没有边,要求加上一些边,使这N个点两两连通,且权值最大的边的权值最小。其中权值就是连边的两个点之间的距离。

       思路:典型的kruskal算法,运用并查集实现

kruskal算法的原理、过程等可以参考http://www.cnblogs.com/yanlingyin/archive/2011/11/16/greedy.html ,作者写得挺全面的。

代码如下:(kruskal实现)

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

const int N=510;
int t,len,n;
int f[N];

struct EDGE
{
       int i,j,dis;
}edge[N*N/2];

bool cmp(EDGE a,EDGE b)
{
     return a.dis<b.dis;
 }
 
int find(int x)
{
    if(x==f[x])return x;
    return find(f[x]);
}

void merge(int x,int y)
{
    int x1,y1;
    x1=find(x);
    y1=find(y);
    if(x1!=y1)f[x1]=y1;//这一步一开始弄错了,提交时是RTE 
}

int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        int cnt=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                cin>>len;
                if(i>j)
                {
                      edge[cnt].i=i;
                      edge[cnt].j=j;
                      edge[cnt].dis=len;
                      cnt++;
                      }
            }
        }
        cnt--;
        for(int i=1;i<=n;i++)
            f[i]=i;
        
        sort(edge+1,edge+cnt+1,cmp);    
        int num=0;
        int max=0;
        for(int k=1;k<=cnt;k++)
        {
                if(find(edge[k].i)!=find(edge[k].j))
                {    
                    merge(edge[k].i,edge[k].j);
                    if(max<edge[k].dis)max=edge[k].dis;
                    num++;
                    }
                if(num==cnt-1)break;
        }
        cout<<max<<endl;
        if(t!=0)cout<<endl;
    }
    return 0;
} 

下面是很久以前用prim算法AC的,忘记思路了。。直接贴代码吧:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;


int a[501][501];
int g[500];
bool v[501];

int main()
{
    int t,n;
    cin>>t;
    int f=0;
    while(t--)
    {
        cin>>n;
        int max=0; 
        int k=0;
        for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                        cin>>a[i][j];
                        
        memset(g,0x3f,sizeof(g));
        memset(v,0,sizeof(v));
        
        v[0]=1;
        //g[0]=0;
        
        for(int i=0;i<n;i++)
        {
                g[i]=a[0][i];
                }
                
        for(int i=0;i<n-1;i++)
        {
            int len=0xffff;
            for(int i=0;i<n;i++)
            {
                    //g[i]=a[k][i];
                    if(v[i]==0 && (len>g[i])){
                                 len=g[i];
                                k=i;
                                 }
                    
                    }
             v[k]=1;
             if(len>max)max=len;
            //k++;
            
             for(int i=1;i<n;i++)
             {
                     if((v[i]==0) && (g[i]>a[k][i]))
                     {
                         g[i]=a[k][i];
                     }
             } 
             //k++;   
             
             /*int f=0;
             for(int i=0;i<n;i++)
             {
                     if(v[i]!=1)f=0;
                     }
             
             if(f==1)break;*/
        }
     if(f==0)f=1;
     else
         cout<<endl;
            
    cout<<max<<endl;
    //cout<<endl;    
    }
    //system("pause");
    return 0;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值