zoukankan      html  css  js  c++  java
  • 185. [USACO Oct08] 挖水井

    185. [USACO Oct08] 挖水井

    ★★   输入文件:water.in   输出文件:water.out   简单对比
    时间限制:1 s   内存限制:128 MB

    农夫约翰决定给他的N(1<=N<=300)个牧场浇水,这些牧场被自然的命名为1..N。他可以给一个牧场引入水通过在这个牧场挖一口井或者修一条管道使这个牧场和一个已经有水的牧场连接。

    在牧场i挖一口井的花费是w_i(1<=w_i<=100000)。修建一条水管连接牧场i和牧场j的花费是p_ij(1<=p_ij<=100000;p_ij=p_ji;p_ii=0)。

    请确定农夫约翰为了完成浇灌所有的牧场所需的最小的总花费。

    题目名称:water

    输入格式:

    • 第1行:一个单独的整数n。
    • 第2..n+1行:第i+1行包含一个单独的整数w_i。
    • 第n+2..2n+1行:第n+1+i行包含n个用空可分开的整数;其中第j个数是p_ij。

    输入样例(file water.in):

    4
    5
    4
    4
    3
    0 2 2 2
    2 0 3 3
    2 3 0 4
    2 3 4 0
    

    输入说明:

    这里有4个牧场,修井和修管道的代价如图。

    输出格式:

    • 第1行:一个单独的整数,表示花费。

    输出样例(file water.out):

    9
    

    输出说明:

    农夫约翰可以在第4个牧场修井,并且将每个牧场和第一个连接起来,这样,花费是3+2+2+2=9。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 struct node
     7 {
     8     int u,v,money;
     9     bool operator < (const node &a)const
    10     {
    11         return money<a.money;
    12     }
    13 }edge[900009];
    14 int num=1;
    15 int father[100001];
    16 int find(int x) 
    17 {
    18     return (father[x]==x) ? x : father[x]=find(father[x]);
    19 }
    20 int main()
    21 {
    22     freopen("water.in","r",stdin);
    23     freopen("water.out","w",stdout);
    24     int n;
    25     scanf("%d",&n);
    26     for(int i=1;i<=n;i++)
    27     {
    28         scanf("%d",&edge[num].money);
    29         edge[num].u=i;
    30         edge[num].v=0;
    31         num++;
    32         edge[num].u=0;
    33         edge[num].v=i;
    34         edge[num].money=edge[num-1].money;
    35         num++;
    36         father[i]=i;
    37     }
    38     father[n+1]=father[n+1];int x;
    39     for(int i=1;i<=n;i++)
    40     for(int j=1;j<=n;j++)
    41     {
    42         scanf("%d",&x);
    43         if(i==j)
    44         continue;
    45         else 
    46         edge[num].u=i;
    47         edge[num].v=j;
    48         edge[num++].money=x;
    49     }
    50     sort(edge+1,edge+num);
    51     int ans=0;
    52     for(int i=1;i<num;i++)
    53     {
    54         int x=find(edge[i].u),y=find(edge[i].v);
    55         if(x!=y)
    56         {
    57             ans+=edge[i].money;
    58             father[x]=y;
    59         }
    60     }
    61     printf("%d",ans);
    62     return 0;
    63 }
  • 相关阅读:
    HashSet,TreeSet和LinkedHashSet
    HashMap结构及使用
    Elasticsearch-如何控制存储和索引文档(_source、_all、返回源文档的某些字段)
    Elasticsearch-数组和多字段
    Elasticsearch-布尔类型
    Elasticsearch-日期类型
    Elasticsearch-数值类型
    Elasticsearch-字符串类型
    Elasticsearch-使用映射来定义各种文档
    Elasticsearch-集群增加节点
  • 原文地址:https://www.cnblogs.com/sssy/p/6734329.html
Copyright © 2011-2022 走看看