Highways
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18191 | Accepted: 8455 |
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.
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.
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
Kruskal算法:
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int MAX = 501;
int n,m;
int u[MAX*MAX],v[MAX*MAX],w[MAX*MAX],r[MAX*MAX];
int p[MAX];
int cmp(const int x,const int y) {return w[x]<w[y];}
int find(int x) {return (p[x] == x)?x:p[x] = find(p[x]);} //并查集的find
int main()
{
int k,i,j,ans,buf;
int num; //选取的边的数目
cin>>k;
for(;k != 0;k--)
{
cin>>n;
m = 0; //边的数目
ans = -1;
num = 0;
for(i = 0;i < n;i++) //读取边
for(j = 0;j < n;j++)
{
if(i <= j) {cin>>buf;continue;} //i == j不读
u[m] = i;
v[m] = j;
r[m] = m;
cin>>w[m++];
}
for(i = 0;i < n;i++) p[i] = i; //初始化并查集
sort(r,r+m,cmp);
for(i = 0;i < m;i++)
{
if(num == n-1) break; //最小生成树n-1条边
int e = r[i];
int x = find(u[e]);
int y = find(v[e]);
if(x != y)
{
num++;
if(ans < w[e]) ans = w[e];
p[x] = y;
}
}
cout<<ans<<endl;
}
return 0;
}
Prim算法:
#include<stdio.h>
#include<iostream>
using namespace std;
const int MAX = 501;
#define INF 0x7fffffff
int map[MAX][MAX]; //邻接矩阵
int n; //结点数
int lowcost[MAX]; //从选中结点集合到相邻边最短距离
int vis[MAX]; //标志结点有无被选中
int main()
{
int i,j,pos,min,ans,k;
scanf("%d",&k);
for(;k > 0;k--)
{
scanf("%d",&n);
memset(vis,0,sizeof(vis));
for(i = 0;i < n;i++)
for(j = 0;j < n;j++)
{
scanf("%d",&map[i][j]);
}
vis[0] = 1; pos = 0; //默认从第一个结点开始
for(i = 1;i < n;i++) lowcost[i] = map[pos][i]; //初始化最小权值
ans = -1;
for(j = 1;j < n;j++) //再找出来n-1个点
{
min = INF;
for(i = 1;i < n;i++)
if(!vis[i] && min > lowcost[i])
{
min = lowcost[i];
pos = i;
}
vis[pos] = 1;
if(ans < min) ans = min;
for(i = 1;i < n;i++) //刷新最小权值
if(!vis[i] && lowcost[i] > map[pos][i])
{
lowcost[i] = map[pos][i];
}
}
printf("%d\n",ans);
}
return 0;
}