UVALive 4015 树形dp

来源:互联网 发布:淘宝写论文有靠谱的吗 编辑:程序博客网 时间:2024/03/29 23:36
A - Caves
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status Practice UVALive 4015 uDebug
Appoint description: 

Description

Download as PDF

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
       题意:给一个树形图,每条边(双向边)有一个边权,表示距离,每次从0点即树根处进入,问走x的路程,能经过多少分个节点。
      dp[i][j][0,1]表示路程,dp[i][j][1],表示从i出发经过了j个节点最后返回到i的路程 dp[i][j][0]表示从i出发经过了j个顶点
最后不回到i的路程
那么判断一条边 u->v 时  ,dp[u][j][1] =   dp[u][j-k][1]+dp[v][k][1]+dis[u][v]*2  因为最后要返回到u,所以u到了v后再回来,所以加二倍 的dis[u][v],dp[u][j][0] = min(  dp[u][j-k][0]+dp[v][k][1]+2*dis[u][v],   dp[u][j-k][1]+dp[v][k][0]+dis[u][v]     ),因为要么是
在u出发回到u再走u->v再从v走k个点  ,要么是先走 u->v 再从v走k个点再回到v再走v->u回到u ,再走j-k个点,比较两个
方案路程短的优先;
ac代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;typedef long long LL;const int maxn=505,inf=0x3f3f3f3f;struct node{    int v,next,cap;}edge[maxn];int tot,head[maxn];void init(){    tot=0;    memset(head,-1,sizeof(head));}void add_edge(int u,int v,int cap)///{    edge[tot].v=v;    edge[tot].cap=cap;    edge[tot].next=head[u];    head[u]=tot++;}int son[maxn],dp[maxn][maxn][2];int dfs(int u){    son[u]=1;    for(int i=head[u];i!=-1;i=edge[i].next)    {        son[u]+=dfs(edge[i].v);    }    dp[u][1][0]=dp[u][1][1]=0;    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        int cap=edge[i].cap;        for(int j=son[u];j>=1;j--)        {            for(int k=1;k<=son[v]&&k<j;k++)            {                dp[u][j][1]=min(dp[u][j][1],dp[u][j-k][1]+dp[v][k][1]+2*cap);                dp[u][j][0]=min(dp[u][j][0],min(dp[u][j-k][0]+dp[v][k][1]+cap*2,dp[u][j-k][1]+dp[v][k][0]+cap));            }        }    }    return son[u];}int main(){    int n,u,v,cap;    int q,s;    int T=1;    while(scanf("%d",&n)!=-1)    {        if(n==0)break;        init();        for(int i=1;i<n;i++)        {            scanf("%d%d%d",&u,&v,&cap);            add_edge(v,u,cap);        }        memset(dp,inf,sizeof(dp));        dfs(0);        scanf("%d",&q);        printf("Case %d:\n",T++);        while(q--)        {            scanf("%d",&s);            for(int i=n;i>=0;i--)            {                if(dp[0][i][0]<=s)                {                    printf("%d\n",i);                    break;                }            }        }    }    return 0;}


0 0
原创粉丝点击