您的位置:首页 > 其它

PKU 2485 Highways

2010-08-14 16:52 302 查看
Highways

Time Limit:
1000MS
Memory Limit:
65536K
Total Submissions:
9544
Accepted:
4482
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
这是一道最小生成树的题目;恩……发现最小生成树的题目都比较模板化,基本上都是套的,还是不会用prime算法,不过k还是很好使的说;

题目是要求最小生成树的最大边,很显然是k算法。只需判断是不是最后一条边就好了。

]#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
int s;
int e;
int dis;
}highway[250000];
int p[505];
int rank[505];
void make_set(int x)
{
p[x]=x;
rank[x]=0;
}
int find_set(int x)
{
if(p[x]!=x)
p[x]=find_set(p[x]);
return p[x];
}
void Union(int x,int y)
{
x=find_set(x);
y=find_set(y);
if(rank[x]>rank[y])
{
p[y]=x;
}
else
{
p[x]=y;
if(rank[x]==rank[y])
rank[y]++;
}
}
bool cmp(node a,node b)
{
return a.dis<b.dis;
}
int main()
{
int n;
cin>>n;
int i,j;
while(n--)
{
getchar();
int t;
cin>>t;
int A=0;
for(i=0;i<t;i++)
{
for(j=0;j<t;j++)
{
if(j>i)
{
highway[A].s=i;
highway[A].e=j;
scanf("%d",&highway[A].dis);
A++;
}
else
{
int a;
scanf("%d",&a);
}
}
}
for(i=0;i<t;i++)
{
make_set(i);
}
sort(highway,highway+A,cmp);
//for(i=0;i<A;i++)
//{
// cout<<highway[i].dis<<' ';
//}
//cout<<endl;
for(i=0;i<A;i++)
{
if(find_set(highway[i].s)!=find_set(highway[i].e))
{
Union(highway[i].s,highway[i].e);
//cout<<t<<A<<endl;
if(t==2)
cout<<highway[i].dis<<endl;
t--;
}

}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: