最小生成树MST POJ-2485

来源:互联网 发布:腾讯数据分析招聘 编辑:程序博客网 时间:2024/06/15 03:22
Highways
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 18191 Accepted: 8455

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

Kruskal算法:

#include<iostream>#include<algorithm>#include<stdio.h>using namespace std;const int MAX = 501;int n,m;int u[MAX*MAX],v[MAX*MAX],w[MAX*MAX],r[MAX*MAX];int p[MAX];int cmp(const int x,const int y) {return w[x]<w[y];}int find(int x) {return (p[x] == x)?x:p[x] = find(p[x]);}//并查集的findint main(){int k,i,j,ans,buf;int num;//选取的边的数目cin>>k;for(;k != 0;k--){cin>>n;m = 0;//边的数目ans = -1;num = 0;for(i = 0;i < n;i++)//读取边for(j = 0;j < n;j++){if(i <= j) {cin>>buf;continue;}//i == j不读u[m] = i;v[m] = j;r[m] = m;cin>>w[m++];}for(i = 0;i < n;i++) p[i] = i;//初始化并查集sort(r,r+m,cmp);for(i = 0;i < m;i++){if(num == n-1) break;//最小生成树n-1条边int e = r[i];int x = find(u[e]);int y = find(v[e]);if(x != y){num++;if(ans < w[e]) ans = w[e];p[x] = y;}}cout<<ans<<endl;}return 0;}

Prim算法:

#include<stdio.h>#include<iostream>using namespace std;const int MAX = 501;#define INF 0x7fffffffint map[MAX][MAX];//邻接矩阵int n;//结点数int lowcost[MAX];//从选中结点集合到相邻边最短距离int vis[MAX];//标志结点有无被选中int main(){int i,j,pos,min,ans,k;scanf("%d",&k);for(;k > 0;k--){scanf("%d",&n);memset(vis,0,sizeof(vis));for(i = 0;i < n;i++)for(j = 0;j < n;j++){scanf("%d",&map[i][j]);}vis[0] = 1; pos = 0;//默认从第一个结点开始for(i = 1;i < n;i++) lowcost[i] = map[pos][i];//初始化最小权值ans = -1;for(j = 1;j < n;j++)//再找出来n-1个点{min = INF;for(i = 1;i < n;i++)if(!vis[i] && min > lowcost[i]){min = lowcost[i];pos = i;}vis[pos] = 1;if(ans < min) ans = min;for(i = 1;i < n;i++)//刷新最小权值if(!vis[i] && lowcost[i] > map[pos][i]){lowcost[i] = map[pos][i];}}printf("%d\n",ans);}return 0;}