标签:
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|.
/* *********************************************** 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 最小树形图
标签:
原文地址:http://blog.csdn.net/ck_boss/article/details/46771535