HDOJ 4009 Transfer water 最小树形图
Transfer water
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 4216 Accepted Submission(s): 1499
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.
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.
2 10 20 30
1 3 2
2 4 1
1 2
2 1 2
0 0 0 0
30HintIn 3‐dimensional space Manhattan distance of point A (x1, y1, z1) and B(x2, y2, z2) is |x2‐x1|+|y2‐y1|+|z2‐z1|.
pid=4008" target="_blank" style="color:rgb(26,92,200); text-decoration:none">4008
/* ***********************************************
Author :CKboss
Created Time :2015年07月06日 星期一 09时23分30秒
File Name :HDOJ4009.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; const int maxn=1200;
const int INF=0x3f3f3f3f; int n,X,Y,Z; struct POS
{
int a,b,c;
}pos[maxn]; struct Edge
{
int u,v,cost;
}edge[maxn*maxn]; int en;
int pre[maxn],id[maxn],vis[maxn],in[maxn]; void init() { en=0; } int zhuliu(int root,int n,int m,Edge edge[])
{
int res=0,v;
while(true)
{
for(int i=0;i<n;i++) in[i]=INF;
for(int i=0;i<m;i++)
{
if(edge[i].u!=edge[i].v&&edge[i].cost<in[edge[i].v])
{
pre[edge[i].v]=edge[i].u;
in[edge[i].v]=edge[i].cost;
}
}
for(int i=0;i<n;i++)
{
if(i!=root&&in[i]==INF) return -1;
}
int tn=0;
memset(id,-1,sizeof(id));
memset(vis,-1,sizeof(vis));
in[root]=0;
for(int i=0;i<n;i++)
{
res+=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(int u=pre[v];u!=v;u=pre[u])
id[u]=tn;
id[v]=tn++;
}
}
if(tn==0) break;
for(int i=0;i<n;i++)
if(id[i]==-1) id[i]=tn++;
for(int i=0;i<m;)
{
v=edge[i].v;
edge[i].u=id[edge[i].u];
edge[i].v=id[edge[i].v];
if(edge[i].u!=edge[i].v)
edge[i++].cost-=in[v];
else
swap(edge[i],edge[--m]);
}
n=tn;
root=id[root];
}
return res;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); while(scanf("%d%d%d%d",&n,&X,&Y,&Z)!=EOF)
{
if(n==0&&X==0&&Y==0&&Z==0) break; init(); for(int i=1,x,y,z;i<=n;i++)
{
scanf("%d%d%d",&x,&y,&z);
pos[i]=(POS){x,y,z};
} /// root 0 is water
for(int i=1;i<=n;i++)
{
int hight = pos[i].c;
edge[en++]=(Edge){0,i,hight*X};
} for(int i=1,m;i<=n;i++)
{
int to,from=i;
scanf("%d",&m);
for(int j=0;j<m;j++)
{
scanf("%d",&to);
if(from==to) continue; int dist = abs(pos[to].a-pos[from].a)+abs(pos[to].b-pos[from].b)+abs(pos[to].c-pos[from].c);
int h_to = pos[to].c;
int h_from = pos[from].c; if(h_from>=h_to)
{
edge[en++]=(Edge){from,to,dist*Y};
}
else
{
edge[en++]=(Edge){from,to,dist*Y+Z};
}
}
} /// zhuliu
int lens = zhuliu(0,n+1,en,edge);
if(lens==-1) puts("poor XiaoA");
else printf("%d\n",lens);
} return 0;
}
HDOJ 4009 Transfer water 最小树形图的更多相关文章
- HDU 4009 Transfer water 最小树形图
分析:建一个远点,往每个点连建井的价值(单向边),其它输水线按照题意建单向边 然后以源点为根的权值最小的有向树就是答案,套最小树形图模板 #include <iostream> #incl ...
- HDU4009 Transfer water —— 最小树形图 + 不定根 + 超级点
题目链接:https://vjudge.net/problem/HDU-4009 Transfer water Time Limit: 5000/3000 MS (Java/Others) Me ...
- hdu4009 Transfer water 最小树形图
每一户人家水的来源有两种打井和从别家接水,每户人家都可能向外输送水. 打井和接水两种的付出代价都接边.设一个超级源点,每家每户打井的代价就是从该点(0)到该户人家(1~n)的边的权值.接水有两种可能, ...
- HDU 4009——Transfer water——————【最小树形图、不定根】
Transfer water Time Limit:3000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64u Subm ...
- HDU 4009 Transfer water(最小树形图)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意:给出一个村庄(x,y,z).每个村庄可以挖井或者修建水渠从其他村庄得到水.挖井有一个代价, ...
- HDU - 4009 - Transfer water 朱刘算法 +建立虚拟节点
HDU - 4009:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意: 有n户人家住在山上,现在每户人家(x,y,z)都要解决供水的问题,他可以自己 ...
- hdu 4009 Transfer water(最小型树图)
Transfer water Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)To ...
- hdu 2121 , hdu 4009 无定根最小树形图
hdu 2121 题目:给出m条有向路,根不确定,求一棵最小的有向生成树. 分析:增加一个虚拟节点,连向n个节点,费用为inf(至少比sigma(cost_edge)大).以该虚拟节点为根求一遍最小树 ...
- HDU 4009 Transfer water
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意:给出一个村庄(x,y,z).每个村庄可以挖井或者修建水渠从其他村庄得到水.挖井有一个代价, ...
随机推荐
- axios的坑
1.axios默认发送application/json 格式 https://www.cnblogs.com/qdcnbj/p/8143155.html 资料: https://www.npmjs.c ...
- ES6学习笔记(二十)Module 的加载实现
上一章介绍了模块的语法,本章介绍如何在浏览器和 Node 之中加载 ES6 模块,以及实际开发中经常遇到的一些问题(比如循环加载). 1.浏览器加载 传统方法 HTML 网页中,浏览器通过<sc ...
- js获取当前根目录的方法
function getRootPath_web() { //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp var curWwwP ...
- JSON.stringify(),JSON.parse(),eval(string)
JSON.stringify()用于从一个对象解析出字符串 : var obj = {"name":"week","age":" ...
- [POJ2823][洛谷P1886]滑动窗口 Sliding Window
题目大意:有一列数,和一个窗口,一次能框连续的s个数,初始时窗口在左端,不断往右移动,移到最右端为止,求每次被框住的s个数中的最小数和最大数. 解题思路:这道题是一道区间查询问题,可以用线段树做.每个 ...
- Linux CentOs6.5误卸载自带python和yum后的解决办法
事故背景:前几天因项目需要,在服务器上搭建python-mysql模块,结果没安装好,于是乎想卸载重装,遂在网上查询卸载python的方法,结果一不小心直接把系统的python删了个干净....... ...
- 关于buffer和cache的区别
1. Cache:缓存区,是高速缓存,是位于CPU和主内存之间的容量较小但速度很快的存储器,因为CPU的速度远远高于主内存的速度,CPU从内存中读取数据需等待很长的时间,而 Cache保存着CPU刚 ...
- Java 二进制,八进制,十进制,十六进制转换
A.十进制转换其他 十进制转成二进制 Integer.toBinaryString(int i) 十进制转成八进制 Integer.toOctalString(int i) 十进制转成十六进制: ...
- 小米净水器与小区过滤价格水对照.xls
总结:要是一天用水量为7升下面.还是用小区的过滤水为好,合算. 假设过滤水需求量大,可能小米的净水器比較好.当然,小区的要天天去接.要求风雨无阻的. 这点小米的随用随接就更好. 注意一点,小米的还要用 ...
- 自己封装js组件 - 中级中高级
接着做关于alert组件的笔记 怎么又出来个中高级呢 对没错 就是出一个中高级来刷流量呵呵呵,但是中高级也不是白叫的 这次主要是增加了widget类,增加了自己绑定的事件和触发事件的方法!这么做是为什 ...