Cave hdoj3672 LA4015

树形DP一枚,table[i][j][0]表示从i出发访问j个节点并且返回到i所走的最小距离,table[i][j][1]表示从i出发访问j个节点但不用返回到i所走的最短距离


table[i][j][0] = min(table[i][j-k][0]+table[z][k][0]+2*dist[i][z])(其中z为i的儿子, 0 < k < num[z])

table[i][j][1] = min(table[i][j-k][0]+table[z][k][1]+dist[i][z], table[i][j-k][1]+table[z][k][0]+2*dist[i][z]) (其中z为i的儿子, 0 < k < num[z])


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>   
#include <ctime>
 
using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::greater;
using std::endl;

const int MAXN(510);
const int MAXM(MAXN);
const int INFI((INT_MAX-1) >> 1);

struct EDGE
{
    int v, w;
    EDGE *next;
};

EDGE *first[MAXN];
EDGE edge[MAXM];
EDGE *rear;

void init()
{
    memset(first, 0, sizeof(first));
    rear = edge;
}

void insert(int tu, int tv, int tw)
{
    rear->v = tv;
    rear->w = tw;
    rear->next = first[tu];
    first[tu] = rear++;
}

int num[MAXN];
bool is_root[MAXN];

void dfs_num(int cur)
{
    num[cur] = 1;
    for(EDGE *i = first[cur]; i; i = i->next)
    {
        dfs_num(i->v);
        num[cur] += num[i->v];
    }
}

int table[MAXN][MAXN][2];

void dfs(int cur)
{
    if(num[cur] == 1)
    {
        table[cur][1][0] = table[cur][1][1] = 0;
        return;
    }
    int tn = num[cur];
    for(int i = 0; i <= tn; ++i)
        table[cur][i][0] = table[cur][i][1] = INFI;
    table[cur][1][0] = table[cur][1][1] = 0;
    for(EDGE *i = first[cur]; i; i = i->next)
    {
        dfs(i->v);
        for(int j = tn; j > 1; --j)
        {
            for(int k = min(num[i->v], j-1); k >= 1; --k)
            {
                table[cur][j][0] = min(table[cur][j][0], table[i->v][k][0]+table[cur][j-k][0]+2*i->w);
                table[cur][j][1] = min(table[cur][j][1], table[i->v][k][1]+table[cur][j-k][0]+i->w);
                table[cur][j][1] = min(table[cur][j][1], table[i->v][k][0]+table[cur][j-k][1]+2*i->w);
            }
        }
    }
}

int main()
{
    int n;
    int n_case(0);
    while(scanf("%d", &n), n)
    {
        init();
        memset(is_root, -1, sizeof(is_root));
        int root;
        int tu, tv, tw;
        for(int i = 1; i < n; ++i)
        {
            scanf("%d%d%d", &tv, &tu, &tw);
            insert(tu, tv, tw);
            is_root[tv] = false;
        }
        for(int i = 0; i < n; ++i)
            if(is_root[i])
            {
                root = i;
                break;
            }
        dfs_num(root);
        dfs(root);
        int q;
        scanf("%d", &q);
        printf("Case %d:\n", ++n_case);
        while(q--)
        {
            int x;
            scanf("%d", &x);
            int ans;
            for(ans = n; ans >= 1; --ans)
                if(table[root][ans][1] <= x)
                    break;
            printf("%d\n", ans);
        }
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值