Uva 10369 Arctic Network 最小生成树

来源:互联网 发布:linux source 编辑:程序博客网 时间:2024/05/18 05:10


题目大意:n个城市,s个卫星,有卫星的可以无限长度相连,其他城市间用无线电相连。问剩余城市间最大距离。

模型即最小生成树中n-s长边


#include <stdio.h>#include <string.h>#include <math.h>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;const int maxn = 1005;const int Mod = 1000000007;int s,n,m,c;double ans;struct Node{int x,y;}point[maxn];struct node{int u,v;double w;}edge[maxn*maxn];int p[maxn];bool cmp( node a,node b ){return a.w < b.w;}int find( int x ){return x == p[x]?x:p[x] = find( p[x] );}void merge( node s ){int x = find( s.u );int y = find( s.v );if( x != y ){p[x] = y;c ++;ans = s.w;}}int main(){#ifndef ONLINE_JUDGE   freopen("data.txt","r",stdin);   #endifint cas;scanf("%d",&cas);while( cas -- ){scanf("%d%d",&s,&n);ans = m = c = 0;for( int i = 1; i <= n; i ++ ){scanf("%d%d",&point[i].x,&point[i].y);}for( int i = 1; i <= n; i ++ ){for( int j = i+1; j <= n; j ++ ){edge[m].u = i;  edge[m].v = j;edge[m++].w = sqrt( (( point[i].x - point[j].x )*( point[i].x - point[j].x ) + ( point[i].y - point[j].y )*( point[i].y - point[j].y ))*1.0 );}}sort( edge,edge+m,cmp );for( int i = 0; i <= n; i ++ )p[i] = i;for( int i = 0; i < m; i ++ ){merge( edge[i] );if( c == n - s )break;}printf("%.2lf\n",ans);}return 0;}


0 0
原创粉丝点击