UVa 10369:arctic Network (to minimize the K-side of the spanning tree)

Source: Internet
Author: User
Tags bool contains

Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=1310

Topic:

Problem c:arctic Network
The Department of National Defence (DND) wishes to connect several northern from outposts by a wireless. Two different communication technologies are to is used in establishing the Network:every outpost would have a radio trans Ceiver and some outposts would 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. Due to purchasing and maintenance considerations, the transceivers at the outposts must is identical; That's, 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.

The number of input contains N, the number of test cases. The "a" of each test case contains 1 <= s <=, the number of satellite channels, and S < P <=, t He number of outposts. P lines follow, giving the x,y coordinates of each outpost in km (coordinates are integers between 0 and 10,000). For each case, the output should consist of the "a single" giving the minimum D required to connect the network. Output should is specified to 2 decimal points.
Sample Input

1
2 4
0 100
0 300
0 600
150 750

Sample Output

212.13

The main effect of the topic:

There are n scientific stations in Antarctica to connect these stations with satellites or radios so that any two can be directly or indirectly connected. Any two of them are equipped with satellite equipment and can be directly passed through satellite communications, no matter how far away they are. and two stations with radio equipment installed, the distance must not exceed D. D The longer the cost the more.

Now there is a satellite device that can be installed, and there are enough radios to find a solution that will cost d the least (d) depending on the most expensive route for radio communications.

Analysis and Summary:

It is natural to think of a minimum spanning tree, and then, to make the D cost the least, the one with the longest path will be used to install the satellite. S communication satellite can install the longest path of the S-1 bar. Then, the path length of the P-s large in the minimum spanning tree is d.

Just ask for all the path lengths on the minimum spanning tree, arrange them, and get the answers.

Code:

1.Kruskal

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define N 505 US  
ing namespace std;  
Double x[n], y[n], ans[n*n];   
int n,m,f[n*n],rank[n*n];  
    struct edge{int u,v;  
    Double Val;  
    friend bool operator< (const Edge&a,const edge&b) {return a.val<b.val;  
    
}}arr[n*n];  
Inline double getdist (double x1,double y1,double x2,double y2) {return sqrt (x1-x2) * (X1-X2) + (y1-y2) * (Y1-y2));  
} inline void init () {for (int i=0; i<n*n; ++i) f[i]=i,rank[i]=0;  
    int find (int x) {int i,j=x;  
    while (J!=f[j]) j=f[j];  
    while (X!=J) {i=f[x]; f[x]=j; x=i;  
} return J;  
    BOOL Union (int x, int y) {int a=find (x), B=find (y);  
    if (a==b) return false;  
    if (rank[a]>rank[b]) f[b]=a;  
        else{if (rank[a]==rank[b]) ++rank[b];  
    F[a]=b;  
return true; int main() {int T;  
    scanf ("%d", &t);  
        while (t--) {scanf ("%d%d", &m,&n);  
        Init ();  
        for (int i=1; i<=n; ++i) scanf ("%lf%lf", &x[i],&y[i));  
        int pos=0; for (int i=1; i<=n; ++i) {for (int j=i+1; j<=n; ++j) if (i!=j) {arr[pos].u=i, arr[pos].v  
                =j;  
            Arr[pos++].val = Getdist (X[i],y[i],x[j],y[j]);  
        } sort (Arr,arr+pos);  
        int k=0;  
            for (int i=0; i<pos; ++i) {if (Union (ARR[I].U,ARR[I].V)) {ans[k++] = Arr[i].val;  
    } printf ("%.2f\n", ans[k-m]);  
return 0; }

This column more highlights: http://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.