hdu 4009(最小树形图)
来源:互联网 发布:网络时间现在几点 编辑:程序博客网 时间:2024/06/06 00:15
Transfer water
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 482 Accepted Submission(s): 139
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.
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
30HintIn 3‐dimensional space Manhattan distance of point A (x1, y1, z1) and B(x2, y2, z2) is |x2‐x1|+|y2‐y1|+|z2‐z1|.
Source
The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest
Recommend
lcy
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4009
分析:这题认真看就会发现是最小树形图问题,之前poj 3164 这题的那个算法没错,hdu这题wa的原因是数组开小了,不过改一下居然TLE。。。于是各种Debug,本地生成大数据,发现光排序就超时了。。。囧,再接着发现是stl的sort不给力阿,自己写了个qsort,1046ms飘过。。。之间还加上了读入优化
求200++ms写法。。。
代码:
#include<cstdio>using namespace std;const int mm=2222222;const int mn=2222;struct edge{ int s,t,w;}g[mm];int a[mn],b[mn],c[mn];int in[mn],p[mn],q[mn],mark[mn],w[mn],fp[mn],from[mn],vis[mn];int i,j,k,n,x,y,z,e,ans,sum,r;bool huan;inline void addedge(int u,int v,int c){ g[e].s=u,g[e].t=v,g[e++].w=c;}inline int f(int a){ return a<0?-a:a;}inline int len(int i,int j){ return f(a[i]-a[j])+f(b[i]-b[j])+f(c[i]-c[j]);}inline void init(int& a){ char ch=getchar(); while (ch<'0'||ch>'9') ch=getchar(); for (a=0; ch>='0'&&ch<='9'; ch=getchar()) a=a*10+ch-48;}void swap(int i,int j){ edge tmp; tmp=g[i],g[i]=g[j],g[j]=tmp;}void mysort(int l,int r){ int i=l,j=r,s=g[(l+r)>>1].t,w=g[(l+r)>>1].w; while(i<=j) { while(g[i].t<s||(g[i].t==s&&g[i].w<w))++i; while(g[j].t>s||(g[j].t==s&&g[j].w>w))--j; if(i<=j)swap(i++,j--); } if(i<r)mysort(i,r); if(l<j)mysort(l,j);}int main(){ freopen("a.in","r",stdin); freopen("a.out","w",stdout); while(init(n),init(x),init(y),init(z),n+x+y+z) { for(i=1;i<=n;++i)init(a[i]),init(b[i]),init(c[i]); for(e=0,i=1;i<=n;++i) { addedge(0,i,c[i]*x); init(k); while(k--) { init(j); if(i==j)continue; addedge(i,j,len(i,j)*y+(c[i]>=c[j]?0:z)); } } mysort(0,e-1); for(i=0;i<=n;++i)fp[i]=p[i]=-1,in[i]=0,mark[i]=i; for(i=0;i<e;++i) if(p[g[i].t]<0)p[g[i].t]=i; huan=1,ans=sum=0; while(huan) { sum=huan=0; for(i=1;i<=n;++i) if(fp[j=mark[i]]>=0) { if(fp[i]<0)in[i]+=w[j],mark[i]=mark[mark[i]]; else { in[i]+=w[i],ans+=w[i]; if(g[++p[fp[i]]].t!=fp[i])p[fp[i]]=-1; } } for(i=0;i<=n;++i)fp[i]=-1,vis[i]=0; for(i=1;i<=n;++i) if(p[i]>=0) { if(fp[j=mark[i]]<0||(fp[j]>=0&&w[j]>g[p[i]].w-in[i])) w[j]=g[p[i]].w-in[i],fp[j]=i,from[j]=mark[g[p[i]].s]; } for(i=1;i<=n;++i) if(fp[i]>=0)sum+=w[i]; for(i=1;i<=n;++i) if(!vis[i]) { r=0,j=i; while(j>0&&vis[j]>=0) { if(vis[j]>0) { huan=1; while(q[--r]!=j)mark[q[r]]=j,vis[q[r]]=-1; vis[j]=-1; } else if(!vis[j])vis[q[r++]=j]=1; if(fp[j]>=0)j=from[j]; else j=-1; } while(r--)vis[q[r]]=fp[q[r]]=-1; } } printf("%d\n",ans+sum); } return 0;}
- hdu 4009(最小树形图)
- HDU 4009 最小树形图
- HDU 4009 最小树形图
- HDU 4009 最小树形图
- hdu 4009 最小树形图模版
- Hdu 4009 Transfer water (最小树形图)
- HDU 4009 不定根最小树形图
- hdu 2121+4009 最小树形图
- [HDU 4009] Transfer water 最小树形图
- HDU - 4009 Transfer water(最小树形图)
- Hdu 4009 Transfer water【最小树形图】
- hdu 4009 Transfer water 最小树形图
- HDU 4009Transfer water 最小树形图
- HDU 4009 Transfer water【最小树形图】
- HDU 4009 最小树形图裸题
- hdu 4966 最小树形图
- HDU 4966 (最小树形图)
- HDU 6141 最小树形图
- WPF 数据绑定 定制一个集合的视图 导航
- Web导出(二)之Freemarker+XML导出Word
- 设计模式之命令模式
- 通信专业如何发论文
- expdp时遇到的三个异常
- hdu 4009(最小树形图)
- HDU1060 数学方法实现超大数计算
- Milking Cows 挤牛奶
- 用java字节码解释i++和++i
- 什么是 C# 分部类(partia)
- smarty初体验
- attachEvent使用简介
- 三张经典图片展示Data Guard主备之间日志传输及恢复
- 网络课的笔记