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. Data Base System.Data.OracleClient requires Oracle client software version 8.1.7 or greater解决方案

    System.Data.OracleClient requires Oracle client software version 8.1.7 or greater解决方案 一.问题: 1.通过Syst ...

  2. [CQOI2012][bzoj2668] 交换棋子 [费用流]

    题面 传送门 思路 抖机灵 一开始看到这题我以为是棋盘模型-_-|| 然而现实是骨感的 后来我尝试使用插头dp来交换,然后又惨死 最后我不得不把目光转向那个总能化腐朽为神奇的算法:网络流 思维 我们要 ...

  3. django 基础框架学习 (一)

    Django-01 Web框架 1.Web应⽤程序处理流程  : 2.Web框架的意义            1.⽤于搭建Web应⽤程序            2.免去不同Web应⽤相同代码部分的重复 ...

  4. 前端开发快速定位bug的一些小技巧

    1,根据报错信息定位: (1) Uncaught TypeError: Cannot read property 'attr' of undefined; 此类型为变量或者对象属性未定义类型. (2) ...

  5. luogu1891 疯狂lcm ??欧拉反演?

    link 给定正整数N,求LCM(1,N)+LCM(2,N)+...+LCM(N,N). 多组询问,1≤T≤300000,1≤N≤1000000 \(\sum_{i=1}^nlcm(i,n)\) \( ...

  6. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  7. 论文阅读笔记五十六:(ExtremeNet)Bottom-up Object Detection by Grouping Extreme and Center Points(CVPR2019)

    论文原址:https://arxiv.org/abs/1901.08043 github: https://github.com/xingyizhou/ExtremeNet 摘要 本文利用一个关键点检 ...

  8. Hibernate常见报错

    1.A different object with the same identifier value was already associated with the session(使用Hibern ...

  9. chkconfig命令详细介绍

    命令介绍 chkconfig命令用来更新.查询.修改不同运行级上的系统服务.比如安装了httpd服务,并且把启动的脚本放在了/etc/rc.d/init.d目录下,有时候需要开机自动启动它,而有时候则 ...

  10. Luogu P4095 [HEOI2013]Eden 的新背包问题 思维/动规

    当时一直在想前缀和...多亏张队提醒... 从1到n背次包,保存每一个状态下的价值,就是不要把第一维压掉:再从n到1背一次,同样记住每种状态: 然后询问时相当于是max(前缀+后缀),当然前缀后缀中间 ...