您的位置:首页 > 其它

POJ 一 2485 Highways(最小生成树)

2013-08-27 10:47 441 查看
Highways

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 19029Accepted: 8829
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
1

3
0 990 692
990 0 179
692 179 0

Sample Output
692

典型的最小生成树。

在多个村子之间铺设高速公路,要使高速公路可以连通所有村子同时成本最低。

输出路线中最长距离。

#include<stdio.h>
#define INF 0xfffff
#define max 500

int map[max][max];
int dis[max],n,Max;

void prim();
int  min(int a,int b)
{return a>b?b:a;}

int main(void)
{
int t,i,j;

scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&map[i][j]);
}
prim();
printf("%d\n",Max);
}
return 0;
}

void prim()
{
int i,j,now;
int minedge,minnode;

for(i=1;i<=n;i++)
dis[i]=INF;
now=1;
Max=0;
for(i=1;i<n;i++)
{
dis[now]=-1;
minedge=INF;
for(j=1;j<=n;j++)
{
if(j!=now&&dis[j]>=0)
{
dis[j]=min(dis[j],map[now][j]);
if(dis[j]<minedge)
{
minedge=dis[j];
minnode=j;
}
}
}
now=minnode;
if(minedge>Max)
Max=minedge;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: