poj2349Arctic Network(最小產生樹kruscal+第k長的邊)

來源:互聯網
上載者:User

標籤:des   style   http   color   os   io   strong   for   

題目連結:啊哈哈,點我點我
題意:
北極的某地區共有n座村莊( 1 ≤ n ≤ 500 ),每座村莊的座標用一對整數(x, y)表示,其中 0 ≤ x, y ≤ 10000。為了加強聯絡,決定在村莊之間建立通訊網路。通訊工具可以是無線電收發機,也可以是衛星裝置。所有的村莊都可以擁有一部無線電收發機, 且所有的無線電收發機型號相同。但衛星裝置數量有限,只能給一部分村莊配備衛星裝置。  不同型號的無線電收發機有一個不同的參數d,兩座村莊之間的距離如果不超過d就可以用該型號的無線電收發機直接通訊,d值越大的型號價格越貴。擁有衛星裝置的兩座村莊無論相距多遠都可以直接通訊。
這道題目主要是那衛星不好處理,但是如果正面不好想,那麼可以從逆向來想,如果知道了最小距離D,那麼去掉最小產生樹裡面大於D的邊,那麼圖會存在k個連通支,這裡給一個定理:如果去掉所有權大於d的邊後最小產生樹被分割成k個連通分支,圖也被分割成k個連通分支,。那麼給每個連通支分一個衛星電話即可,那麼這個最短距離D就變成了去掉大於D後的最大的邊,即第k長的邊。。。。那麼這個問題就解決了。。。
題目:
Arctic Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10211   Accepted: 3374

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

Source

Waterloo local 2002.09.28
代碼:
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>using namespace std;const int maxn=500+10;int s,p,root[maxn],cal;double dis[maxn*maxn];struct Point{    int x,y;}point[maxn];struct Edge{    int u,v;    double distance;}edge[maxn*maxn];int findroot(int x){    if(root[x]!=x)        root[x]=findroot(root[x]);    return root[x];}double caldistance(Point a,Point b){    return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));}bool cmp(Edge a,Edge b){    return a.distance<b.distance;}bool cmp2(double a,double b){    return a>b;}void kruscal(){    memset(dis,0.0,sizeof(dis));    int sum=0,i,k=0;    for(i=1;i<=cal;i++)    {        int fx=findroot(edge[i].u);        int fy=findroot(edge[i].v);        if(fx==fy)            continue;        else        {            root[fx]=fy;            sum++;            dis[sum]=edge[i].distance;            if(sum==p-1)                break;        }    }    sort(dis+1,dis+1+sum,cmp2);    printf("%.2f\n",dis[s]);}void read_graph(){    for(int i=1;i<=p;i++)        root[i]=i;    for(int i=1;i<=p;i++)       scanf("%d%d",&point[i].x,&point[i].y);    for(int i=1;i<p;i++)       for(int j=i+1;j<=p;+j++)    {        double temp=caldistance(point[i],point[j]);        edge[++cal].u=i;        edge[cal].v=j;        edge[cal].distance=temp;    }    sort(edge+1,edge+1+cal,cmp);}int main(){    int t;    scanf("%d",&t);    while(t--)    {        cal=0;        scanf("%d%d",&s,&p);        read_graph();        kruscal();    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.