zoukankan      html  css  js  c++  java
  • hdu 4009 Transfer water

    Problem Description
    XiaoA lives in a village. Last year flood rained the village. So they decide to move the whole village to the mountain nearby this year. There is no spring in the mountain, so each household could only dig a well or build a water line from other household. If the household decide to dig a well, the money for the well is the height of their house multiplies X dollar per meter. If the household decide to build a water line from other household, and if the height of which supply water is not lower than the one which get water, the money of one water line is the Manhattan distance of the two households multiplies Y dollar per meter. Or if the height of which supply water is lower than the one which get water, a water pump is needed except the water line. Z dollar should be paid for one water pump. In addition,therelation of the households must be considered. Some households may do not allow some other households build a water line from there house. Now given the 3‐dimensional position (a, b, c) of every household the c of which means height, can you calculate the minimal money the whole village need so that every household has water, or tell the leader if it can’t be done.
     
    Input
    Multiple cases. 
    First line of each case contains 4 integers n (1<=n<=1000), the number of the households, X (1<=X<=1000), Y (1<=Y<=1000), Z (1<=Z<=1000). 
    Each of the next n lines contains 3 integers a, b, c means the position of the i‐th households, none of them will exceeded 1000. 
    Then next n lines describe the relation between the households. The n+i+1‐th line describes the relation of the i‐th household. The line will begin with an integer k, and the next k integers are the household numbers that can build a water line from the i‐th household. 
    If n=X=Y=Z=0, the input ends, and no output for that. 
     
    Output
    One integer in one line for each case, the minimal money the whole village need so that every household has water. If the plan does not exist, print “poor XiaoA” in one line. 
     
    Sample Input
    2 10 20 30
    1 3 2
    2 4 1
    1 2
    2 1 2
    0 0 0 0
     
    Sample Output
    30
    Hint
    In 3‐dimensional space Manhattan distance of point A (x1, y1, z1) and B(x2, y2, z2) is |x2‐x1|+|y2‐y1|+|z2‐z1|.
     
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #define inf 0x3f3f3f3f
      6 using namespace std;
      7 const int maxm=1200000;
      8 const int maxn=1200;
      9 struct node
     10 {
     11     int x,y,z;
     12 };
     13 struct NN
     14 {
     15     int u,v,w;
     16 };
     17 NN res[maxm];
     18 int m;
     19 node dd[1010];
     20 int n,X,Y,Z;
     21 int resCost,cnt;
     22 bool ff;
     23 int pre[maxn],id[maxn],vis[maxn],in[maxn];
     24 
     25 void addEdge(int x,int y,int z)
     26 {
     27     res[m].u=x;
     28     res[m].v=y;
     29     res[m].w=z;
     30     m++;
     31 }
     32 
     33 int myabs(int a)
     34 {
     35     if(a<0)
     36     return -a;
     37     return a;
     38 }
     39 
     40 int directedMST(int root)
     41 {
     42     int ans=0,nv=n,i;
     43     while (1)
     44     {
     45         for (i=0;i<nv;i++)
     46         {
     47             in[i]=inf;
     48         }
     49         for (i=0;i<m;i++)
     50         {
     51             int u=res[i].u;
     52             int v=res[i].v;
     53             if (res[i].w<in[v]&&u!=v)
     54             {
     55                 pre[v]=u;
     56                 in[v]=res[i].w;
     57             }
     58         }
     59         for (i=0;i<nv;i++)
     60         {
     61             if (i==root)continue;
     62             if (in[i]==inf)return -1;
     63         }
     64         int cntnode=0;
     65         memset(id,-1,sizeof(id[0])*(nv+3));
     66         memset(vis,-1,sizeof(vis[0])*(nv+3));
     67         in[root]=0;
     68         for (i=0;i<nv;i++)
     69         {
     70             ans+=in[i];
     71             int v=i;
     72             while (vis[v]!=i&&id[v]==-1&&v!=root)
     73             {
     74                 vis[v]=i;
     75                 v=pre[v];
     76             }
     77             if (v!=root&&id[v]==-1)
     78             {
     79                 for (int u=pre[v];u!=v;u=pre[u])
     80                 {
     81                     id[u]=cntnode;
     82                 }
     83                 id[v]=cntnode++;
     84             }
     85         }
     86         if (cntnode==0)break;
     87         for (i=0;i<nv;i++)
     88         {
     89             if (id[i]==-1)id[i]=cntnode++;
     90         }
     91         for (i=0;i<m;i++)
     92         {
     93             int v=res[i].v;
     94             res[i].u=id[res[i].u];
     95             res[i].v=id[res[i].v];
     96             if (res[i].u!=res[i].v)
     97             {
     98                 res[i].w-=in[v];
     99             }
    100         }
    101         nv=cntnode;
    102         root=id[root];
    103     }
    104     return ans;
    105 }
    106 
    107 int main()
    108 {
    109     while(~scanf("%d%d%d%d",&n,&X,&Y,&Z))
    110     {
    111         if(n==0 && X==0 && Y==0 && Z==0)
    112         break;
    113         m=0;
    114         n++;
    115         for(int i=1;i<n;i++)
    116         {
    117             scanf("%d%d%d",&dd[i].x,&dd[i].y,&dd[i].z);
    118             addEdge(0,i,dd[i].z*X);
    119         }
    120         for(int i=1;i<n;i++)
    121         {
    122             int k;
    123             scanf("%d",&k);
    124             for(int j=0;j<k;j++)
    125             {
    126                 int x;
    127                 scanf("%d",&x);
    128                 if(x==i)
    129                 continue;
    130                 if(dd[i].z>=dd[x].z)
    131                 {
    132                     int cost=myabs(dd[i].x-dd[x].x)+myabs(dd[i].y-dd[x].y)+myabs(dd[i].z-dd[x].z);
    133                     addEdge(i,x,cost*Y);
    134                 }
    135                 else
    136                 {
    137                     int cost=myabs(dd[i].x-dd[x].x)+myabs(dd[i].y-dd[x].y)+myabs(dd[i].z-dd[x].z);
    138                     addEdge(i,x,cost*Y+Z);
    139                 }
    140             }
    141         }
    142         printf("%d
    ",directedMST(0));
    143     }
    144     return 0;
    145 }
    View Code

     有向图的最小生成树(最小树形图),更新模板,复杂度(O(nm))

  • 相关阅读:
    看从小自带BUFF的他,如何用代码降低万物互联的门槛
    30亿参数,华为云发布全球最大预训练模型,开启工业化AI开发新模式
    6大新品重磅发布,华为云全栈云原生技术能力持续创新升级
    ISO/IEC 5055:软件代码质量的标尺
    Python基础语法和数据类型最全总结
    40个问题让你快速掌握Java多线程的精髓
    编程实战:如何管理代码里的常量
    总是记不住java的IO流用法?用N个问题教你掌握java IO流
    4种语义分割数据集Cityscapes上SOTA方法总结
    PHP 在Swoole中使用双IoC容器实现无污染的依赖注入
  • 原文地址:https://www.cnblogs.com/ouyangduoduo/p/3312805.html
Copyright © 2011-2022 走看看