POJ-3345(树形DP)

本文详细介绍了树形DP算法的应用,通过具体的代码示例展示了如何计算一棵树中达到特定节点所需的最小成本。此外,还讨论了如何优化算法以减少递归次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于买通了根节点,全部子孙节点也都会被买通,所以进行一般的树形DP之后需要再对本根节点进行削峰处理


#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
#define INF 999999999

int N, M;
map<string,int> nameIdMap;      //node name index map
vector<string> domination[201]; //domination names of each node
int cost[201];                  //cost of each node
int gain[201];                  //gain of each node
vector<int> children[201];      //children nodes' index
bool isRoot[201];               //flag of whether node i is root of one tree
int need[201][201] = {0};       //need[i][j] is how much need for j votes in subtree i

void getGain(int i)
{
    gain[i] = 1;
    for(int k = children[i].size() - 1; k > -1; --k){
        int child = children[i][k];
        getGain(child);
        gain[i] += gain[child];
    }
}
void postDP(int i)
{
//init node i
    for(int j = M; j; --j) need[i][j] = INF;
//post traverse children
    for(int n = children[i].size() - 1; n > -1; --n){
        int child = children[i][n];
        postDP(child);
        //update this by child
        for(int j = M; j; --j)
            for(int k = 1; k <= j; ++k)
                need[i][j] = min(need[i][j], need[child][k] + need[i][j-k]);
    }
    for(int j = gain[i]; j; --j) need[i][j] = min(need[i][j], cost[i]);
}

int main()
{
    ios::sync_with_stdio(false);

    string line, name;
    while(true){
        nameIdMap.clear();
    //input N, M
        getline(cin, line);
        if(line == "#") break;
        sscanf(line.c_str(), "%d%d", &N, &M);
    //special judge
        if(M == 0){
            cout << "0\n";
            while(N--) getline(cin, line);
            continue;
        }
        if(M == 1){
            for(int i = 0; i < N; ++i){
                cin >> name >> cost[i];
                getline(cin, line);
            }
            cout << *min_element(cost, cost + N) << "\n";
            continue;
        }
    //initialize then input N countries' info
        for(int i = 1; i <= N; ++i){
            domination[i].clear();
            children[i].clear();
            isRoot[i] = true;
        //input its info
            cin >> name >> cost[i];
            nameIdMap.insert(map<string,int>::value_type(name, i));
        //input its dominations
            while(cin.get() != '\n'){
                cin >> name;
                domination[i].push_back(name);
            }
        }
    //build tree
        for(int i = 1; i <= N; ++i){
            for(int j = 0, s = domination[i].size(); j < s; ++j){
                int id = nameIdMap[domination[i][j]];
                children[i].push_back(id);
                isRoot[id] = false;
            }
        }
    //set those roots be children of node 0
        children[0].clear();
        for(int i = 1; i <= N; ++i){
            if(isRoot[i]) children[0].push_back(i);
        }
    //post traverse to get gain of each node
        getGain(0);
    //tree DP
        cost[0] = INF;
        postDP(0);
        cout << need[0][M] << "\n";
    }

    return 0;
}
实际上,上面的后续遍历树计算每棵子树的节点个数这一过程可以放在postDP里进行,从而减少一次递归遍历树的过程:

#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
#define INF 999999999

int N, M;
map<string,int> nameIdMap;      //node name index map
vector<string> domination[201]; //domination names of each node
int cost[201];                  //cost of each node
int gain[201];                  //gain of each node
vector<int> children[201];      //children nodes' index
bool isRoot[201];               //flag of whether node i is root of one tree
int need[201][201] = {0};       //need[i][j] is how much need for j votes in subtree i

void postDP(int i)
{
    gain[i] = 1;
    for(int j = M; j; --j) need[i][j] = INF;
    for(int n = children[i].size() - 1; n > -1; --n){
        int child = children[i][n];
        postDP(child);
        gain[i] += gain[child];
        //update this by child
        for(int j = M; j; --j)
            for(int k = 1; k <= j; ++k)
                need[i][j] = min(need[i][j], need[child][k] + need[i][j-k]);
    }
    for(int j = gain[i]; j; --j) need[i][j] = min(need[i][j], cost[i]);
}

int main()
{
    ios::sync_with_stdio(false);

    string line, name;
    while(true){
        nameIdMap.clear();
    //input N, M
        getline(cin, line);
        if(line == "#") break;
        sscanf(line.c_str(), "%d%d", &N, &M);

    //special judge
        if(M == 0){
            cout << "0\n";
            while(N--) getline(cin, line);
            continue;
        }
        if(M == 1){
            for(int i = 0; i < N; ++i){
                cin >> name >> cost[i];
                getline(cin, line);
            }
            cout << *min_element(cost, cost + N) << "\n";
            continue;
        }
    //initialize then input N countries' info
        for(int i = 1; i <= N; ++i){
            domination[i].clear();
            children[i].clear();
            isRoot[i] = true;
        //input its info
            cin >> name >> cost[i];
            nameIdMap.insert(map<string,int>::value_type(name, i));
        //input its dominations
            while(cin.get() != '\n'){
                cin >> name;
                domination[i].push_back(name);
            }
        }
    //build tree
        for(int i = 1; i <= N; ++i){
            for(int j = 0, s = domination[i].size(); j < s; ++j){
                int id = nameIdMap[domination[i][j]];
                children[i].push_back(id);
                isRoot[id] = false;
            }
        }
    //set those roots be children of node 0
        children[0].clear();
        for(int i = 1; i <= N; ++i){
            if(isRoot[i]) children[0].push_back(i);
        }
    //tree DP
        cost[0] = INF;
        postDP(0);
        cout << need[0][M] << "\n";
    }

    return 0;
}
没想到时间却比刚才长了,judge还真是随机呀……



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值