POJ 2349 Arctic Network 最小生成树 prim && kruscal

来源:互联网 发布:python计算机视觉编程 编辑:程序博客网 时间:2024/05/22 14:37

Arctic Network
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 13208 Accepted: 4284

Description

The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel. 
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts. 

Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.

Input

The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000).

Output

For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.

Sample Input

12 40 1000 3000 600150 750

Sample Output

212.13
注-此题为:POJ 2349 poj 2349 Arctic Network

题意:
       有两种不同的通信技术,有卫星通信的两个城市之间可以任意联络,
但用无线电通信(只能一种规格)的城市只能和距离不超过D的城市联系。
无线电的能力越高(即传输距离D越大),花费就越大。已知无线电的数目m,让求最小的D。
思路:
       先求出每两个顶点之间的距离,(注意:是double类型的),
然后用 克鲁斯卡尔(kruscal)或普里姆算法(Prim) 求最小生成树。
由于求最小的D,故把构成最小生成树的边排序,删除m-1条边较长边后,
最大的边即所要求的最小D。

已AC代码:(prim

#include<cstdio>#include<cmath>#include<cstring>#define MAX 251000#define INF 0xfffffff#include<algorithm>using namespace std;int n,m,A;int x[600],y[600];double map[600][600],low[600];int vis[600];   //map二维数组存图,low记录每2个点间最小权值,vis标记某点是否已访问double g[600];double dlen(int i,int j){return sqrt( 1.0*((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])) );}int prim(){memset(vis,0,sizeof(vis));int i,j,pos=1,t=0;double min;   //从某点开始,分别标记vis和记录该点posfor(i=1;i<=n;++i)low[i]=map[1][i];  //第一次给low数组赋值 map的第一行 vis[1]=1;for(i=1;i<n;++i)    //再运行n-1次,一次找一个最小 {min=INF;for(j=1;j<=n;++j) {if(vis[j]==0&&min>low[j]){min=low[j];   // 找出最小值min,记录位置pospos=j;}}vis[pos]=1;  //标记该点已访问 g[t++]=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 t;}int main(){int T,i,j,t;scanf("%d",&T);while(T--){scanf("%d%d",&A,&n);for(i=1;i<=n;++i)for(j=1;j<=n;++j)map[i][j]=INF;for(i=1;i<=n;++i)scanf("%d%d",&x[i],&y[i]);double d;for(i=1;i<=n;++i){for(j=i+1;j<=n;++j){d=dlen(i,j);map[i][j]=map[j][i]=d;}}int t=prim();sort(g,g+t);  //对 g 排序 printf("%.2lf\n",g[t-A]);}return 0;}

已AC代码:(kruscal

#include<cstdio>#include<cmath>#include<algorithm>#define MAX 251000using namespace std;int n,A;int per[600];   // 并查集 int x[600],y[600];struct node{int u,v;double w;    //w为权值 }s[MAX];bool cmp(node a,node b){return a.w<b.w;}double dlen(int i,int j) //两点间距离 {return sqrt( 1.0*((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])) );}void itoa()   //初始化 {for(int i=0;i<=n;++i)per[i]=i;}int find(int x)     // 查找根节点 {return x==per[x]?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,i,j,t;scanf("%d",&T);while(T--){double d;scanf("%d%d",&A,&n);for(i=1;i<=n;++i)scanf("%d%d",&x[i],&y[i]);int k=0;for(i=1;i<n;++i)  //存入数据 {for(j=i+1;j<=n;++j){d=dlen(i,j);s[k].u=i;s[k].v=j;s[k].w=d;k++;}}itoa();  //初始化根节点 sort(s,s+k,cmp);   //按距离从小到大排序 double g[600];t=0;for(i=0;i<k;++i){if(join(s[i].u,s[i].v)){g[t++]=s[i].w;}}     //由于对 g 存放时已经排好序,直接输出所求 printf("%.2lf\n",g[t-A]);}return 0;}

0 0