Transfer water

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

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|. 

题目大意:首先给你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;
}

  


HDU 4009——Transfer water——————【最小树形图、不定根】的更多相关文章

  1. HDU 4009 Transfer water 最小树形图

    分析:建一个远点,往每个点连建井的价值(单向边),其它输水线按照题意建单向边 然后以源点为根的权值最小的有向树就是答案,套最小树形图模板 #include <iostream> #incl ...

  2. HDU4009 Transfer water —— 最小树形图 + 不定根 + 超级点

    题目链接:https://vjudge.net/problem/HDU-4009 Transfer water Time Limit: 5000/3000 MS (Java/Others)    Me ...

  3. HDOJ 4009 Transfer water 最小树形图

    Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) T ...

  4. HDU 4009 Transfer water(最小树形图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意:给出一个村庄(x,y,z).每个村庄可以挖井或者修建水渠从其他村庄得到水.挖井有一个代价, ...

  5. HDU - 4009 - Transfer water 朱刘算法 +建立虚拟节点

    HDU - 4009:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意: 有n户人家住在山上,现在每户人家(x,y,z)都要解决供水的问题,他可以自己 ...

  6. hdu 2121 , hdu 4009 无定根最小树形图

    hdu 2121 题目:给出m条有向路,根不确定,求一棵最小的有向生成树. 分析:增加一个虚拟节点,连向n个节点,费用为inf(至少比sigma(cost_edge)大).以该虚拟节点为根求一遍最小树 ...

  7. hdu 4009 Transfer water(最小型树图)

    Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)To ...

  8. hdu4009 Transfer water 最小树形图

    每一户人家水的来源有两种打井和从别家接水,每户人家都可能向外输送水. 打井和接水两种的付出代价都接边.设一个超级源点,每家每户打井的代价就是从该点(0)到该户人家(1~n)的边的权值.接水有两种可能, ...

  9. HDU 4009 Transfer water

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意:给出一个村庄(x,y,z).每个村庄可以挖井或者修建水渠从其他村庄得到水.挖井有一个代价, ...

随机推荐

  1. UINavigationController + UIScrollView组合,视图尺寸的设置探秘(二)

    承接上文,我想把view布局修改为如下模式,让ScrollView长在NavigationBar的下方,这总不会有遮挡的问题了吧: story board内容如下,主要是右侧视图蓝色区域添加了Scro ...

  2. 一道面试题关于js中添加动态属性

    js中数据类型包含基本数据类型和引用类型,基本类型包括:string.null.undefined.number.boolean.引用类型即是对象比如:array  .function以及自定义对象等 ...

  3. Java基础之开发工具Eclipse的使用

    Eclipse简介 Eclipse是由IBM公司投资4000万美元开发的集成开发工具.它是目前最流行的Java集成开发工具之一,基于Java语言编写,并且是开放源代码的.可扩展的(Integrated ...

  4. Python之‘数据结构’

    简介 数据结构基本上就是--它们是可以处理一些数据的结构.或者说,它们是用来存储一组相关数据的.在Python里面有三种内建的数据结构--列表.元组和字典. 一.列表 list是处理一组有序项目的数据 ...

  5. C#中抽象类与接口

    1抽象类 (1) 抽象方法只作声明,而不包含实现,可以看成是没有实现体的虚方法 (2) 抽象类不能被实例化 (3) 抽象类可以但不是必须有抽象属性和抽象方法,但是一旦有了抽象方法,就一定要把这个类声明 ...

  6. 老男孩Day2作业:购物车程序

    作业需求: 用户入口: 1.商品信息存在文件里 2.已购商品,余额记录.第一次启动程序时需要记录工资,第二次启动程序时谈出上次余额 3.允许用户根据商品编号购买商品 4.用户选择商品后,检测是否够,够 ...

  7. UVA11270 Tiling Dominoes

    \(\color{#0066ff}{ 题目描述 }\) 给定一个m×n的矩形网格,用1×2多米诺骨牌完全平铺. 请注意,即使一个平铺的旋转与另一个平铺相匹配,它们仍算作不同的平铺. 下面显示了一个平铺 ...

  8. P2723 丑数 Humble Numbers

    题意:给你k个质数,定义丑数集合为k个质数随机(1--k)个相乘得到的数 求第n小的丑数 暴力...貌似不太可行,(把所有大量丑数求出来,sort   QAQ) 可以想到,对于第i个丑数f[i],它一 ...

  9. CSS3圆角圆边 支持IE6-IE8浏览器

    CSS3圆角圆边样式,支持各大版本浏览器,包括支持IE6-IE9浏览器的圆边圆角. 本文我们主要是讲解如果用CSS 3样式表来实现圆角效果,值得注意的是,IE6/7/8这三个IE浏览器版本都不支持CS ...

  10. 模拟使用zookeeper实现master选举

    1.模拟选举机器类 package com.karat.cn.zookeeperAchieveLock.zkclient; import java.io.Serializable; /** * 选举的 ...