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);
    }
    
  • 相关阅读:
    Visula Studio 2013 初始化静态浮点型数据在C++类内
    catkin_make与gtest出现冲突的问题与解决
    用Visual studio2012在Windows8上开发内核驱动监视线程创建
    用Visual studio2012在Windows8上开发内核驱动监视进程创建
    TEA加密算法的C/C++实现
    说说某游戏保护驱动中驱动黑名单的具体实现
    [Windows驱动开发](四)内存管理
    [Windows驱动开发](三)基础知识——驱动例程
    [Windows驱动开发](二)基础知识——数据结构
    [Windows驱动开发](一)序言
  • 原文地址:https://www.cnblogs.com/suika/p/9147735.html
Copyright © 2011-2022 走看看