• BZOJ_3479_[Usaco2014 Mar]Watering the Fields_Prim


    BZOJ_3479_[Usaco2014 Mar]Watering the Fields_Prim

    Description

     Due to a lack of rain, Farmer John wants to build an irrigation system to send water between his N fields (1 <= N <= 2000). Each field i is described by a distinct point (xi, yi) in the 2D plane, with 0 <= xi, yi <= 1000. The cost of building a water pipe between two fields i and j is equal to the squared Euclidean distance between them: (xi - xj)^2 + (yi - yj)^2 FJ would like to build a minimum-cost system of pipes so that all of his fields are linked together -- so that water in any field can follow a sequence of pipes to reach any other field. Unfortunately, the contractor who is helping FJ install his irrigation system refuses to install any pipe unless its cost (squared Euclidean length) is at least C (1 <= C <= 1,000,000). Please help FJ compute the minimum amount he will need pay to connect all his fields with a network of pipes.

    草坪上有N个水龙头,位于(xi,yi)

    求将n个水龙头连通的最小费用。
    任意两个水龙头可以修剪水管,费用为欧几里得距离的平方。 修水管的人只愿意修费用大于等于c的水管。

    Input

    * Line 1: The integers N and C.

    * Lines 2..1+N: Line i+1 contains the integers xi and yi.

    Output

    * Line 1: The minimum cost of a network of pipes connecting the fields, or -1 if no such network can be built. 

    Sample Input

    3 11
    0 2
    5 0
    4 3

    INPUT DETAILS: There are 3 fields, at locations (0,2), (5,0), and (4,3). The contractor will only install pipes of cost at least 11.

    Sample Output

    46


    裸的一个最小生成树,只不过是欧几里得距离的。。不会优化。

    于是学了下prim。

    基本思路就是每次拿出一个dis最小的然后更新其他所有点的dis。

    时间复杂度$O(n^2)$

    代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define N 2050
    int xx[N],yy[N],n,C,vis[N],dis[N];
    int di(int i,int j) {
        return (xx[i]-xx[j])*(xx[i]-xx[j])+(yy[i]-yy[j])*(yy[i]-yy[j]);
    }
    int main() {
        scanf("%d%d",&n,&C);
        int i,j;
        for(i=1;i<=n;i++) {
            scanf("%d%d",&xx[i],&yy[i]);
        }
        memset(dis,0x3f,sizeof(dis));
        int x=1; vis[1]=1;
        long long ans=0;
        for(i=1;i<n;i++) {
            int nxt=0;
            for(j=1;j<=n;j++) {
                if(!vis[j]) {
                    int tmp=di(x,j);
                    if(tmp>=C) dis[j]=min(dis[j],tmp);
                    if(!nxt) nxt=j;
                    else if(dis[j]<dis[nxt]) nxt=j;
                }
            }
            if(dis[nxt]>2000000) {
                puts("-1"); return 0;
            }
            x=nxt; ans+=dis[x]; vis[x]=1;
        }
        printf("%lld
    ",ans);
    }
    
  • 相关阅读:
    git add . 的时候遇到warning: LF will be replaced by CRLF inXXX 解决办法
    用Merge存储引擎中间件实现MySQL分表
    隐型马尔科夫模型(HMM) 简介
    隐型马尔科夫模型(HMM)向前算法实例讲解(暴力求解+代码实现)---盒子模型
    数据输入——生成你需要的echart图(世界地图,气泡图)
    数据输入——生成你需要的echart图(堆积柱状图、扇形图、嵌套环形图)
    jython实现java运行python代码
    django第四课 标签的用法(if/else、for、ifequal、过滤器、注释等)
    django第三课 模版
    paddlepaddle使用(一)
  • 原文地址:https://www.cnblogs.com/suika/p/9147735.html
Copyright © 2020-2023  润新知