题目来源:http://poj.org/problem?id=2485
问题描述
Highways
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 36622 | Accepted: 16378 |
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
------------------------------------------------------------
题意
给定一个图,求一个支撑树,使得支撑树中最长的边最短。输出该边的长度。
------------------------------------------------------------
思路
题意所要求的边就是最小支撑树的最长边。证明如下:
假设图G存在一个支撑树T’, T’的所有边的长度均小于G的最小支撑树T的最长边(u,v). 存在一个割集C使得(u,v) = C∩T. 因为T’是G的支撑树,所以T’∩C不是空集,记T’∩C=(x,y), (x,y) < (u,v). 那么将T中的(u,v)替换为(x,y)得到T’’, T’’仍是支撑树且|T’’|<|T|, 与T是G的最小支撑树矛盾。
最小支撑树可以用Prim算法或Kruskal算法求解。本文采用Prim算法。
------------------------------------------------------------
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
const int NMAX = 505, INF = 0x3f3f3f3f;
int mat[NMAX][NMAX];
int d[NMAX]; // 每个点到当前树的距离
int vis[NMAX]; // 是否在当前树中
int n;
int prime() // prime算法求最小支撑树,注意:返回最小支撑树所用的最长路
{
int i, j, ret = 0, minv, minid;
vis[0] = 1;
minid = 0;
for (j=0; j<n-1; j++)
{
for (i=0; i<n; i++)
{
if (!vis[i] && mat[minid][i])
{
d[i] = std::min(d[i], mat[minid][i]);
}
}
minv = INF; minid = -1;
for (i=0; i<n; i++)
{
if (!vis[i] && d[i] < minv)
{
minv = d[i];
minid = i;
}
}
vis[minid] = 1;
ret = std::max(minv, ret);
}
return ret;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("2485.txt", "r", stdin);
#endif
int t, i, j, ans;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
memset(mat, 0, sizeof(mat));
memset(d, 0x3f, sizeof(d));
memset(vis, 0, sizeof(vis));
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
scanf("%d", &mat[i][j]);
mat[j][i] = mat[i][j];
}
}
ans = prime();
printf("%d\n", ans);
}
return 0;
}