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);
    }
    
  • 相关阅读:
    实体ip 虚拟ip 固定ip 动态ip
    mysql数据库性能调优总结积累
    Java项目相关监控与调优
    线程锁与避免线程锁 线程锁检测
    在Eclipse中使用JUnit4进行单元测试(高级篇)
    MySQL性能优化的21条最佳经验【转】
    在Eclipse中使用JUnit4进行单元测试(中级篇)
    在Eclipse中使用JUnit4进行单元测试(初级篇)
    Windbg学习使用
    性能测试积累总结
  • 原文地址:https://www.cnblogs.com/suika/p/9147735.html
Copyright © 2011-2022 走看看