程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 4276 The Ghost Blows Light(DP-樹形DP)

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

編輯:C++入門知識

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


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

\

#include 
#include 
#include 
#include 
#include 
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;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved