• POJ 2349 -- Arctic Network


    Arctic Network
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 20177   Accepted: 6247

    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

    1
    2 4
    0 100
    0 300
    0 600
    150 750
    

    Sample Output

    212.13
    

    Source

     
     
    思路就是最小生成树。这道题其实用prim比较方便,但是由于本人对kruskal的爱坚不可摧,于是硬着头皮用kruskal写完了整道题。并因此浪费了一个上午( ̄ε(# ̄)。
    对于写kruskal的同学们,以下是几个需要注意的点:
    1. i在整个程序中不要多次用,否则会提前退出;
    2. 算三角形斜边(即两点间边权)时,要记得强行转double;
    3. 每一个新的数据前要记得初始化;
    4. 各种循环变量不要混。比如在j的循环里不要用成i。
     1 #include <iostream>
     2 #include <cmath>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <cstdlib>
     6 #include <algorithm>
     7 
     8 using namespace std;
     9 int p,s;
    10 int cnt;
    11 int i,j;
    12 int fa[505];
    13 struct data
    14 {
    15     int x,y;
    16 }a[505];
    17 
    18 struct edge
    19 {
    20     int st,ed;   //边两边的点 
    21     double length;  //边权 
    22     bool vis;  //记录是否访问过 
    23 }c[505*505];
    24 
    25 bool cmp(edge a,edge b) {return a.length < b.length;} 
    26 
    27 int find(int x) {return fa[x]==x ? x : find(fa[x]);}  //找根 
    28 
    29 double kruskal()
    30 {
    31     double ans=0;
    32     int s=1,t=1;  //这里的循环变量不要用i,j 
    33     for(s=1;s<=cnt && t<=p;s++)
    34     {
    35         int X=find(c[s].st),Y=find(c[s].ed);
    36         if(X!=Y)
    37         {
    38             if(X>Y) fa[X]=Y;
    39             else fa[Y]=X;
    40             ans+=c[s].length;
    41             c[s].vis=1;
    42             t++;
    43         }
    44     }
    45     return ans;
    46 }
    47 
    48 int main()
    49 {
    50     int n;
    51     scanf("%d",&n);
    52     for(i=1;i<=n;i++)  
    53     {
    54         scanf("%d%d",&s,&p);
    55         for(j=1;j<=p;j++)
    56             scanf("%d%d",&a[j].x,&a[j].y);
    57 
    58         cnt=1;
    59         for(j=1;j<p;j++)
    60         {
    61             for(int k=j+1;k<=p;k++)
    62             {
    63                 c[cnt].st=j;
    64                 c[cnt].ed=k;
    65                 c[cnt].length=sqrt(double((a[j].x-a[k].x)*(a[j].x-a[k].x)+(a[j].y-a[k].y)*(a[j].y-a[k].y)));
    66                 c[cnt].vis=0;
    67                 //printf("%.2lf ",c[cnt].length);
    68                 cnt++;
    69             }
    70         }
    71         
    72         for(j=1;j<=p;j++) fa[j]=j;
    73         sort(c+1,c+cnt+1,cmp);
    74         double sum=kruskal();
    75         
    76         for(j=cnt;j>=1;j--)
    77             if(c[j].vis)
    78             {
    79                 s--;
    80                 if(s==0) break;
    81             }
    82         printf("%.2f
    ",c[j].length);
    83     }
    84     //system("pause");
    85     return 0;
    86 }
    POJ2349
  • 相关阅读:
    unix网络编程 初步了解TCP/IP协议
    unix网络编程 常见概念
    linux 环境变量
    linux c编程
    第二周学习笔记
    jmeter第一周学习笔记
    建造者模式
    原型设计模式
    抽象工厂模式
    工厂方法模式
  • 原文地址:https://www.cnblogs.com/YXY-1211/p/7169961.html
Copyright © 2020-2023  润新知