Highways
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 16037 | Accepted: 7460 |
题目链接:http://poj.org/problem?id=2485
Description
The island nation of Flatopiais perfectly flat. Unfortunately, Flatopia has no public highways. So thetraffic is difficult in Flatopia. The Flatopian government is aware of thisproblem. They're planning to build some highways so that it will be possible todrive between any pair of towns without leaving the highway system.
Flatopian towns are numbered from 1 to N. Each highway connects exactly twotowns. All highways follow straight lines. All highways can be used in bothdirections. Highways can freely cross each other, but a driver can only switchbetween 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 tobe built. However, they want to guarantee that every town is highway-reachablefrom every other town.
Input
The first line of input is aninteger T, which tells how many test cases followed.
The first line of each case is an integer N (3 <= N <= 500), which is thenumber 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 aninteger within [1, 65536]) between village i and village j. There is an emptyline after each test case.
Output
For each test case, you shouldoutput a line contains an integer, which is the length of the longest road tobe 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 isrecommended.
Source
POJ Contest,Author:Mathematica@ZSU
题意:
给你一张图,让你求最小生成树中最大的边
解题思路:
直接用prim算法来求解即可。
代码:
#include <iostream>
#include<cstdio>
#include<cstring>
#define MAX 503
#define VALUE 0xfffff
using namespace std;
int g[MAX][MAX];
char gra[MAX][7];
int minCost[MAX];
int visited[MAX];
int prim(int n)
{
int i;
for(i=0;i<n;i++)
{
visited[i]=0;
minCost[i]=VALUE;
}
minCost[0]=0;
// int res=0;
int max=0;
while(true)
{
int t=-1;
for(i=0;i<n;i++)
{
if(visited[i]==0 && (t==-1 || minCost[i]<minCost[t]))
{
t=i;
}
}
if(t==-1)
break;
visited[t]=1;
if(minCost[t]>max)
{
max=minCost[t];
}
// res+=minCost[t];
for(i=0;i<n;i++)
{
if(minCost[i]>g[i][t] && g[i][t]!=0)
{
minCost[i]=g[i][t];
}
}
}
return max;
}
int main()
{
int ts;
int i,j;
scanf("%d",&ts);
while(ts--)
{
memset(g,0,sizeof(g));
int n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&g[i][j]);
}
}
printf("%d\n",prim(n));
}
return 0;
}