HDU 1055 & POJ 2054 Color a tree & greedy find maximum cost point and parent node merge

Source: Internet
Author: User

Color a Tree
Time limit:1000ms Memory limit:30000k
Total submissions:7144 accepted:2458
Description


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


BOB intends to color all the nodes of a tree with a pen. A Tree has N nodes, these nodes is numbered 1, 2, ..., N. Suppose coloring a node takes 1 unit of time, and after Finishi ng Coloring One node, he is allowed to color another. Additionally, he is allowed to color a node-only while its father node has been colored. Obviously, Bob is a allowed to color the root in the first try.


Each node have a "coloring cost factor", Ci. The coloring cost of each node depends both in Ci and the time at which Bob finishes the coloring of this node. At the beginning, the time was set to 0. If the finishing time of coloring node I is fi and then the coloring cost of node I am Ci * Fi.


For example, a tree with five nodes are shown in Figure-1. The coloring cost factors of each node is 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 all node, please help Bob to find the minimum possible total coloring cost F or coloring all the nodes.
Input


The input consists of several test cases. The first line of all case contains-integers n and R (1 <= n <=, 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 are CI (1 <= Ci <=), the coloring cost factor of node I. E Ach of the next N-1 lines contains-space-separated node numbers V1 and V2, which is the endpoints of an edge in the T Ree, denoting that V1 is the Father node of V2. No Edge would be listed twice, and all edges would be listed.


A test Case of N = 0 and R = 0 indicates the end of input, and should not is 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


Test instructions

There is a tree that needs to stain every point he has. The node designator from the 1-n, the weights of each node are given in the second line of the input. Requires that the parent node be dyed, the child nodes can be dyed, and need to be dyed in topological order. Then each node needs a unit of staining time. Time starts at 1, the cost per node is the weight of the current time * node when calculating the cost.

The third line begins the next input is the relationship between the father and son of the tree.


Practice:

Greedy, and began to regard each point as a regiment individually. Then the weight of the regiment and the number of points in the regiment are recorded at the topmost ancestor of the regiment. Then each time the greedy find out (weights and/points) the largest group. Then the link list is used to record the relationship between the ancestors of the new regiment and their fathers. and merge the regiment with the regiment where the father was.

Finally, from the root, the calendar of the list, the time starting from 1, the total cost calculation.


Why so greedy, because if the order of more than the group first, it represents the first to go to a lot of regiment, spend more time, the cost will increase later. Therefore, the priority selection group is inversely proportional to the number of points and the total weight value of the regiment.





#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include < malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream># Include <algorithm>using namespace std, #include <stack> #include <queue> #include <vector># Include <deque> #include <set> #include <map> int w[1010];//weight int root,num[1010];//Group of points int n,c[1010   ]; The weighted value of the point int nex[1010],pre[1010]; Record of the linked list int fa[1010],vis[1010]; Parent node and have no access int find () {double maxx=-1;int id=-1;for (int i=1;i<=n;i++) {if (i!=root&& (1.0*w[i]/num[i]) > Maxx&&!vis[i]) {maxx= (1.0*w[i]/num[i]); id=i;}} Vis[id]=1;return ID;} void unit (int p) {int i;for (I=fa[p];~pre[i];i=pre[i]) {}//Find out the ancestors of the parent group, which is the first staining point in the regiment W[i]+=w[p];num[i]+=num[p];for (i=fa[ P];~nex[i];i=nex[i] {}//Find the last dyed point in the parent's Regiment Pre[p]=i;nex[i]=p;} int main () {while (scanf ("%d%d", &n,&root), n| | Root) {for (int i=1;i<=n;i++) {scanf ("%d", &w[i]); c[i]=w[i];num[i]=1;pre[i]=nex[i]=-1;vis[i]=0;} int u,v;for (int i=1;i<n;i++) {scanf ("%d%d", &u,&v); fa[v]=u;} while (1) {int Tem=find (), if (tem==-1) break; Unit (TEM);} int time=1;int ans=0;for (int i=root;~i;i=nex[i]) {ans+=time*c[i];time++;}    printf ("%d\n", ans);} return 0;}  /*5 11 2 1 2 41 21 32 43 50 0*/




HDU 1055 & POJ 2054 Color a tree & greedy find maximum cost point and parent node merge

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.