Highways
Time Limit:1000MS |
|
Memory Limit:65536K |
Total Submissions:15145 |
|
Accepted:7050 |
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
题目大意: 找出 最小生成树的最大边。
#include<stdio.h>
#include<string.h>
const int MAX=70000;
int map[505][505],vir[505];
int prim(int n);
int main()
{
//freopen("in.txt","r",stdin);
int t,n,i,j;
scanf("%d",&t);
while(t--)
{
memset(vir,0,sizeof(vir));
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&map[i][j]);
printf("%d\n",prim(n));
}
return 0;
}
int prim(int n)
{
int count=0,min,max=0,i,j,temp=n,next;
int p[505];
p[0]=0;
vir[0]=1;
while(--temp)
{
min=MAX;
for(i=0;i<=count;i++)
{
for(j=0;j<n;j++)
{
if(!vir[j] && map[p[i]][j]<min)
{
min=map[p[i]][j];
next=j;
}
}
}
vir[next]=1;
p[++count]=next;
if(min>max) max=min;
// printf("%d %d %d\n",count , next, max);
}
return max;
}
分享到:
相关推荐
北大POJ2485-Highways【Prim】 解题报告+AC代码
poj 2485 Highways 测试数据 解题报告:http://blog.csdn.net/qq7366020/article/details/8615293
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 ...
POJ3211--Washing Clothes
POJ水题集-----50道左右-----增加自信啊..
北大POJ1258-Agri-Net【Prim】 解题报告+AC代码
POJ 1038--Bugs Integrated
POJ3036--Honeycomb Walk
POJ---1456.Supermarket测试数据及答案,题目描述:A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral ...
北大POJ初级题-数据结构:解题报告+AC代码
POJ3259--Wormholes,使用bellman方法。
poj题目分类--acmer做题极有用资源
poj 1690 Your-Term-Project.md
poj 1240 Pre-Post-erous!.md
北大POJ3414-Pots 解题报告+AC代码
poj 2827 Auto-Calculation Machine.md
北大POJ2002-Squares 解题报告+AC代码
北大POJ3253-POJ3253-Fence Repair【STL优先队列】 解题报告+AC代码
NULL 博文链接:https://128kj.iteye.com/blog/1705139
用Java代码实现POJ(PKU)上题2494!