uva1407 - Caves 树形DP

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$ \le$n$ \le$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$ \le$d$ \le$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$ \le$q$ \le$1000), which is the number of queries, followed by q lines. For one query, there is one integer x (0$ \le$x$ \le$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

 

  给你一棵树,知道各个节点之间的的距离。有Q个询问,每次询问一个路程,求从根出发走这个路程最多经过多少个节点(一个节点经过多次只算一次)。

  先算出经过1-N个节点的最小路程,询问的时候从后往前找。

  设dp[i][j][0]为以第i个节点为根经过j个节点并且回到根节点的最小路程,dp[i][j][1]为以第i个节点为根经过j个节点但是不回到根节点的最小路程。若v是u节点的子节点,w为u到v的距离,node[v]为以v为根的树的节点数量。那么有:

  dp[u][j][0]=min(dp[u][j][0],dp[u][j-k][0]+dp[v][k][0]+2*w)

  dp[u][j][1]=min(dp[u][j][1],min(dp[u][j-k][0]+dp[v][k][1]+w,dp[u][j-k][1]+dp[v][k][0]+2*w))

  注意k从1开始,循环进行条件是k<j&&k<=node[v]。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstdlib>
#define INF 0x3f3f3f3f
#define MAXN 510
#define MAXM 30
#define MAXT 1000010
#define MAXNODE 4*MAXN
#define pii pair<int,int>
using namespace std;
int N,Q,vis[MAXN],dp[MAXN][MAXN][2],node[MAXN];
vector<pii> V[MAXN];
int DFS(int u){
    vis[u]=1;
    node[u]=1;
    dp[u][1][0]=dp[u][1][1]=0;
    int L=V[u].size();
    for(int i=0;i<L;i++){
        int v=V[u][i].first,w=V[u][i].second;
        if(vis[v]) continue;
        node[u]+=DFS(v);
        for(int j=node[u];j>=2;j--){
            for(int k=1;k<j&&k<=node[v];k++){
                dp[u][j][0]=min(dp[u][j][0],dp[u][j-k][0]+dp[v][k][0]+2*w);
                dp[u][j][1]=min(dp[u][j][1],min(dp[u][j-k][0]+dp[v][k][1]+w,dp[u][j-k][1]+dp[v][k][0]+2*w));
            }
        }
    }
    return node[u];
}
int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    while(scanf("%d",&N),N){
        for(int i=0;i<N;i++) V[i].clear();
        for(int i=0;i<N-1;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            V[u].push_back(make_pair(v,w));
            V[v].push_back(make_pair(u,w));
        }
        memset(vis,0,sizeof(vis));
        memset(dp,INF,sizeof(dp));
        int n=DFS(0);
        printf("Case %d:\n",++cas);
        scanf("%d",&Q);
        while(Q--){
            int x;
            scanf("%d",&x);
            for(int i=n;i>=1;i--) if(dp[0][i][1]<=x){
                printf("%d\n",i);
                break;
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值