Highways
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 25027 | Accepted: 11548 |
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注-此题为: POJ 2485 Highways
题意: 求最小生成树的最大边 Kruskal && Prim 模板
注意输出不要加空行 ,否则 PE
已AC代码:(Kruskal)
#include<cstdio>
#define MAX 250000
#include<algorithm>
using namespace std;
int n,m,per[600]; // 并查集
struct node{
int u,v,w; //w为距离
}s[MAX];
bool cmp(node a,node b)
{
return a.w<b.w;
}
void into() //初始化
{
for(int i=0;i<=n;++i)
per[i]=i;
}
int find(int x) // 查找根节点
{
return x==per[x]?x:per[x]=find(per[x]);
}
bool join(int a,int b) //合并根节点,并判断是否成环
{
int fa=find(a);
int fb=find(b);
if(fa!=fb)
{
per[fa]=fb;
return true;
}
return false;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int i,j,a,b,c,k,q;
scanf("%d",&n);
k=0;
for(i=1;i<=n;++i) //读入数据
{
for(j=1;j<=n;++j)
{
scanf("%d",&a);
if(i>j) //只读取一半 (i>j)的数据
{
s[k].u=i;
s[k].v=j;
s[k].w=a;
k++;
}
}
}
into(); //初始化根节点
sort(s,s+k,cmp); //按距离从小到大排序
int max=0;
for(i=0;i<k;++i)
{
if(join(s[i].u,s[i].v))
{
if(max<s[i].w) //最小生成树的最大边
max=s[i].w;
}
}
printf("%d\n",max);
}
return 0;
}
已AC代码:( Prim)
#include<cstdio>
#include<cstring>
#define INF 0xfffffff
int map[600][600],low[600];
int vis[600]; //map二维数组存图,low记录每2个点间最小权值,vis标记某点是否已访问
int n,MAX;
void prim()
{
int min;
int i,j,pos;
memset(vis,0,sizeof(vis));
vis[1]=1; low[1]=0;
for(i=2;i<=n;++i) //从某点开始,分别标记vis和记录该点pos
low[i]=map[1][i]; //第一次给low数组赋值 map的第一行
for(i=1;i<n;++i) //再运行n-1次,一次找一个最小
{
min=INF;
for(j=1;j<=n;++j) // 找出最小值min,记录位置pos
{
if(vis[j]==0&&low[j]<min)
{
min=low[j];
pos=j;
}
}
vis[pos]=1; //标记该点已访问
if(min>MAX) //最小生成树的最大边
MAX=min;
for(j=1;j<=n;++j) //更新权值low 把 map的 pos 行中比对应的 low 小的赋给low
if(vis[j]==0&&low[j]>map[pos][j])
low[j]=map[pos][j];
}
return ;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int i,j,a,b,c,k,q;
scanf("%d",&n);
memset(map,0,sizeof(map));
for(i=1;i<=n;++i) //建图
{
for(j=1;j<=n;++j)
{
scanf("%d",&map[i][j]);
}
}
MAX=0;
prim(); //最小生成树的最大边
printf("%d\n",MAX);
}
return 0;
}