日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 2054 Color a Tree

發(fā)布時(shí)間:2025/3/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 2054 Color a Tree 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

?

貪心。。。。

? ? ? ? ? ? ? ? ? ?Color a Tree
Time Limit:?1000MS?Memory Limit:?30000K
Total Submissions:?6647?Accepted:?2249

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.?

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

Source

Beijing 2004


?

貪心原則應(yīng)該是Ci大的盡量先染色,但是由于父節(jié)點(diǎn)染了才能染子節(jié)點(diǎn)的限制使得問題不好解決了,但是Ci大的一定是在其父節(jié)點(diǎn)染色后立即被染色,這時(shí)大牛們的思路我也沒有看明白如何證明的,但仔細(xì)一想就明白了。于是我們根據(jù)這個條件就可以將Ci大的點(diǎn)與其父節(jié)點(diǎn)合并在一起組成一個集合。這樣就可以將問題規(guī)模減小。

?? 合并后的點(diǎn)(即集合)的屬性如何變化呢?假如設(shè)fact[i]表示集合的Ci和,iNum[i]表示i所屬集合的結(jié)點(diǎn)個數(shù);那么把fact[i]/iNum[i]作為貪心原則,其值大者先合并到其父節(jié)點(diǎn),最終合并成一個集合。

?

1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 5 using namespace std; 6 7 struct Edge 8 { 9 int to,next; 10 }e[1111]; 11 12 int n,root,Size,Adj[1111],c[1111],num[1111],father[1111]; 13 bool vis[1111]; 14 15 void Init() 16 { 17 Size=0; 18 memset(Adj,-1,sizeof(Adj)); 19 memset(vis,false,sizeof(vis)); 20 } 21 22 void Add_Edge(int u,int v) 23 { 24 ///u-->v 25 e[Size].to=v; 26 e[Size].next=Adj[u]; 27 Adj[u]=Size++; 28 } 29 30 int Find() 31 { 32 int k=-1; 33 double maxn=-0x3f3f3f3f; 34 for(int i=1;i<=n;i++) 35 { 36 if(!vis[i]&&i!=root&&maxn<(double)c[i]/num[i]) 37 { 38 maxn=(double)c[i]/num[i]; 39 k=i; 40 } 41 } 42 return k; 43 } 44 45 void Union(int a,int b) 46 { 47 /// a to b 48 num[b]+=num[a]; 49 c[b]+=c[a]; 50 father[a]=b; 51 for(int i=Adj[a];~i;i=e[i].next) 52 { 53 int v=e[i].to; 54 father[v]=b; 55 } 56 } 57 58 int solve() 59 { 60 int ans=0; 61 for(int i=0;i<n-1;i++) 62 { 63 int k=Find(); 64 vis[k]=true; 65 int p=father[k]; 66 while(vis[p]) p=father[p]; 67 ans+=c[k]*num[p]; 68 Union(k,p); 69 } 70 ans+=c[root]; 71 return ans; 72 } 73 74 int main() 75 { 76 while(scanf("%d%d",&n,&root)!=EOF) 77 { 78 if(n==0&&root==0) break; 79 Init(); 80 for(int i=1;i<=n;i++) 81 { 82 scanf("%d",c+i); 83 num[i]=1; 84 } 85 for(int i=0;i<n-1;i++) 86 { 87 int u,v; 88 scanf("%d%d",&u,&v); 89 Add_Edge(u,v); 90 father[v]=u; 91 } 92 printf("%d\n",solve()); 93 } 94 return 0; 95 }

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/CKboss/p/3361809.html

總結(jié)

以上是生活随笔為你收集整理的POJ 2054 Color a Tree的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。