hdu 4009 Transfer water

本文介绍了一种解决有向图最小生成树问题的方法,即最小树形图问题,并给出了一段实现该算法的C++代码。问题背景为一个村庄搬迁至山上后,各户考虑打井或铺设水管获取水源,旨在找到最低成本方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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\n",directedMST(0));
143     }
144     return 0;
145 }
View Code

 

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

 

转载于:https://www.cnblogs.com/ouyangduoduo/p/3312805.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值