POJ 题目2485 Highways(最小生成树最大边)

Highways
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 23073 Accepted: 10631

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

Hint

Huge input,scanf is recommended.

Source

POJ Contest,Author:Mathematica@ZSU
ac代码
#include<stdio.h>
#include<string.h>
#define INF 0xfffffff
int map[550][550],n,v[550];
int prim()
{
	int i,j,min,flag,max=0;
	memset(v,0,sizeof(v));
	v[1]=1;
	for(i=2;i<=n;i++)
	{
		flag=-1;
		min=INF;
		for(j=1;j<=n;j++)
		{
			if(!v[j]&&map[1][j]<min)
			{
				flag=j;
				min=map[1][j];
			}
		}
		if(min>max)
			max=min;
		v[flag]=1;
		for(j=1;j<=n;j++)
		{
			if(!v[j]&&map[1][j]>map[flag][j])
			{
				map[1][j]=map[flag][j];
			}
		}
	}
	return max;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int i,j;
		scanf("%d",&n);
		memset(map,0,sizeof(map));
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
				scanf("%d",&map[i][j]);
		}
		printf("%d\n",prim());
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Kruskal算法是一种用来求解最小生成树的贪心算法。它的基本思想是,按照边的权值从小到大的顺序选择边,并且保证所选的边不会形成环,直到选取了n-1条边为止。另外,Kruskal算法还需要使用并查集来判断两个节点是否属于同一个连通分量。 在具体的实现过程中,可以按照以下步骤进行: 1. 将图中的所有边按照权值从小到大排序。 2. 创建一个并查集,并初始化每个节点为一个独立的集合。 3. 遍历排序后的边列表,对于每一条边(u, v),判断u和v是否属于同一个连通分量。如果不属于,则将这条边加入最小生成树中,并将u和v合并到同一个连通分量中。 4. 重复步骤3,直到最小生成树中的边数达到n-1。 通过以上步骤,就可以使用Kruskal算法求解出给定图的最小生成树。 参考资料: 引用:(1)度限制最小生成树和第K最短路. (poj1639) (2)最短路,最小生成树,二分图,最大流问题的相关理论(主要是模型建立和求解) (poj3155, poj2112,poj1966,poj3281,poj1087,poj2289,poj3216,poj2446 (3)最优比率生成树. (poj2728) (4)最小树形图(poj3164) (5)次小生成树. (6)无向图、有向图的最小环 。 引用:http://home.ustc.edu.cn/~zhuhcheng/ACM/segment_tree.pdf 。 引用:练习复杂一点,但也较常用的算法。 二分图匹配(匈牙利),最小路径覆盖 网络流,最小费用流。 线段树. 并查集。 熟悉动态规划的各个典型:LCS、最长递增子串、三角剖分、记忆化dp 6.博弈类算法。博弈树,二进制法等。 7.最大团,最大独立集。 8.判断点在多边形内。 差分约束系统. 双向广度搜索、A*算法,最小耗散优先. 第三阶段: 。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值