http://poj.org/problem?id=2485
Highways
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16024 | Accepted: 7449 |
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
题意:给定n个点,然后分别给定这几个点的距离。求链接这几个点的最小生成树的最大边。。。
第一次用prime()给个模版
http://www.nocow.cn/index.php/Prim算法
把模版改下结果就出来了。。。
#include<stdio.h>
#include<string.h>
#define MAXN 1100
int lowcost[MAXN],closest[MAXN],cost[MAXN][MAXN],maxn;
int n;
#define UPPERDIS 0x3F3F3F3F
//int maxn= -1000000;//这边WA了3次
void prim()
{
maxn=0;//这边WA了3次啊啊啊啊
int i,j,mindis,minone;
int ans = 0;
for(i = 0;i < n;i++)
{
lowcost[i] = cost[0][i];
closest[i] = 0;
}
for(i = 0;i < n-1;i++)
{
mindis = UPPERDIS;
for(j = 0;j < n;j++)
{
if(lowcost[j] && mindis > lowcost[j])
{
mindis = lowcost[j];
minone = j;
}
}
maxn=(maxn>mindis?maxn:mindis);
// ans += lowcost[minone];//需要的话还可以求最小生成树
lowcost[minone] = 0;
for(j = 0;j < n;j++)
{
if(cost[j][minone] < lowcost[j])
{
lowcost[j] = cost[j][minone];
closest[j] = minone;
}
}
}
// return maxn;
}
int main()
{
int t,i,j;
while(scanf("%d",&t)!=EOF)
{
int count=t;
//printf("\n");
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
}
prim();
printf("%d\n",maxn);
}
}
return 0;
}
#include<string.h>
#define MAXN 1100
int lowcost[MAXN],closest[MAXN],cost[MAXN][MAXN],maxn;
int n;
#define UPPERDIS
//int maxn= -1000000;//这边WA了3次
void prim()
{
}
int main()
{
}
按照维基修改的。。里面的东西还需理解
#include<stdio.h>
#include<string.h>
#define MAXN 1100
int lowcost[MAXN],closest[MAXN],cost[MAXN][MAXN],maxn;
int n;
#define INF 0x3F3F3F3F
//int maxn= -1000000;//这边WA了3次
void prim()
{
maxn=0;
int i,j,min,k;
int ans = 0;
for(i = 0;i < n;i++)
{
lowcost[i] = cost[0][i];
closest[i] = 0;
}
for(i = 0;i < n-1;i++)
{
min = INF;
for(j = 0;j < n;j++)
{
if(lowcost[j] && min > lowcost[j])
{
min = lowcost[j];
k = j;
}
}
maxn=(maxn>min?maxn:min);//最小生成树的最大边的长度
// ans += lowcost[k];//需要的话还可以求最小生成树
lowcost[k] = 0;
for(j = 0;j < n;j++)
{
if(cost[k][j] < lowcost[j])
{
lowcost[j] = cost[k][j];
closest[j] = k;
}
}
}
// return maxn;
}
int main()
{
int t,i,j;
while(scanf("%d",&t)!=EOF)
{
int count=t;
//printf("\n");
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
}
prim();
printf("%d\n",maxn);
}
}
return 0;
}
#include<string.h>
#define MAXN 1100
int lowcost[MAXN],closest[MAXN],cost[MAXN][MAXN],maxn;
int n;
#define INF
//int maxn= -1000000;//这边WA了3次
void prim()
{
}
int main()
{
}
2012.9.5在poj1789的基础上稍微改下出入方式就行了()还一个地方在代码中有标出
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 2100
char s[MAXN][10];
int map[MAXN][10];
int vis[MAXN];
int lowcost[MAXN],cost[MAXN][MAXN];
int n;
#define inf 0x3f3f3f3f
int prim()
{
int max=-10000;
memset(vis,0,sizeof(vis));
//int ans=0;
for(int i=0;i<n;i++)
{
lowcost[i]=cost[0][i];
}
vis[0]=1;
int min;
int k;
for(int i=0;i<n-1;i++)
{
min=inf;
for(int j=0;j<n;j++)
{
if(!vis[j]&&lowcost[j]<min)
{
min=lowcost[j];
k=j;
}
}
max=max>lowcost[k]?max:lowcost[k];//就是这个地方。。求最长边
//ans+=lowcost[k];//求整个最小生成树的边的和
vis[k]=1;
for(int j=0;j<n;j++)
{
if(cost[j][k]<lowcost[j]&&vis[j]==0)
{
lowcost[j]=cost[j][k];
}
}
}
return max;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,j;
memset(cost,0,sizeof(cost));
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&cost[i][j]);
}
}
printf("%d\n",prim());
}
return 0;
}
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 2100
char s[MAXN][10];
int map[MAXN][10];
int vis[MAXN];
int lowcost[MAXN],cost[MAXN][MAXN];
int n;
#define inf 0x3f3f3f3f
int prim()
{
}
int main()
{
}