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

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.

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

30

Hint

In  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   |   We have carefully selected several similar problems for you:  4007 4004 4006 4001 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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-01 02:25:18

HDOJ 4009 Transfer water 最小树形图的相关文章

hdu 4009 Transfer water(最小树形图:有向图的最小生成树模板)

题目: 链接:点击打开链接 题意: 有n个村庄,要求使得每个村庄都能得到水的最小费用.每个村庄可以通过挖井或从其他村庄修水路获得水.挖井的费用是房子的高度乘以X,修水道的费用和有向图边的起点和终点的高度有关. 思路: 代码: #include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; #define inf 0x3f3f3f3f

HDU 4009 Transfer water 最小树形图

分析:建一个远点,往每个点连建井的价值(单向边),其它输水线按照题意建单向边 然后以源点为根的权值最小的有向树就是答案,套最小树形图模板 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <stack> #include <

hdoj 4009 Transfer water 【无源点最小树形图】【好题】

题目:hdoj 4009 Transfer water 题意:题目有点长,说是有个村子,有 n 户人家要用水,他们加的位置用三维坐标来表示(x,y,z),他们有两种选择: 1:自己挖一口井,花费为 z * cost_x 2:从别人家接个水管引过来,化为为距离 * cost_y,如果要引的地方比当前地方低的话,还要买一个水泵,花费cost_z. 距离算法|x2‐x1|+|y2‐y1|+|z2‐z1|. 然后求让所有的人都有用的水的最小花费. 分析:发现它是一个求最小生成树的题目,但是关键点有二 1

hdu4009 Transfer water 最小树形图

每一户人家水的来源有两种打井和从别家接水,每户人家都可能向外输送水. 打井和接水两种的付出代价都接边.设一个超级源点,每家每户打井的代价就是从该点(0)到该户人家(1~n)的边的权值.接水有两种可能,从高处接水,那么代价是哈密顿距离与Y的乘积(可以认为就是水管的费用):从低处接水,还要加上付出水泵的钱(水管+水泵的费用).这样就可以建图了.图论,会建图的话问题就解决一半了. 然后,用模版来解题.不过朱刘算法的模版时间复杂度的差异还是蛮大的.我的模版的建图是邻接矩阵,时间复杂度是O(N^3).超时

HDU4009 Transfer water —— 最小树形图 + 超级点

题目链接:https://vjudge.net/problem/HDU-4009 Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 5612    Accepted Submission(s): 1997 Problem Description XiaoA lives in a village. Last ye

Hdu 4009 Transfer water【最小树形图】

Transfer water Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 4702 Accepted Submission(s): 1665 Problem Description XiaoA lives in a village. Last year flood rained the village. So they decide to

HDU 4009 Transfer water

Transfer water Time Limit: 3000ms Memory Limit: 65768KB This problem will be judged on HDU. Original ID: 400964-bit integer IO format: %I64d      Java class name: Main XiaoA lives in a village. Last year flood rained the village. So they decide to mo

hdu 4009 Transfer water(最小型树图)

Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 3995    Accepted Submission(s): 1438 Problem Description XiaoA lives in a village. Last year flood rained the village. So they deci

kuangbin带你飞 生成树专题 : 次小生成树; 最小树形图;生成树计数

第一个部分 前4题 次小生成树 算法:首先如果生成了最小生成树,那么这些树上的所有的边都进行标记.标记为树边. 接下来进行枚举,枚举任意一条不在MST上的边,如果加入这条边,那么肯定会在这棵树上形成一个环,如果还要维护处树的特点 那么就要在这个环上删去一条边,这样他还是树,删掉的边显然是这条链上权值最大边更可能形成次小生成树.那么就有2中方法可以做. 第一种PRIM在prim时候直接可以做出这个从I到J的链上权值最大的值MAX[i][j]; 同时可以用kruskal同样方式标记树边,然后DFS跑