POJ-2485-Highways

点击打开链接


Highways
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 32183 Accepted: 14637

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


题意:就是先找出能把所有城镇连起来,并且修的路最短的路线,输出该路线中最长的那条路

如果直接求的话会超时,要做点优化。在找路的时候,只要找到n-1条路就不用再找了,在输入的时候也要注意,

题中给的时二维表,数据是对称的,只要将i<j或者i>j的数据输入就行了;

kruskal算法

#include<stdio.h>
#include<algorithm>
using namespace std;
int par[500*500];
struct node{
	int s,e,w;
}p[500*500];
bool cmp(node a,node b)	//排序,按照路长 从小到大排序 
{
	return a.w<b.w;
}
void init(int n)	//节点初始化 
{
	for(int i=1;i<=n;i++)
		par[i]=i;
}
int find(int x)	//找根节点 
{
	return x==par[x]?x:par[x]=find(par[x]);
 }
int unite(int a,int b)	//连接节点 
{
	int fa=find(a),fb=find(b);
	if(fa!=fb)
	{
		par[fa]=fb;
		return 1;
	}
	return 0;
 }
 int main()
 {
 	int T,n,map[555][555];
 	scanf("%d",&T);
 	while(T--)
 	{
 		int k=1;
 		int ans=0;	//记录选中路里面的最大值 
		scanf("%d",&n);
		init(n); 	//调用初始化函数 
		for(int i=1;i<=n;i++)	//输入 
			for(int j=1;j<=n;j++)
			{
				scanf("%d",&map[i][j]);
				if(i<j)		//只要将i<j或者i>j的输入 
				{
					p[k].s=i;
					p[k].e=j;
					p[k].w=map[i][j];
					k++;							
				}	
			}

		int num=0;		//记录路的条数 
		sort(p+1,p+k+1,cmp);	//如果是从k=0开始存的要写成sort(p,p+k,cmp) 
		for(int i=1;i<k;i++)
		{
			if(unite(p[i].s,p[i].e))
			{
				ans=max(p[i].w,ans);	//更新最大值 
				num++;	//路的条数 
			}
			if(num==n-1)	//选够了,就退出。算是优化吧,避免超时 
			break;
		}
		printf("%d\n",ans);			
	 }
 	return 0;
}

prim算法

#include<stdio.h>
#include<cstring>
#define MAX 502
using namespace std;
 
int map[MAX][MAX];
int n;
int dis[MAX];	//dis[i]记录树里的几点到节点i的最小距离 
int vis[MAX];	//表示节点是否加入树里了,1表示放到树里 
int prime()
{
	int max=0;	//选中路径中的最大距离 
	int index;	//记录新的节点,初始化为第一个节点 
	vis[1]=true;	//将第一个节点加入树中 
	for(int i=1;i<=n;i++)	//记录第一个节点到其他节点的距离 
		dis[i]=map[1][i];
	 for(int j=2;j<=n;j++)	//已经有了一个节点,所以从2开始 
	 {
	 	int min=0x3f3f3f3f;	//记录最小的距离,min只要大于最大的距离就行 
	 	for(int i=1;i<=n;i++)	//找出最小的边 
	 	{
	 		if(!vis[i]&&dis[i]<min)	
	 		{
	 			min=dis[i];
				 index=i;	//记录最短距离所对应的节点	
			}
		}
		vis[index]=true;	//标记节点,加入树中 
		if(max<min)	//更新最大的长度 
		max=min;
		for(int i=1;i<=n;i++)
		{
			/*如果从新节点到节点i的距离比从第一个节点到节点i的距离小,
			  更新到节点i的最小距离 */
			if(!vis[i]&&dis[i]>map[index][i])	 
			{
				dis[i]=map[index][i];
			}
		} 
	 }
	 return max; 
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		vis[i]=0; 
		memset(vis,false,sizeof(vis));	//初始化,开始时都不在树里 
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				scanf("%d",&map[i][j]);
			int ans=prime();
		printf("%d\n",ans);
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值