Transfer water
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 4157 Accepted Submission(s): 1487
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 30 1 3 2 2 4 1 1 2 2 1 2 0 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|.
每个点可以选择打井或者从别的地方引一条线把水引过来。。。。
虚拟一个根 这个点到其他的点的权值为打水的费用
枚举其他点的情况 建边 根据高度情况 计算不同的费用
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1010
#define MAXM 10010
#define INF 1000000000
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
struct Edge
{
int u,v;
int cost;
}edge[MAXN*MAXN];
int pre[MAXN],id[MAXN],vis[MAXN];
int in[MAXN];
int ansid;
int zhuliu(int root,int n,int m)
{
int res=0;
int u,v;
while(1)
{
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;
if(edge[i].u==root) ansid=i;
}
for(int i=0;i<n;i++)
if(i!=root&&in[i]==INF)
return -1;
int tn=0;
MEM(id,-1);
MEM(vis,-1);
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;i++)
{
// int uu = edge[i].u;
// int vv = edge[i].v;
// edge[i].u = id[uu];
// edge[i].v = id[vv];
// if(id[uu] != id[vv])
// edge[i].cost -= in[vv];
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];
}
n=tn;
root=id[root];
}
return res;
}
struct point
{
int x,y,z;
void input()
{
scanf("%d%d%d",&x,&y,&z);
}
}p[MAXN];
int dis(int a,int b)
{
return abs(p[a].x-p[b].x)+abs(p[a].y-p[b].y)+abs(p[a].z-p[b].z);
}
int main()
{
// fread;
int n,x,y,z;
while(scanf("%d%d%d%d",&n,&x,&y,&z)!=EOF)
{
if(n==0&&x==0&&y==0&&z==0) break;
for(int i=1;i<=n;i++)
p[i].input();
int num=0;
int sum=0;
for(int i=1;i<=n;i++)
{
int k;
scanf("%d",&k);
while(k--)
{
int v;
int j;
scanf("%d",&j);
if(i==j) continue;
edge[num].u=i; edge[num].v=j;
int len=dis(i,j);
if(p[i].z>=p[j].z)
edge[num].cost=len*y;
else edge[num].cost=len*y+z;
// sum+=edge[num].cost;
num++;
}
}
for(int i=1;i<=n;i++)
{
edge[num].u=0; edge[num].v=i;
edge[num].cost=x*p[i].z; num++;
}
int ans=zhuliu(0,n+1,num);
if(ans==-1)
puts("poor XiaoA");
else printf("%d\n",ans);
}
return 0;
}