Luo Gu -1991 wireless communication network

Title Description
Defense Ministry plans to connect several border posts with the wireless network. Two different techniques are used to build a wireless communication network;
each border post should be equipped with a radio transceiver; some posts may also increase with a satellite telephone.
Any two equipped with a satellite telephone line posts (both sides ᤕ satellite telephone) call can be, no matter how far apart they are. Only the distance between the call through the radio transceiver post can not exceed D, which is limited by the power of the transceiver. The higher the transceiver power, call distance D farther, but the price will be more expensive.
The transceiver requires a unified purchase and installation, so all the posts can only install one type of transceiver. In other words, the distance between each pair of the call are the same post D. Your task is to determine the minimum call transceiver must distance D, between each pair of posts such that at least a call path (direct or indirect).
Input Output Format
Input Format:
from wireless.in input data line 1, 2 and integer S P, S represents the number of posts may be mounted satellite phone, P represents the number of border post. Took over P lines of two integers x, y plane coordinates describe a post of the (x, y), in km.
Output Format:
Output wireless.out the
first line, a real number D, represents a minimum transmission distance of a radio transceiver, accurate to two decimal places.

Sample Input Output
Input Sample # 1:
2. 4
0 100
0 300
0 600
150 750

Output Sample # 1:
212.13

Description
for 20% of the data: P = 2, S = 1
data for the other 20%: P = 4, S = 2
to 100% of the data to ensure that: 1 ≤ S ≤ 100, S <P ≤ 500,0 ≤ x , y ≤ 10000.

Explanation: The first enumerates each point, calculate their distance as a side, and then ordering it from small to large. We dichotomous answer to the edge, while taking advantage of disjoint-set to maintain good relations Unicom, each group 1-mid contribution to a satellite phone, mid + 1 - t each point contribution to a satellite phone. Finally, you can compare s and a half

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define N 505
using namespace std;
struct node{
    double x,y;
};
node s[N];
int father[N*N]={0};
int H[N*N]={0};
int n=0,m=0;
struct edge{
    int a,b;
    double c;
};
edge E[N*N];
int t=0;
void init(){
    for(int i=0;i<N*N;i++) father[i]=i,H[i]=0;
}
int find(int x){
    if(father[x]==x) return x;
    return father[x]=find(father[x]);
}
void merge(int x,int y){
    x=find(x);y=find(y);
    father[x]=y;
}
bool cmp(edge &a,edge &b){
    return a.c<b.c;
}
bool same(int x,int y){
    return find(x)==find(y);
}
bool ok(int x){
    init();
    int ret=0;
    for(int i=1;i<=x;i++){
        if(!same(E[i].a,E[i].b)){
            H[E[i].a]=H[E[i].b]=1;
            merge(E[i].a,E[i].b);
        }
    }
    for(int i=1;i<=n;i++){
        if(!H[i]) ret++;
        if(find(i)!=i&&H[find(i)]==1) ret++;
        H[find(i)]=2;
    }
    return ret<=m;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>m>>n;
    for(int i=1;i<=n;i++) cin>>s[i].x>>s[i].y;
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            E[++t].a=i,E[t].b=j;
            E[t].c=(s[i].x-s[j].x)*(s[i].x-s[j].x)+(s[i].y-s[j].y)*(s[i].y-s[j].y);
        }
    }
    sort(E+1,E+1+t,cmp);
    int l=0,r=t;
    int mid=0;
    while(l<r){
        mid=(l+r)/2;
        if(ok(mid)) r=mid;
        else l=mid+1;
    }
    printf("%.2f\n",sqrt(E[l].c));
    return 0;
}

Guess you like

Origin blog.csdn.net/mkopvec/article/details/92617243