Highways
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18607 | Accepted: 8627 |
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
Hint
Huge input,scanf is recommended.
Source
POJ Contest,Author:Mathematica@ZSU
题意:
依旧是最小生成树
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<string>
#define MAX 502
#define INF 0x3f3f3f3f
int dist[MAX];
int map[MAX][MAX];
bool vist[MAX];
int n;
void init()
{
memset(map,INF,sizeof(map));
memset(dist,INF,sizeof(dist));
memset(vist,false,sizeof(vist));
}
int Prim()
{
int s=1;
vist[1]=true;
int m=1;
int max=-1;
while(true)
{
if(m==n) break;
int min=INF;
int flag=0;
for(int i=2;i<=n;i++)
{
if(!vist[i] && dist[i]>map[s][i])
dist[i]=map[s][i];
if(!vist[i] && min>dist[i])
{
flag=i;
min=dist[i];
}
}
if(max<min) max=min;
vist[flag]=true;
s=flag;
m++;
}
return max;
}
int main()
{
int i,j,t;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
init();
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&map[i][j]);
int k=Prim();
printf("%d\n",k);
}
}
return 0;
}
思路:
Prim。。但改之前的代码总是过不了 可能是有什么问题吧。。
以后就按照这个写咯
另:POJ 2421跟这道题基本一样
将已确定的边权值赋为0即可
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<string>
#define MAX 502
#define INF 0x3f3f3f3f
int dist[MAX];
int map[MAX][MAX];
bool vist[MAX];
int n;
void init()
{
memset(map,INF,sizeof(map));
memset(dist,INF,sizeof(dist));
memset(vist,false,sizeof(vist));
}
int Prim()
{
int s=1;
vist[1]=true;
int m=1;
int sum=0;
while(true)
{
if(m==n) break;
int min=INF;
int flag=0;
for(int i=2;i<=n;i++)
{
if(!vist[i] && dist[i]>map[s][i])
dist[i]=map[s][i];
if(!vist[i] && min>dist[i])
{
flag=i;
min=dist[i];
}
}
sum+=min;
vist[flag]=true;
s=flag;
m++;
}
return sum;
}
int main()
{
int i,j,t;
while(scanf("%d",&n)!=EOF)
{
init();
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&map[i][j]);
int q,a,b;
scanf("%d",&q);
for(int i=0;i<q;i++)
{
scanf("%d%d",&a,&b);
map[a][b]=map[b][a]=0;
}
int k=Prim();
printf("%d\n",k);
}
return 0;
}