POJ-3345(树形DP)

来源:互联网 发布:mac版录音 编辑:程序博客网 时间:2024/04/30 02:49

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


#include <cstdio>#include <string>#include <iostream>#include <vector>#include <map>#include <algorithm>using namespace std;#define INF 999999999int N, M;map<string,int> nameIdMap;      //node name index mapvector<string> domination[201]; //domination names of each nodeint cost[201];                  //cost of each nodeint gain[201];                  //gain of each nodevector<int> children[201];      //children nodes' indexbool isRoot[201];               //flag of whether node i is root of one treeint need[201][201] = {0};       //need[i][j] is how much need for j votes in subtree ivoid 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 999999999int N, M;map<string,int> nameIdMap;      //node name index mapvector<string> domination[201]; //domination names of each nodeint cost[201];                  //cost of each nodeint gain[201];                  //gain of each nodevector<int> children[201];      //children nodes' indexbool isRoot[201];               //flag of whether node i is root of one treeint need[201][201] = {0};       //need[i][j] is how much need for j votes in subtree ivoid 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还真是随机呀……



0 0