首页 技术 正文
技术 2022年11月15日
0 收藏 548 点赞 4,436 浏览 3865 个字

Transfer water

Time Limit:3000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 4009

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 301 3 22 4 11 22 1 20 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|. 题目大意:首先给你n,X,Y,Z表示有n个房子,自建水井需要X*海拔(纵坐标)的花费,从高海拔或者等高海拔处引水,需要Y*曼哈顿距离(|x1-x2|+|y1-y2|+|z1-z2|)。如果从低海拔引水过来,需要Y*曼哈顿距离+Y(水泵价格)的花费。问你让每个房子都能用水的最小花费是多少。如果有房子不能用水,输出”poor XiaoA“。解题思路:由于可以自建水井,所以不存在不能用水的情况。对于引水的我们可以直接建立有向边,那么对于自建水井的我们应该怎么处理呢?自环?当然不能这样写。我们可以构造一个起点,跟所有点都连边,边的权值表示自建水井的花费。然后跑朱刘算法,就能得到结果。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef long long INT;
const int maxn = 1100;
const int INF = 0x3f3f3f3f;
struct Coor{
int x,y,z;
}coors[maxn];
struct Edge{
int from,to;
int dist;
}edges[maxn*maxn];
int pre[maxn],vis[maxn],ID[maxn];
int In[maxn];
int ansidx ;
int distan(Coor a,Coor b){
return abs(a.x-b.x)+abs(a.y-b.y)+abs(a.z-b.z);
}
INT Zhuliu(int root,int n,int m){
INT ret = 0;
int u,v;
while(true){
for(int i = 0; i < n; i++){
In[i] = INF;
}
for(int i = 0; i < m; i++){
Edge &e = edges[i];
u = e.from; v = e.to;
if(In[v] > e.dist && u != v){
pre[v] = u;
if(u == root){
ansidx = i;
}
In[v] = e.dist;
}
}
for(int i = 0; i < n; i++){
if(i == root) continue;
if(In[i] == INF)
return -1;
}
In[root] = 0;
int cntcir = 0;
memset(vis,-1,sizeof(vis));
memset(ID,-1,sizeof(ID));
for(int i = 0; i < n; i++){
ret += In[i];
v = i;
while(vis[v]!= i && ID[v] ==-1 &&v != root){
vis[v] = i;
v = pre[v];
}
if(v != root && ID[v] == -1){
for(u = pre[v]; u != v; u = pre[u]){
ID[u] = cntcir;
}
ID[v] = cntcir++;
}
}
if(cntcir == 0){
break;
}
for(int i = 0; i < n; i++){
if(ID[i]==-1){
ID[i] = cntcir++;
}
}
for(int i = 0; i < m; i++){
v = edges[i].to;
Edge & e = edges[i];
e.from = ID[e.from];
e.to = ID[e.to];
if(e.from != e.to){
e.dist -= In[v];
}
}
n = cntcir;
root = ID[root];
}
return ret;
}
int main(){
int n, m, k, T, cas = 0, X,Y,Z;
while(scanf("%d%d%d%d",&n,&X,&Y,&Z)!=EOF&&(n+X+Y+Z)){
int a,b,c;
for(int i = 1; i <= n; i++){
scanf("%d%d%d",&coors[i].x,&coors[i].y,&coors[i].z);
}
int m = 0;
for(int i = 1; i <= n; i++){
scanf("%d",&k);
for(int j = 1; j <= k; j++){
scanf("%d",&b);
edges[m].from = i;
edges[m].to = b;
if(coors[i].z >= coors[b].z){
edges[m++].dist = distan(coors[i],coors[b]) * Y;
}else{
edges[m++].dist = distan(coors[i],coors[b]) * Y + Z;
}
}
}
for(int i = 1; i <= n; i++){
edges[m].from = 0;
edges[m].to = i;
edges[m++].dist = coors[i].z * X;
}
INT res = Zhuliu(0,n+1,m);
printf("%lld\n",res);
}
return 0;
}

  


    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,071
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,549
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,397
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,174
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,809
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,889