1. 程式人生 > >HDU oj 1055(Color a Tree 貪心好題)

HDU oj 1055(Color a Tree 貪心好題)

Color a Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1872    Accepted Submission(s): 654


Problem 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
   
  
 
http://acm.hdu.edu.cn/showproblem.php?pid=1055

題意:給定一個拓撲圖(n個結點n-1條邊 ),每個節點有一定的權重,按照拓撲順序得到一個全序,使得到的全序的代價最小。結點代價的計算為其在全序中的順序乘以其本身的權重。
策略: 貪心。
選取當前權重最大的一個點,將其和其父節點合併成一個點,並在其和父親中連一條有向邊確定其偏序(父親指向兒子)。合併的同時更新拓撲圖和權重。對於權重的更新,有以下的證明:
首先對於每一次的選取,都可以簡化成一個結點和一條鏈或一個結點和一個結點的權重比較。設當前已選(t-1)次,即在選全序的t號元素時,

1,2,3點的權重分別為a, b, c, 先選取1的總代價為a*t+b*(t+1)+c*(t+2), 先選取2,3的代價為b*t+c*(t+1)+a*(t+2),若b*t+c*(t+1)+a*(t+2)a*t+b*(t+1)+c*(t+2), 則   a < (b+c)/2; 類似的遞推可以得到結論:任意一條鏈的權重可以等價為鏈上結點權重的算術平均值。那麼貪心的策略就是先取出拓撲圖中權重最大的鏈。
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;

const int maxn = 1010;

struct node{
    double weight;
    int id;
    node(){}
    node(double _w, int _id):weight(_w), id(_id){}
    bool operator < (const node &n) const{
        return weight < n.weight;
    }
};

double c[maxn], ww[maxn];
int cnt[maxn];
int fa[maxn], G[maxn][maxn];
int path[maxn], nex[maxn];

void init(){
    memset(fa, -1, sizeof fa);
    memset(path, -1, sizeof path);
    memset(G, 0, sizeof G);
    for(int i = 0; i < maxn; i++) nex[i] = i, cnt[i] = 1;
}

int getnext(int id){  
    int i;
    for(i = id; i != nex[i]; i = nex[id]);
    return i;
}

int main(){
    int n, r;
    while(~scanf("%d%d", &n, &r) &&(n||r)){
        init();
        priority_queue<node> q;
        for(int i = 0; i < n; i++){
            scanf("%lf", c+i+1);
            ww[i+1] = c[i+1];
            q.push(node(c[i+1], i+1));
        }
        for(int i = 1; i < n; i++){
            int u, v;
            scanf("%d%d", &u, &v);
            fa[v] = u;
            G[u][++G[u][0]] = v;  
        }
        int cc = 0, ans = 0;
        while(!q.empty()){
            node u = q.top(); q.pop();
            int id = u.id;
            double w = u.weight;
            int fau = fa[id];
            if(cnt[id] == 0) continue;
            if(fau != -1){
                c[fau] = double(c[fau]*cnt[fau] + c[id]*cnt[id])/(1.0*cnt[fau] + cnt[id]); //更新權重
                cnt[fau] = cnt[fau] + cnt[id];
                for(int i = 1; i <= G[id][0]; i++){ //更新圖的結構
                    fa[G[id][i]] = fau;
                    G[fau][++G[fau][0]] = G[id][i];
                }
                q.push(node(c[fau], fau));
                path[nex[fau]] = id;          //記錄最優的選擇路徑,nex[i]陣列維護以i開頭的路徑的最後一個元素
                nex[fau] = getnext(id);
            }
            else {
                for(int i = 1; i <= G[id][0]; i++){
                    fa[G[id][i]] = -1;
                }
                for(int i = id; i != -1; i = path[i]){
                    ans += (++cc)*(int)(ww[i]+0.5);
                }
            }
            cnt[id] = 0;
        }
        printf("%d\n", ans);
    }
    return 0;
}