Transfer water
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 5148 Accepted Submission(s): 1851
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|.
Source
Recommend
lcy
题意:n,X,Y,Z分别代表,n个村子,每个村子可以选择自己凿井或者从有水的村子里引水过来。如果选择自己凿井,那么所需要的费用就是自家高度(z坐标)*X,如果选择从别人家引水,那么费用是两个村子的两地曼哈顿距离*Y,如果终点比起点要高那么还需要加上Z。 最后让所有人家里都有水,最少需要多少费用。
解题思路:最小树形图,一个超级源点0号点代表自家凿井,这道题其实不存在找不到路径的情况,因为每个村子都可以自己凿井,也就是从0号店到每个村子各一条边
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
const int MAXN = 1010;
const int MAXM = 1000010;
struct Edge
{
int u,v,cost;
}edge[MAXM];
struct Point
{
int x,y,z;
}p[1050];
int pre[MAXN],id[MAXN],visit[MAXN],in[MAXN];
int zhuliu(int root,int n,int m,Edge edge[])
{
int res = 0,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;
}
}
for(int i = 0; i < n; i++)
if(i != root && in[i] == INF) return -1; //不存在最小树形图
int tn = 0;
memset(id,-1,sizeof(id));
memset(visit,-1,sizeof(visit));
in[root] = 0;
for(int i = 0; i < n; i++)
{
res += in[i];
v = i;
while( visit[v] != i && id[v] == -1 && v != root)
{
visit[v] = i;
v = pre[v];
}
if( v != root && id[v] == -1 )
{
for(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++)
{
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()
{
int n,x,y,z;
while(~scanf("%d%d%d%d",&n,&x,&y,&z))
{
if(!x&&!y&&!z&&!n) break;
for(int i=1;i<=n;i++)
scanf("%d %d %d",&p[i].x,&p[i].y,&p[i].z);
int v,k;
int L = 0;
for(int i=1;i<=n;i++)
{
scanf("%d",&k);
edge[L].u = 0; edge[L].v = i;
edge[L++].cost = p[i].z * x;
for(int j=1;j<=k;j++)
{
scanf("%d",&v);
edge[L].u = i;
edge[L].v = v;
edge[L].cost =(abs(p[i].x-p[v].x)+abs(p[i].y-p[v].y)+abs(p[i].z-p[v].z))*y;
if(p[i].z<p[v].z) edge[L].cost+=z;
L++;
}
}
int ans=zhuliu(0,n+1,L,edge);
if(ans==-1) printf("poor XiaoA\n");
else printf("%d\n",ans);
}
return 0;
}