zoukankan      html  css  js  c++  java
  • 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);
    }
    
  • 相关阅读:
    Java进阶7并发优化4——JDK并发数据结构
    Java 进阶7 并行优化 JDK多任务执行框架技术
    Java进阶7 并发优化2 并行程序设计模式
    Java 进阶7 并发优化 1 并行程序的设计模式
    Java 进阶6 异常处理的陷阱
    Algorithm3: 获得一个int数中二进制位为1 的个数
    Algorithm2: 重复查过半数的元素
    Algorithm1: 全排列
    Java进阶5 面向对象的陷阱
    Java进阶2 数组内存和对象的内存管理知识
  • 原文地址:https://www.cnblogs.com/suika/p/9147735.html
Copyright © 2011-2022 走看看