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

UVALive 4015 - Caves(树形DP)

时间:2015-05-13 10:32:41      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:dp   uva   

Description

技术分享

It is said that the people of Menggol lived in caves. A tribe‘s caves were connected to each other with paths. The paths were so designed that there was one and only one path to each cave. So the caves and the paths formed a tree. There was a main cave, which connected the world outside. The Menggolian always carved a map of the tribe‘s caves on the wall of the main cave.

Scientists have just discovered Menggolian‘s tribe. What a heart-stirring discovery! They are eager to explore it. Since the terrain is very complex and dangerous, they decide to send out a robot.

The robot will be landed into the main cave, where he will begin his adventure. It doesn‘t have to return to the main cave, because the messages of his exploration will be sent immediately to the scientists while he is on the way.

A robot can only walk x meters before it runs out of energy. So the problem arises: given the map of the tribe‘s caves and a set of x , how many caves can be explored at most?

Input

There are multiple test cases in the input file. Each test case starts with a single number n(0技术分享n技术分享500) , which is the number of caves, followed by n - 1 lines describing the map. Each of the n - 1 lines contains three integers separated by blanks: i , j , and d(1技术分享d技术分享10000) . It means that the i -th cave‘s parent cave is the j -th cave and the distance is d meters. A parent cave of cave i is the first cave to enter on the path from i to the main cave. Caves are numbered from 0 to n - 1 . Then there is an integer q(1技术分享q技术分享1000) , which is the number of queries, followed by q lines. For one query, there is one integer x(0技术分享x技术分享5000000) , the maximum distance that the robot can travel. n = 0 indicates the end of input file.

Output

For each test case, output q lines in the format as indicated in the sample output, each line contains one integer, the maximum number of caves the robot is able to visit.

Sample Input

3 
1 0 5 
2 0 3 
3 
3 
10 
11 
0

Sample Output

Case 1:
2
2
3


题意:一棵n个节点的有根树,树的边有正整数权,表示两个节点之间的距离,你的任务是回答这样的询问,从根节点出发,走不超过x单位的距离,最多能走多少个节点,节点经过多次算一个,对于每次的询问输出:经过节点数最大的值。 注意题目给出的  i, j,d,其中 j 是 i 的父节点。


思路:树形DP,设 d[ i ][ j ][ k ] 表示以第i个节点为根节点的子树机器人访问j个节点走的最少的路程,k==0表示访问完后又回到i节点,k==1 表示访问完后不回来,

则状态转移方程为:

d[x][j+k][0] = min(d[x][j+k][0],d[x][j][0]+d[y][k][0]+len*2);

d[x][j+k][1] = min(d[x][j+k][1],min(d[x][j][1]+d[y][k][0]+len*2,d[x][j][0]+d[y][k][1]+len));

y 表示 x 的儿子,len 表示 x 到 y 的树枝长度。

用 son[ i ] 表示 i 这棵树的节点数。d[i][1][0] = d[i][1][1] = 0, 每个点到自身的距离均为 0 。接下来就是单独考虑它的子节点了,首先考虑如果返回的话,就一种可能就是:它走其他的子树要返回,然后还要对当前的节点要返回。

如果不返回的话,那么它可能走其他的子树要返回,当前的不返回,还有就是当前的子树返回,其他的子树不返回。

还有枚举 son[ i ] 时要从大到小,因为这是 0, 1 背包。


#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const double e = 2.718281828459;
const double eps = 1e-8;
const int MAXN = 550;
int dp[MAXN][MAXN][2];
int n, son[MAXN], cnt[MAXN];
vector<int>g[MAXN];

void dfs(int x)
{
    dp[x][1][0] = dp[x][1][1] = 0;
    son[x] = 1;
    for (int i = 0; i < g[x].size(); i += 2)
    {
        int y = g[x][i];
        int len = g[x][i+1];
        dfs(y);
        for(int j = son[x]; j > 0; j--)
        {   // son 数组要从大到小,从小到大会出错
            for (int k = 1; k <= son[y]; k++)
            {
                dp[x][j+k][0] = min(dp[x][j+k][0], dp[x][j][0]+dp[y][k][0]+len*2);
                dp[x][j+k][1] = min(dp[x][j+k][1], min(dp[x][j][1]+dp[y][k][0]+len*2, dp[x][j][0]+dp[y][k][1]+len));
                //printf("dp[%d][%d] 1   =   %d\n", x, j+k, dp[x][j+k][1]);
                //printf("dp[%d][%d] 0   =   %d\n", x, j+k, dp[x][j+k][0]);
            }
        }
        son[x] += son[y];
    }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int Case = 1;
    int u, v, w, s;
    while (cin>>n && n)
    {
        for (int i = 0; i <= n; i++)
            g[i].clear();
        memset(cnt, 0, sizeof(cnt));
        for (int i = 1; i < n; i++)
        {
            scanf("%d %d %d", &u, &v, &w);
            cnt[u] = 1;
            g[v].push_back(u);
            g[v].push_back(w);
        }
        s = 0;
        for (int i = 1; i < n; i++)
            if (!cnt[i])
            {
                s = i;
                break;
            }
        memset(dp, INF, sizeof(dp));
        dfs(s);
        int q, x;
        scanf("%d",&q);
        printf("Case %d:\n", Case++);
        while(q--)
        {
            scanf("%d", &x);
            int ans = 1;
            for (int i = 1; i <= n; i++)
            {
                //printf("%d %d\n", dp[s][i][0], dp[s][i][1]);
                if (dp[s][i][0]<=x || dp[s][i][1] <= x)
                    ans = i;
            }
            printf("%d\n", ans);
        }
    }
    return 0;
}



UVALive 4015 - Caves(树形DP)

标签:dp   uva   

原文地址:http://blog.csdn.net/u014028317/article/details/45678921

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