您的位置:首页 > 其它

POJ2485--Highways

2013-04-27 14:37 417 查看
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
/*
首先当然是我最擅长的Kruskal算法咯
必须压缩路径+按秩合并
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define maxn 508
int father[maxn];
int rank1[maxn];
int find(int x)
{
if(x == father[x]) return x;
return father[x] = find(father[x]);
}
void Union(int aa,int bb)
{
if(rank1[aa] > rank1[bb])
{
father[bb] = aa;
}
else if(rank1[aa] == rank1[bb])
{
father[bb] = aa;
rank1[aa]++;
}
else
{
father[aa] = bb;
}
}
struct Edge
{
int from,to,key;
}edge[maxn*maxn/2];
bool cmp(Edge a,Edge b)
{
return a.key < b.key;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
memset(rank1,0,sizeof(rank1));
for(int i=1;i<=n;i++) father[i] = i;
int k = 1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
int cost;
scanf("%d",&cost);
if(i > j)
{
edge[k].from = i;
edge[k].to = j;
edge[k++].key = cost;
}
}
}
sort(edge+1,edge+k,cmp);
int ans;
for(int i=1;i<k;i++)
{
int uu = find(edge[i].from);
int vv = find(edge[i].to);
if(uu != vv)
{
Union(uu,vv);
ans = edge[i].key;
}
}
printf("%d\n",ans);
}
return 0;
}


 
/*
其实此题的正解赶脚应该是Prim
因为边数太多。
我比较稀饭优先队列优化的。帅一点
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define maxn 508
bool vis[maxn];
int dis[maxn],n;
struct Edge
{
int v,w;
bool operator < (const Edge & a) const
{
return w > a.w;
}
}edge;
vector <Edge> ans[maxn];
inline int max(int a,int b)
{
return a > b?a:b;
}
int heapprim()
{
priority_queue <Edge> q;
int nown = 0;
edge.v = 1;
edge.w = 0;
q.push(edge);
int cost = 0;
while(nown < n && !q.empty())
{
L:
edge = q.top();
q.pop();
while(vis[edge.v] && !q.empty()) goto L;
if(!vis[edge.v])
{
int nowv = edge.v;
vis[edge.v] = 1;
nown++;
cost = max(cost,edge.w);
for(int i=0;i<ans[nowv].size();i++)
{
if(!vis[ans[nowv][i].v] && dis[ans[nowv][i].v] > ans[nowv][i].w)
{
dis[ans[nowv][i].v] = ans[nowv][i].w;
edge.v = ans[nowv][i].v;
edge.w = ans[nowv][i].w;
q.push(edge);
}
}
}
}
return cost;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(vis,0,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
for(int i=1;i<=n;i++) ans[i].clear();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
int cost;
scanf("%d",&cost);
edge.w = cost;
edge.v = i;
ans[j].push_back(edge);
}
}
printf("%d\n",heapprim());
}
return 0;
}


 
/*
接下来是不优化的Prim算法
*/
#include <iostream>
#include <cstdio>
using namespace std;
#define maxn 508
#define inf 0x3f3f3f3f
int W[maxn][maxn];
int dis[maxn];
bool vis[maxn];
inline int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
memset(vis,0,sizeof(vis));
memset(W,0x3f,sizeof(W));
memset(dis,0x3f,sizeof(dis));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
int cost;
scanf("%d",&cost);
W[i][j] = W[j][i] = cost;
}
}
dis[1] = 0;
int cost = 0;
for(int i=1;i<=n;i++)
{
int minlen = inf;
int nowv;
for(int j=1;j<=n;j++)
{
if(!vis[j] && dis[j] < minlen)
{
minlen = dis[j];
nowv = j;
}
}
vis[nowv] = 1;
cost = max(cost,dis[nowv]);
for(int j=1;j<=n;j++)
{
if(!vis[j] && dis[j] > W[nowv][j])
{
dis[j] = W[nowv][j];
}
}
}
printf("%d\n",cost);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POJ2485 Highways