poj-2054 Color a Tree

Color a Tree
Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 7238Accepted: 2497

Description

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. 
poj-2054 Color a Tree - NatureRan - NatureRan

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
原題

題意:給定一棵樹,每個節點有一個權值,現要求給這些節點進行排列,設排列後的節點順序爲v1~vn,它們的權值是w1~wn,那麼我們要求一種排列使得w1*1+w2*2+...+wn*n最小。還有一個限制就是這個排列滿足每個節點的父節點都排在該結點之前。

分析:試想,如果沒有父節點排在節點之前的限制,那麼這個題目非常簡單,只需要將結點按照權值從大到小排列即可。加上了這個限制之後,如果權值最大的那個節點一旦滿足了條件(父節點被排在了之前的某個位置),那麼這個權值最大的節點一定要緊挨着這個父節點,即把這個權值最大的節點排在它所能排的最前面的位置。因爲對於這個節點如果不受限制應該排在第一位,而有了限制,在滿足了限制之後也應把它儘可能地排在前面。所以它一定是挨着父節點的。那麼現在在最終的排列中我們確定了兩個節點的前後相鄰關係,將他們綁定在了一起。

試想如果保持這個相鄰關係的同時去掉其他節點的限制,那麼我們應該如何排列呢?我們假設綁定在一起的兩節點是a和b。現有一個另外的節點x,我們看兩種排列xab,abx對最終的計算結果有什麼影響。x*i+a*(i+1)+b*(i+2); a*i + b*(i+1) + x*(i+2)。後者減去前者等於2x-(a+b)。即將x從ab之前挪到ab之後,ab各左移1位,結果減小a+b。x右移2位結果增加2x。因此兩者誰在前誰在後我們只需要比較a+b和2x即可,也可以比較(a+b)/2和x。

將這個定理進行一下推廣,綁定在一起的不一定是兩個節點,可以是一個更長的序列,與這個序列進行比較看誰放在前面的也可以是一個序列。設一個序列有n1個節點,第二個序列有n2個節點。那麼我們比較兩者誰放在前面的時候需要比較的是(n1個權值之和×n2)和(n2個權值之和×n1)。即左移和右移產生的結果變化。當然也可以比較(n1個權值之和/n1)和(n2個權值之和/n2)。

我們可以再次進行推廣,如果我們要排列的不是節點,而是許多序列的話,那麼我們只需要計算每個序列權值的平均數(例如:n個節點的序列,要計算n個權值之和/n),然後按照這個平均數從大到小排列即可使得計算結果最小。這樣就可以讓序列與節點有了一個統一的衡量值——平均數。

這樣一來,我們就可以將上面的綁定兩節點的操作看成是將問題規模縮小的操作,在幫定兩節點的同時我們在樹中也將兩節點合併,變爲一個節點,即將子節點的孩子變爲父節點的孩子。然後合併後的節點的權值是合併在這個節點中的所有節點的權值的平均數。我們成功的將問題規模減小了1。只需要不斷這樣做即可將問題縮減爲只有一個節點。

實現過程中,應在樹中每個節點記錄併入該節點的個數和權值和。樹的存儲與綁定前後關係的記錄要分開存儲,用一個數組單獨記錄前後綁定的排序關係。




貪心原則應該是Ci大的儘量先染色,但是由於父節點染了才能染子節點的限制使得問題不好解決了,但是Ci大的一定是在其父節點染色後立即被染色,這時大牛們的思路我也沒有看明白如何證明的,但仔細一想就明白了。於是我們根據這個條件就可以將Ci大的點與其父節點合併在一起組成一個集合。這樣就可以將問題規模減小。

   合併後的點(即集合)的屬性如何變化呢?假如設fact[i]表示集合的Ci和,iNum[i]表示i所屬集合的結點個數;那麼把fact[i]/iNum[i]作爲貪心原則,其值大者先合併到其父節點,最終合併成一個集合。答案是怎麼得出來的我看了很長時間才明白。先看一下下面的例子;

先初始化fact[i]=Ci, iNum[i]=1;將每一個點看成一個集合。

按照貪心原則首先選擇5合併到3形成集合A={3,5},fact[A]=5,iNum[A]=2;

Ans+=fact[5]*iNum[3]=4;

然後再按照貪心原則選擇集合A合併到1形成集合B={1,3,5},fact[B]=6,iNum[B]=3;

Ans+=fact[A]*iNum[1]=9;

然後再選擇2合併到集合B形成集合C={1,2,3,5},fact[C]=8, iNum[C]=4;

Ans+=fact[2]*iNum[B]=15;

最後選擇4合併到集合C形成集合D={1,2,3,4,5},fact[D]=10,iNum[D]=5;

Ans+=fact[4]*iNum[C]=23;

最後別忘了Ans+=fact[D]=33;




#include<cstdio>
#include<iostream>
#include<cstring>
#define N 1010
using namespace std;
int fa[N];
int a[N];
int num[N];
bool vis[N];
int n, root;
int fint()
{
int k;
double ma = 0;
for(int i=1; i<=n; ++i)
if(i!=root&&vis[i]&&ma < a[i]*1.0/num[i])
{
ma = a[i]*1.0/num[i];
k = i;
}
return k;
}
int main()
{
while(cin >> n >> root, n||root)
{
memset(vis,true,sizeof(vis));
memset(fa,0,sizeof(fa));
for(int i=1; i<=n; ++i)
num[i] = 1;
for(int i=1; i<=n; ++i)
cin >> a[i];
int x,y;
for(int i=1; i<n; ++i)
{
cin >> x >> y;
fa[y] = x;
}
int ans = 0,k,g;
for(int i=1; i<n; ++i)
{
k = fint();
vis[k] = false;
g = fa[k];
while(!vis[g]) g = fa[g];
ans += (num[g]*a[k]);
num[g] += num[k];
a[g] += a[k];
for(int e=1; e<=n; ++e)
if(fa[e] == k)
{
fa[e] = g;
}
}
ans += a[root];
cout << ans <<endl;
}
return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章