zoukankan      html  css  js  c++  java
  • POJ2349+prim

    最小生成树

     1 /*
     2 prim
     3 题意:给定一些点,一些卫星,一个卫星能连接两个点,点和点之间通信有一定的距离限制。
     4 问能使得所有的点联通的最小距离。
     5 */
     6 #include<stdio.h>
     7 #include<string.h>
     8 #include<stdlib.h>
     9 #include<algorithm>
    10 #include<iostream>
    11 #include<queue>
    12 #include<map>
    13 #include<stack>
    14 #include<set>
    15 #include<math.h>
    16 using namespace std;
    17 typedef long long int64;
    18 //typedef __int64 int64;
    19 typedef pair<int64,int64> PII;
    20 #define MP(a,b) make_pair((a),(b)) 
    21 const int maxn = 505;
    22 const int inf = 0x7fffffff;
    23 const double pi=acos(-1.0);
    24 const double eps = 1e-8;
    25 
    26 struct Node{
    27     double x,y;
    28 }a[ maxn ];
    29 int vis[ maxn ];
    30 double dis[ maxn ];
    31 double mat[ maxn ][ maxn ];
    32 
    33 double dist( Node a,Node b ){
    34     return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
    35 }
    36 
    37 void init( int n ){
    38     for( int i=1;i<=n;i++ ){
    39         for( int j=1;j<=n;j++ ){
    40             mat[i][j] = dist( a[i],a[j] );
    41         }
    42     }
    43     return ;
    44 } 
    45 
    46 void prim( int n ){
    47     for( int i=1;i<=n;i++ ){
    48         vis[ i ] = 0;
    49         dis[ i ] = mat[1][i];
    50     }
    51     vis[ 1 ] = 1;
    52     for( int i=1;i<=n;i++ ){
    53         int M = inf;
    54         int id = -1;
    55         for( int j=1;j<=n;j++ ){
    56             if( vis[ j ]==0&&M>dis[j] ){
    57                 M = dis[ j ];
    58                 id = j;
    59             }
    60         }
    61         if( id==-1 ) return ;
    62         vis[ id ] = 1;
    63         for( int j=1;j<=n;j++ ){
    64             if( vis[j]==0&&dis[j]>mat[ id ][ j ] ){
    65                 dis[ j ] = mat[ id ][ j ];
    66             }
    67         }
    68     }
    69     return ;
    70 }
    71 
    72 int main(){
    73     int T;
    74     scanf("%d",&T);
    75     while( T-- ){
    76         int s,n;
    77         scanf("%d%d",&s,&n);
    78         for( int i=1;i<=n;i++ )
    79             scanf("%lf%lf",&a[i].x,&a[i].y);
    80         init( n );
    81         prim( n );
    82         sort( dis+1,dis+1+n );
    83         //for( int i=1;i<=n;i++ )
    84         //    printf("%lf ",dis[ i ]);
    85         //puts("");
    86         printf("%.2lf
    ",dis[ n-s+1 ]);
    87     }
    88     return 0;
    89 }
    View Code
    keep moving...
  • 相关阅读:
    BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊(分块)
    BZOJ 2648 SJY摆棋子(KD Tree)
    Codeforces Round #441 D. Sorting the Coins(模拟)
    HDU 3400 Line belt (三分套三分)
    HDU 5919 Sequence II(主席树+区间不同数个数+区间第k小)
    HDU 5985 Lucky Coins(概率)
    HDU 5988 Coding Contest(浮点数费用流)
    HDU 5792 World is Exploding(树状数组+离散化)
    HDU 5791 Two(LCS求公共子序列个数)
    HDU 5787 K-wolf Number(数位dp)
  • 原文地址:https://www.cnblogs.com/xxx0624/p/3268181.html
Copyright © 2011-2022 走看看