码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4276 The Ghost Blows Light(DP-树形DP)

时间:2014-08-29 21:26:18      阅读:370      评论:0      收藏:0      [点我收藏+]

标签:hdu 4276 the ghost b

The Ghost Blows Light

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2240    Accepted Submission(s): 688


Problem Description

My name is Hu Bayi, robing an ancient tomb in Tibet. The tomb consists of N rooms (numbered from 1 to N) which are connected by some roads (pass each road should cost some time). There is exactly one route between any two rooms, and each room contains some treasures. Now I am located at the 1st room and the exit is located at the Nth room. 
Suddenly, alert occurred! The tomb will topple down in T minutes, and I should reach exit room in T minutes. Human beings die in pursuit of wealth, and birds die in pursuit of food! Although it is life-threatening time, I also want to get treasure out as much as possible. Now I wonder the maximum number of treasures I can take out in T minutes.
 

Input
There are multiple test cases.
The first line contains two integer N and T. (1 <= n <= 100, 0 <= T <= 500)
Each of the next N - 1 lines contains three integers a, b, and t indicating there is a road between a and b which costs t minutes. (1<=a<=n, 1<=b<=n, a!=b, 0 <= t <= 100)
The last line contains N integers, which Ai indicating the number of treasure in the ith room. (0 <= Ai <= 100)
 

Output
For each test case, output an integer indicating the maximum number of treasures I can take out in T minutes; if I cannot get out of the tomb, please output "Human beings die in pursuit of wealth, and birds die in pursuit of food!".
 

Sample Input
5 10 1 2 2 2 3 2 2 5 3 3 4 3 1 2 3 4 5
 

Sample Output
11
 


 

 

 

 

 

 

 

bubuko.com,布布扣

 

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 110;
const int maxt = 510;
int dp[maxn][maxt] , A[maxn] , vis[maxn] , ans[maxt];
int way[maxn];
int N , T , cnt , sumT , route;

struct edge{
    int u , v , t , next;
    edge(int a = 0 , int b = 0, int c = 0 , int d = 0){
        u = a , v = b , t = c , next = d;
    }
}e[4*maxn];
int head[maxn];

void add(int u , int v , int t){
    e[cnt] = edge(u , v , t , head[u]);
    head[u] = cnt++;
}

void initial(){
    for(int i = 0; i < maxn; i++){
        head[i] = -1;
        A[i] = 0;
        vis[i] = 0;
        for(int j = 0; j < maxt; j++) dp[i][j] = 0;
    }
    for(int i = 0; i < maxt; i++) ans[i] = 0;
    cnt = 0;
    sumT = 0;
    route = 0;
}

void readcase(){
    int u , v , t;
    for(int i = 0; i < N-1; i++){
        scanf("%d%d%d" , &u , &v , &t);
        add(u , v , t);
        add(v , u , t);
    }
    for(int i = 1; i <= N; i++){
        scanf("%d" , &A[i]);
    }
}

void DP(int k , int f){
    dp[k][0] = A[k];
    for(int i = head[k]; i != -1; i = e[i].next){
        int son = e[i].v;
        if(son != f){
            DP(son , k);
            if(vis[son]) continue;
            for(int t = T; t >= 0; t--){
                if(dp[k][t] == 0 && t != 0) continue;
                for(int j = T; j >= 0; j--){
                    if(dp[son][j] != 0 && t+j+2*e[i].t <= T && dp[k][t+j+2*e[i].t] < dp[k][t]+dp[son][j]) dp[k][t+j+2*e[i].t] = dp[k][t]+dp[son][j];
                }
            }
        }
    }
}

bool dfs(int k , int index , int f){
    way[index] = k;
    route = index;
    if(k == N) return true;
    for(int i = head[k]; i != -1; i = e[i].next){
        int son = e[i].v;
        if(f != son){
            sumT += e[i].t;
            if(dfs(son , index+1 , k)) return true;
            sumT -= e[i].t;
        }
    }
    return false;
}

void computing(){
    dfs(1 , 0 , 1);
    for(int i = 0; i <= route; i++) vis[way[i]] = 1;
    DP(1 , 1);
    if(sumT > T){
        printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
        return;
    }
    T -= sumT;
    for(int i = 0; i <= route; i++){
        for(int t = T; t >= 0; t--){
            if(ans[t] == 0 && t != 0) continue;
            for(int j = T; j >= 0; j--){
                if(t+j <= T && ans[t+j] < ans[t]+dp[way[i]][j]) ans[t+j] = ans[t]+dp[way[i]][j];
            }
        }
    }
    int Max = 0;
    for(int i = 0; i <= T; i++) if(Max < ans[i]) Max = ans[i];
    printf("%d\n" , Max);
}

int main(){
    while(~scanf("%d%d" , &N , &T)){
        initial();
        readcase();
        computing();
    }
    return 0;
}

hdu 4276 The Ghost Blows Light(DP-树形DP)

标签:hdu 4276 the ghost b

原文地址:http://blog.csdn.net/u011836218/article/details/38929975

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!