zoukankan      html  css  js  c++  java
  • BZOJ3479: [Usaco2014 Mar]Watering the Fields

    3479: [Usaco2014 Mar]Watering the Fields

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 81  Solved: 42
    [Submit][Status]

    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
    OUTPUT DETAILS: FJ cannot build a pipe between the fields at (4,3) and (5,0), since its cost would be only 10. He therefore builds a pipe between (0,2) and (5,0) at cost 29, and a pipe between (0,2) and (4,3) at cost 17.

    HINT

     

    Source

    Silver 译文By Hta

     题解:

    呵呵,裸MST

    代码:

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<iostream>
     7 #include<vector>
     8 #include<map>
     9 #include<set>
    10 #include<queue>
    11 #include<string>
    12 #define inf 1000000000
    13 #define maxn 2500
    14 #define maxm 500+100
    15 #define eps 1e-10
    16 #define ll long long
    17 #define pa pair<int,int>
    18 #define for0(i,n) for(int i=0;i<=(n);i++)
    19 #define for1(i,n) for(int i=1;i<=(n);i++)
    20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
    21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
    22 #define mod 1000000007
    23 #define sqr(x) (x)*(x)
    24 using namespace std;
    25 inline int read()
    26 {
    27     int x=0,f=1;char ch=getchar();
    28     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    29     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    30     return x*f;
    31 }
    32 struct rec{int x,y;}a[maxn];
    33 struct edge{int x,y,w;}e[maxn*maxn];
    34 int n,k,ans,tot,fa[maxn];
    35 inline bool cmp(edge a,edge b)
    36 {
    37     return a.w<b.w;
    38 }
    39 inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
    40 int main()
    41 {
    42     freopen("input.txt","r",stdin);
    43     freopen("output.txt","w",stdout);
    44     n=read();k=read();
    45     for1(i,n)
    46      {
    47          a[i].x=read();a[i].y=read();
    48          for1(j,i-1)
    49           e[++tot].x=i,e[tot].y=j,e[tot].w=sqr(a[i].x-a[j].x)+sqr(a[i].y-a[j].y);
    50      }
    51     sort(e+1,e+tot+1,cmp);
    52     int j=1,i;
    53     while(e[j].w<k)j++;
    54     for1(i,n)fa[i]=i;
    55     for(i=1;i<n;i++)
    56     {
    57     while(j<=tot&&find(e[j].x)==find(e[j].y))j++;
    58     if(j>tot)break;
    59     fa[find(e[j].x)]=find(e[j].y);
    60     ans+=e[j].w;
    61     j++; 
    62     }
    63     if(i<n)printf("-1
    ");else printf("%d
    ",ans);
    64     return 0;
    65 }
    View Code
  • 相关阅读:
    Now Task
    数据库的死锁及解决
    Java小对象的解决之道——对象池(Object Pool)的设计与应用
    Java中的对象池技术
    npm install时报错 npm ERR!Windows_NT 6.1.7601
    Angular 入门学习
    React 之 Hello world
    react webpack.config.js 入门学习
    React/React Native 的ES5 ES6写法对照表
    ES5和ES6中对于继承的实现方法
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/4006746.html
Copyright © 2011-2022 走看看