[Kuangbin take you to fly] minimum spanning six thematic H - Highways

H - Highways

Topic links: https://vjudge.net/contest/66965#problem/H

topic:

Flatopia completely flat island. Unfortunately, Flatopia public highway system is very bad. Vladimir Anatolia government aware of the problem, and has built a number of connecting some of the most important towns of the highway. However, there are still some towns can not be reached from the motorway. It is necessary to build more highways in order to be able to travel between any pair of cities and towns in without leaving the highway system.

    Flatopian town number from 1 to N, i is given by the position of the town Cartesian coordinates (xi, yi). Each highway connecting the two towns. All highways (original highway and freeway to be built) follow a straight line, their length is therefore equal to the Cartesian distance between towns. All highways can be used in both directions. Highway can freely cross each other, but the driver can switch between the two highways located at the end of the town highway.

    Flatopian government wants to minimize the cost of building new highways. However, they want to ensure that each town can be reached by road from other towns. Since Flatopia is so flat, cost is always proportional to the length of the highway. Therefore, the most expensive highway system would be to minimize the total length of the highway system.
Input
    Input consists of two parts. The first part describes all towns in the country, the second section describes all of the highway have been built.

    The first line of the input file contains an integer N (1 <= N <= 750), it represents the number of towns. The next two lines contains N integers, xi and yi are separated by spaces. These values give the coordinates of the i-th towns (for i from 1 to N). The absolute value of the coordinates does not exceed 10000. Every town has a unique position.

    The next line contains a single integer M (0 <= M <= 1000), number of the existing motorway. Next M lines each comprising a pair of integers separated by spaces. These two integers are given a number of cities and towns have been connected by a highway. Each pair connected by a highway town at least.
Yield
    Write one output line for each new highway to connect all cities and towns, as far as possible to reduce the total length of the new highway. Each highway should be demonstrated by printing a number of the highway connecting towns and separated by a space.

    If you do not need to build a new highway (all towns are connected), it should create an output file, but should be empty.
Sample input

    . 9
    . 1. 5
    0 0
    . 3 2
    . 4. 5
    . 5. 1
    0. 4
    . 5 2
    . 1 2
    . 5. 3
    . 3
    . 1. 3
    . 9. 7
    . 1 2

Sample Output

    . 1. 6
    . 3. 7
    . 4. 9
    . 5. 7
    . 8. 3

Ideas: first find the distance between two points saved up, and then given the M highway combined, and then through the entire town, the output can be different ancestors, attention subscripts of urban No. 1 starting from

 

//
// Created by hanyu on 2019/7/31.
//
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include<math.h>
using namespace std;
typedef long long ll;
const int maxn=2e7+7;
int father[maxn];
double x[maxn],y[maxn];
struct Node{
    int u,v;
    double w;
    bool operator<(const Node &other)const{
        return this->w<other.w;
    }
}node[maxn];
int find(int x)
{
    if(x==father[x])
        return x;
    return father[x]=find(father[x]);
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int i=1;i<=T;i++)
    {
        scanf("%lf%lf",&x[i],&y[i]);
    }
    for(int i=0;i<=T;i++)
        father[i]=i;
    int m=1;
    for(int i=1;i<=T-1;i++)
    {
        for(int j=i+1;j<=T;j++)
        {
            node[m].u=i;
            node[m].v=j;
            node[m++].w=sqrt(double(x[i]-x[j])*(x[i]-x[j])+double(y[i]-y[j])*(y[i]-y[j]));
        }
    }
    m--;
    sort(node+1,node+m+1);
    int n;
    scanf("%d",&n);
    int p,q;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&p,&q);
        int pp=find(p);
        int qq=find(q);
        if(pp==qq)
            continue;
        else
        {
           if(pp<qq)
            father[pp]=qq;
            else
                father[qq]=pp;
        }
    }
    for(int i=1;i<=m;i++)
    {
        int uu=find(node[i].u);
        int vv=find(node[i].v);
        if(uu==vv)
            continue;
        else
        {
            if(uu<vv)
               father[uu]=vv;
            else
                father[vv]=uu;
            printf("%d %d\n",node[i].u,node[i].v);
         
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Vampire6/p/11280129.html