POJ 2054 Color a Tree

https://cn.vjudge.net/problem/POJ-2054

题目

Bob is very interested in the data structure of a tree. A tree is a directed graph in which a special node is singled out, called the "root" of the tree, and there is a unique path from the root to each of the other nodes.

Bob intends to color all the nodes of a tree with a pen. A tree has N nodes, these nodes are numbered 1, 2, ..., N. Suppose coloring a node takes 1 unit of time, and after finishing coloring one node, he is allowed to color another. Additionally, he is allowed to color a node only when its father node has been colored. Obviously, Bob is only allowed to color the root in the first try.

Each node has a "coloring cost factor", Ci. The coloring cost of each node depends both on Ci and the time at which Bob finishes the coloring of this node. At the beginning, the time is set to 0. If the finishing time of coloring node i is Fi, then the coloring cost of node i is Ci * Fi.

For example, a tree with five nodes is shown in Figure-1. The coloring cost factors of each node are 1, 2, 1, 2 and 4. Bob can color the tree in the order 1, 3, 5, 2, 4, with the minimum total coloring cost of 33.

Given a tree and the coloring cost factor of each node, please help Bob to find the minimum possible total coloring cost for coloring all the nodes.

Input

The input consists of several test cases. The first line of each case contains two integers N and R (1 <= N <= 1000, 1 <= R <= N), where N is the number of nodes in the tree and R is the node number of the root node. The second line contains N integers, the i-th of which is Ci (1 <= Ci <= 500), the coloring cost factor of node i. Each of the next N-1 lines contains two space-separated node numbers V1 and V2, which are the endpoints of an edge in the tree, denoting that V1 is the father node of V2. No edge will be listed twice, and all edges will be listed.

A test case of N = 0 and R = 0 indicates the end of input, and should not be processed.

Output

For each test case, output a line containing the minimum total coloring cost required for Bob to color all the nodes.

Sample Input

5 1
1 2 1 2 4
1 2
1 3
2 4
3 5
0 0

Sample Output

33

题解

先只考虑同根的一层节点,显然应该先涂贵的……因此我们可以把根和最贵的合并,并记录合并顺序,合并后的节点价值为他们的和。

然后考虑不同层,有a、B、x三个节点,a和B已经合并了,那么有两种涂法:

  1. a->B->x
  2. x->a->B

价值分别为

  1. $k\times a+ (k+1)\times B+ (k+2)\times x$
  2. $k\times x+ (k+1)\times a+ (k+2)\times B$

只需要比较两式的大小,所以可以减掉一些数

  1. $2x$
  2. $a+b$

则$2x<a+b$时先涂aB,$2x>a+b$先涂x,$2x=a+b$都可以

同理可得,当$\boldsymbol{A}$和$\boldsymbol{B}$是未合并或经过合并后的节点的时候,两个比较为

  1. $k\times\boldsymbol{A}+(k+n[\boldsymbol{A}])\times\boldsymbol{B}$
  2. $k\times\boldsymbol{B}+(k+n[\boldsymbol{B}])\times\boldsymbol{A}$

即比较

  1. $n[\boldsymbol{A}]\times\boldsymbol{B}$
  2. $n[\boldsymbol{B}]\times\boldsymbol{A}$

  1. $\frac{\boldsymbol{B}}{n[\boldsymbol{B}]}$
  2. $\frac{\boldsymbol{A}}{n[\boldsymbol{A}]}$

即谁平均数大就先涂谁……

AC代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<iomanip>
#include<vector>

#define REP(r,x,y) for(register int r=(x); r<(y); r++)
#define REPE(r,x,y) for(register int r=(x); r<=(y); r++)
#define PERE(r,x,y) for(register int r=(x); r>=(y); r--)
#ifdef sahdsg
#define DBG(...) printf(__VA_ARGS__)
#else
#define DBG(...) (void)0
#endif

using namespace std;
typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;
template <class T>
inline void read(T& x) {
	char c=getchar(); int f=1; x=0; while(!isdigit(c)&&c!='-') c=getchar();
	if(c=='-')f=-1,c=getchar();	while(isdigit(c)) {x=x*10+c-'0';c=getchar();}
	x*=f;
}
int n,r;
int C[1007], fa[1007], nxt[1007], cnt[1007], lst[1007];
double sum[1007], c[1007];
bool vis[1007];
inline void fmb() {
	int k=-1;
	double maxx=-1;
	REP(i,0,n) if(i!=r) {
		if(!vis[i] && c[i]>maxx) {
			maxx=c[i], k=i;
		}
	}
	int f=fa[k];
	while(vis[f]) fa[k]=f=fa[f];
	nxt[lst[f]]=k;
	lst[f]=lst[k];
	cnt[f]+=cnt[k];
	sum[f]+=sum[k];
	c[f]=sum[f]/cnt[f];
	vis[k]=1;
}
inline void go(int x) {
	int ans=0;
	REPE(k,1,n) {
		ans+=k*C[x];
		x=nxt[x];
	}
	printf("%d\n", ans);
}
int main() {
	while(~scanf("%d%d", &n, &r) && n) {
		memset(vis,0,sizeof vis);
		r--;
		REP(i,0,n) {
			read(C[i]);
			lst[i]=i;
			cnt[i]=1;
			sum[i]=c[i]=C[i];
			
		}
		REP(_,1,n) {
			int a,b;
			read(a);read(b);
			fa[b-1]=a-1;
		}
		REP(_,0,n) {
			fmb();
		}
		go(r);
	}
	return 0;
}

 

转载于:https://www.cnblogs.com/sahdsg/p/10719755.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值