luogu2872 [USACO07DEC]道路建设Building Roads

http://www.elijahqi.win/archives/1052
题目描述

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 (Xi, Yi) on the plane (0 ≤ Xi ≤ 1,000,000; 0 ≤ Yi ≤ 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.

给出nn个点的坐标,其中一些点已经连通,现在要把所有点连通,求修路的最小长度.

输入输出格式

输入格式:

Line 1: Two space-separated integers: N and M
Lines 2..N+1: Two space-separated integers: Xi and Yi
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.
输出格式:

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.
输入输出样例

输入样例#1:

4 1
1 1
3 1
2 3
4 3
1 4
输出样例#1:

4.00
这题本来写的是prim 因为大约n^2复杂度嘛 然后因为 写完之后发现把点都打上标记走不过去了,答案错误
于是使用kruscal(prim也可以做
我把相连的两边的权值改成了0这样建立生成树的时候一定存在我这样的边
偷偷看了下zhx的代码 发现直接在读入的时候给他们先都扔进同一个并查集就好了

#include<cstdio>
#include<cmath>
#include<algorithm>
#define N 1100
#define ll long long
using namespace std;
inline int read(){
    int x=0;char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
double ans;
int x[N],y[N],n,m,fa[N],num,h[N];
struct node{
    int x,y,next;ll z;
}data[2*N*N];
inline ll calc(int st,int to){
    return (ll)(x[st]-x[to])*(x[st]-x[to])+(ll)(y[st]-y[to])*(y[st]-y[to]);
}
inline bool cmp(node a,node b){return a.z<b.z;}
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int main(){
    freopen("2872.in","r",stdin);
    n=read();m=read();
    for (int i=1;i<=n;++i) x[i]=read(),y[i]=read();
    for (int i=1;i<=n;++i)
        for (int j=i+1;j<=n;++j) {
            data[++num].x=i;data[num].next=h[i];data[num].y=j,data[num].z=calc(i,j),h[i]=num;
            data[++num].x=j;data[num].next=h[j];data[num].y=i,data[num].z=data[num-1].z,h[j]=num;
        }
    for (int i=1;i<=m;++i){
        int xx=read(),yy=read();
        for (int j=h[xx];j;j=data[j].next)if(data[j].y==yy) data[j].z=0;
    }
    sort(data+1,data+num+1,cmp);for (int i=1;i<=n;++i) fa[i]=i;int cnt=0;
    for (int i=1;i<=num;++i){
        int xx=data[i].x,yy=data[i].y;
        int st=find(xx),to=find(yy);
        if (st!=to) {
            ans+=sqrt(data[i].z),fa[st]=to;
            if (++cnt==n-1) break;
        }
    }
    printf("%.2lf",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值