Highways
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 27781 |
|
Accepted: 12690 |
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
130 990 692990 0 179692 179 0
Sample Output
692
Hint Huge input,scanf is recommended.
題目意思: 有N個城市,用鄰接矩陣給出兩兩之間的距離。
求最小產生樹中的最長公路。
題目中給出的是一個鄰接矩陣。
解題思路: 最小產生樹裸題。
思想:從第1個頂點開始,添加與之有邊的點;再從這些已添加的邊中選擇最小權值的繼續添加。
用prim/kruskal模板改改就能A。
①prim
/** Copyright (c) 2016, 煙台大學電腦與控制工程學院* All rights reserved.* 檔案名稱:prim.cpp* 作 者:單昕昕* 完成日期:2016年6月1日* 版 本 號:v1.0*/#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>#define MAXN 1010#define INF 0xfffffff//0X代表16進位,後面是數字,十進位是4294967295using namespace std;int cost[MAXN][MAXN],dis[MAXN],mincost[MAXN],n,t;bool used[MAXN];//標識是否使用過void prim(){ fill(mincost,mincost+n,INF); fill(used,used+n,false); mincost[0]=0; int res=0,Max=-1; while(true) { int v=-1; for(int u=0; u<n; ++u) {//從不屬於已加入產生樹的頂點中選取從已加入產生樹的點到該頂點的權值最小的點 if(!used[u]&&(v==-1||mincost[u]<mincost[v])) v=u; } if(v==-1) break; used[v]=true; Max=max(Max,mincost[v]); //res+=mincost[v]; for(int u=0; u<n; ++u) mincost[u]=min(mincost[u],cost[v][u]); } cout<<Max<<endl;}int main(){ ios::sync_with_stdio(false); cin.tie(0); int t; cin>>t; while(t--) { cin>>n; /*for(int i=0; i<n; ++i) for(int j=0; j<n; ++j) cost[i][j]=INF;*/ for(int i=0; i<n; i++) for(int j=0; j<n; ++j) cin>>cost[i][j]; prim(); } return 0;}/*130 990 692990 0 179692 179 0*/
②kruskal
/** Copyright (c) 2016, 煙台大學電腦與控制工程學院* All rights reserved.* 檔案名稱:prim.cpp* 作 者:單昕昕* 完成日期:2016年3月30日* 版 本 號:v1.0*/#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>#define MAXN 10010#define INF 0xfffffff//0X代表16進位,後面是數字,十進位是4294967295using namespace std;int cost[MAXN][MAXN],dis[MAXN],pre[MAXN],n,cnt;bool used[MAXN];//標識是否使用過struct edge{ int u,v,cost;};bool cmp(const edge &e1,const edge &e2){ return e1.cost<e2.cost;}edge es[MAXN];int V,E;//頂點數和邊數int set_find(int x){ while(x!=pre[x]) x=pre[x]; return x;}void unite(int p,int q){ p=set_find(p); q=set_find(q); if(p!=q) pre[p]=q;}bool same(int x,int y){ return set_find(x)==set_find(y);}void init_union_find(int n){ for(int i=0; i<=n; i++) pre[i]=i;}void kruskal(){ sort(es,es+cnt,cmp);//按照edge.cost的順序升序排列 init_union_find(V);//並查集初始化 //int res=0; int Max=-1; for(int i=0; i<E; ++i) { edge e=es[i]; if(!same(e.u,e.v)) { unite(e.u,e.v); //res+=e.cost; Max=max(Max,e.cost); } } cout<<Max<<endl;}int main(){ ios::sync_with_stdio(false); cin.tie(0); int t; cin>>t; while(t--) { cin>>n; cnt=0; /*for(int i=0; i<n; ++i) for(int j=0; j<n; ++j) cost[i][j]=INF;*/ for(int i=0; i<n; i++) for(int j=0; j<n; ++j) { es[cnt].u=i; es[cnt].v=j; cin>>es[cnt++].cost; } V=n; E=n*n; kruskal(); } return 0;}/*130 990 692990 0 179692 179 0*/