USACO Building Roads

Luo Gu P2872 [USACO07DEC] road construction Building Roads

Luo Gu Portal

JDOJ 2546: USACO 2007 Dec Silver 2.Building Roads

JDOJ Portal

Description

Farmer John had just acquired several new farms! He wants to connect
the farms with roads so that he can travel from any farm to any
other farm via a sequence of roads; roads already connect some of
the farms.

Each of the N (1 <= N <= 1,000) farms (conveniently numbered 1..N)
is represented by a position (X_i, Y_i) on the plane (0 <= X_i <=
1,000,000; 0 <= Y_i <= 1,000,000). Given the preexisting M roads
(1 <= M <= 1,000) as pairs of connected farms, help Farmer John
determine the smallest length of additional roads he must build to
connect all his farms.

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: Two space-separated integers: X_i and Y_i

* Lines N+2..N+M+2: Two space-separated integers: i and j, indicating
that there is already a road connecting the farm i and farm j.

Output

* Line 1: Smallest length of additional roads required to connect all
farms, printed without rounding to two decimal places. Be sure
to calculate distances as 64-bit floating point numbers.

Sample Input

4 1 1 1 3 1 2 3 4 3 1 4

Sample Output

4.00

HINT

INPUT DETAILS:

Four farms at locations (1,1), (3,1), (2,3), and (4,3). Farms 1 and 4 are
connected by a road.

OUTPUT DETAILS:

Connect farms 1 and 2 with a road that is 2.00 units long, then connect
farms 3 and 4 with a road that is 2.00 units long. This is the best we can
do, and gives us a total of 4.00 unit lengths.

Source

2007~2008

Title Translation:

Farmer John recently got some new farm, he wanted to repair some of the new road so that all may pass his farm existing or new road end to end interoperability of some of the roads (that is, either from a farm can go through reach all the rest of the farm). There were originally connected to the road between some farms. All N (1 <= N <= 1,000) farms (with sequentially numbered 1..N) are represented as a map of coordinates (X_i, Y_i) points (0 <= X_i <= 1,000,000; 0 <= y_i <= 1,000,000), a length of road between two farms nature is representative of the distance between the points thereof. Now Farmer John also told the original M (1 <= M <= 1,000) between your farm road which connected the two farms, he hopes you do the math, in order to make all farm connectivity, he needs to build roads The minimum total length is.

answer:

A minimum spanning tree problem.

This question is to the effect that, there are some side has been built, and asked you to build long side of the picture can have a minimum spanning tree.

If we are in accordance with the first meaning of the questions even side would be too disgusting.

Therefore, we consider a better approach (in fact more clever

We give the title while playing tag, and finally build the whole map, if the marked side we put it in the right side set to 0, thus ensuring it must at the minimum spanning tree.

So can the A.

(I use the KUSKAL)

Code:

#include<cstdio>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
int n,m,vis[1001],tot,fa[1001];
long double ans;
struct node
{
    ll x,y;
}a[1001];
struct edge
{
    int u,v;
    long double val;
}e[1000001<<1];
long double dist(int x,int y,int a,int b)
{
    return sqrt((long double)(x-a)*(long double)(x-a)+(long double)(y-b)*(long double)(y-b));
}
void add(int x,int y,int z)
{
    e[++tot].u=x;
    e[tot].v=y;
    e[tot].val=z;
}
bool cmp(edge a,edge b)
{
    if(a.val==b.val)
        return a.u<b.u;
    return a.val<b.val;
}
int find(int x)
{
    if(fa[x]==x)
        return x;
    return fa[x]=find(fa[x]);
}
void unionn(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
        fa[fx]=fy;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&a[i].x,&a[i].y);
    for(int i=1;i<=n;i++)
        fa[i]=i;
    for(int i=1;i<=m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        vis[u]=vis[v]=1;
    }
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
        {
            if(vis[i]==1 && vis[j]==1)
            {
                add(i,j,0);
                add(j,i,0);
            }
            else
            {
                add(i,j,dist(a[i].x,a[i].y,a[j].x,a[j].y));
                add(j,i,dist(a[i].x,a[i].y,a[j].x,a[j].y));
            }
        }
    sort(e+1,e+tot+1,cmp);
    for(int i=1;i<=tot;i++)
        if(find(e[i].u)!=find(e[i].v))
        {
            ans+=e[i].val;
            unionn(e[i].u,e[i].v);
        }
    printf("%.2lf",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11331396.html