POJ

来源:互联网 发布:db2数据库创建用户 编辑:程序博客网 时间:2024/05/01 05:54
Highways
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 32607 Accepted: 14822

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 of input is an integer T, which tells how many test cases followed. 
The first line of each case 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. There is an empty line after each test case.

Output

For each test case, 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.

Sample Input

130 990 692990 0 179692 179 0

Sample Output

692
给一个矩阵 其中 a[i][j] 为i到j的距离(对称的a[j][i]是可能不一样的!!!,不要问我为什么知道),
    问最优铺路情况下,最长的一段路是多少
Kruskal 算法
#include<queue>#include<algorithm>#include<cstdio>#include<iostream>using namespace std;#define min(a,b) ((a)>(b)?(b):(a))int CheckSet[10000];int LeaderCheckSet(int x){    if(x!=CheckSet[x])        CheckSet[x]=LeaderCheckSet(CheckSet[x]);    return CheckSet[x];}void InsertCheckSet(int x,int y){    x=LeaderCheckSet(x);    y=LeaderCheckSet(y);    CheckSet[max(x,y)]=min(x,y);}struct AP{    int x,y,value;    bool operator < (const AP &a)const    {        return value>a.value;    }};AP func1(int x,int y,int z){    AP t;    t.x=x;    t.y=y;    t.value=z;    return t;}int main(){    int N;    scanf("%d",&N);    while(N--)    {        int n;        scanf("%d",&n);        for(int i=0; i<=n; i++)            CheckSet[i]=i;        priority_queue<AP>que1;        for(int i=0; i<n; i++)            for(int j=0; j<n; j++)            {                int temp;                scanf("%d",&temp);                if(i!=j)                que1.push(func1(i,j,temp));            }        int sum=0;        while(que1.size())        {            AP temp=que1.top();            que1.pop();            if(LeaderCheckSet(temp.x)!=LeaderCheckSet(temp.y))            {                InsertCheckSet(temp.x,temp.y);                sum++;                if(sum==n-1)                {                    printf("%d\n",temp.value);                    break;                }            }        }    }    return 0;}

Prim 算法。
#include<iostream>#include<cstdio>using namespace std;int main(){    int N;    scanf("%d",&N);    while(N--)    {        int n,com[508][508]= {};        scanf("%d",&n);        for(int i=1; i<=n; i++)            for(int j=1; j<=n; j++)                scanf("%d",&com[i][j]);        bool visit[508]= {false,true};        int T=n-1,ans=0;        while(T--)        {            int Min=100000,temp1,temp2;            for(int i=1; i<=n; i++)                if(visit[i])                    for(int j=1; j<=n; j++)                        if((!visit[j])&&com[i][j]&&com[i][j]<Min)                            {                                Min=com[i][j];                                temp1=i;                                temp2=j;                            }            ans=max(ans,com[temp1][temp2]);            if(!T)                printf("%d\n",ans);            visit[temp2]=true;        }    }    return 0;}


原创粉丝点击