POJ3345---Bribing FIPA(树形dp+背包)

Description

There is going to be a voting at FIPA (Fédération Internationale de Programmation Association) to determine the host of the next IPWC (International Programming World Cup). Benjamin Bennett, the delegation of Diamondland to FIPA, is trying to seek other delegation’s support for a vote in favor of hosting IWPC in Diamondland. Ben is trying to buy the votes by diamond gifts. He has figured out the voting price of each and every country. However, he knows that there is no need to diamond-bribe every country, since there are small poor countries that take vote orders from their respected superpowers. So, if you bribe a country, you have gained the vote of any other country under its domination (both directly and via other countries domination). For example, if C is under domination of B, and B is under domination of A, one may get the vote of all three countries just by bribing A. Note that no country is under domination of more than one country, and the domination relationship makes no cycle. You are to help him, against a big diamond, by writing a program to find out the minimum number of diamonds needed such that at least m countries vote in favor of Diamondland. Since Diamondland is a candidate, it stands out of the voting process.

Input

The input consists of multiple test cases. Each test case starts with a line containing two integers n (1 ≤ n ≤ 200) and m (0 ≤ m ≤ n) which are the number of countries participating in the voting process, and the number of votes Diamondland needs. The next n lines, each describing one country, are of the following form:

CountryName DiamondCount DCName1 DCName1 …

CountryName, the name of the country, is a string of at least one and at most 100 letters and DiamondCount is a positive integer which is the number of diamonds needed to get the vote of that country and all of the countries that their names come in the list DCName1 DCName1 … which means they are under direct domination of that country. Note that it is possible that some countries do not have any other country under domination. The end of the input is marked by a single line containing a single # character.

Output

For each test case, write a single line containing a number showing the minimum number of diamonds needed to gain the vote of at least m countries.

Sample Input

3 2

Aland 10

Boland 20 Aland

Coland 15

#

Sample Output

20

Source

Tehran 2006

简单的树形dp背包

dp[u][num] 表示在以u为根的子树里得到num个城市的代价

输入注意下,别弄错了

/*************************************************************************
    > File Name: POJ3345.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年05月11日 星期一 20时24分34秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int num[210];
int in_deg[210];
int dp[210][210];
vector <int> tree[210];
int w[210];
map <string, int> mp;
char str[110];
char str2[110];
string tmp;

void DP(int u) {
    num[u] = 1;
    int size = tree[u].size();
    dp[u][0] = 0;
    if (!size) {
        dp[u][1] = w[u];
        return;
    }
    for (int i = 0; i < size; ++i) {
        int v = tree[u][i];
        DP(v);
        num[u] += num[v];
    }
    dp[u][num[u]] = w[u];
    for (int i = 0; i < size; ++i) {
        int v = tree[u][i];
        for (int j = num[u] - 1; j >= 0; --j) {
            for (int k = 0; k <= j && k <= num[v]; ++k) {
                dp[u][j] = min(dp[u][j], dp[v][k] + dp[u][j - k]);
            }
        }
    }
}

int main() {
    int n, m;
    while (~scanf("%s", str)) {
        if (str[0] == ‘#‘) {
            break;
        }
        mp.clear();
        n = 0;
        scanf("%d", &m);
        for (int i = 0; str[i] != ‘\0‘; ++i) {
            n = n * 10 + str[i] - ‘0‘;
        }
        int cnt = 0;
        memset(dp, inf, sizeof(dp));
        for (int i = 0; i <= n; ++i) {
            tree[i].clear();
            in_deg[i] = 0;
        }
        getchar();
        for (int i = 1; i <= n; ++i) {
            getline(cin, tmp);
            int len = tmp.length();
            int j = 0;
            while (tmp[j] != ‘ ‘) {
                str[j] = tmp[j];
                ++j;
            }
            str[j] = ‘\0‘;
            if (mp.find(str) == mp.end()) {
                mp[str] = ++cnt;
            }
            w[mp[str]] = 0;
            ++j;
            while (j < len && tmp[j] >= ‘0‘ && tmp[j] <= ‘9‘) {
                w[mp[str]] = w[mp[str]] * 10 + tmp[j++] - ‘0‘;
            }
            if (j == len) {
                continue;
            }
            int len2 = 0;
            ++j;
            for (; j < len; ++j) {
                if (tmp[j] == ‘ ‘) {
                    str2[len2] = ‘\0‘;
                    if (mp.find(str2) == mp.end()) {
                        mp[str2] = ++cnt;
                    }
                    tree[mp[str]].push_back(mp[str2]);
                    ++in_deg[mp[str2]];
                    len2 = 0;
                }
                else {
                    str2[len2++] = tmp[j];
                }
            }
            str2[len2] = ‘\0‘;
            if (mp.find(str2) == mp.end()) {
                mp[str2] = ++cnt;
            }
            tree[mp[str]].push_back(mp[str2]);
            ++in_deg[mp[str2]];
        }
        for (int i = 1; i <= n; ++i) {
            if (!in_deg[i]) {
                tree[0].push_back(i);
                w[0] += w[i];
            }
        }
        DP(0);
        int ans = inf;
        for (int i = m; i <= n; ++i) {
            ans = min(ans, dp[0][i]);
        }
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-10-16 06:46:09

POJ3345---Bribing FIPA(树形dp+背包)的相关文章

HDU 1011 Starship Troopers(树形dp+背包)

Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13109    Accepted Submission(s): 3562 Problem Description You, the leader of Starship Troopers, are sent to destroy a base of

hdu1011 树形dp背包

http://acm.hdu.edu.cn/showproblem.php?pid=1011 Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built underground. It is actually a huge cavern, which consists of many rooms connected with

Poj 1112 Rebuilding Roads(树形DP+背包)

题意:给你由N个点构成一颗树,问要孤立出一个有P个节点的子树最少需要删除多少条边.N的范围最大为150 N的范围不大,很容易想到在树上面做背包.把每个节点都看成一个背包,然后把每个儿子节点都看成是一组物品.为什么是一组呢,那是因为假设以儿子为根的节点的子树有S个节点,那么就有S+1种情况,要么将这整棵子树舍弃,要么从这个子树中取1-S个节点. 设f[i][j]为以i为根节点的子树,孤立出以i为根节点,一共含有j个节点的子树最少需要删除的边数(不包括删除i和他父亲的连接的那条边(假设i不是根节点)

HDU 1561 The more, The Better(树形dp+背包)

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6000    Accepted Submission(s): 3548 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻

URAL_1018 Binary Apple Tree 树形DP+背包

这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过程中,有点难表示转移 后来看了下大神的做法才知道其实可以用背包来模拟 树枝的去留,其实真的是个背包诶,每个子树枝就相当于物品,他占用了多少树枝量,带来多少的收益,就是用背包嘛,于是用树形DP+背包就可以做了 #include <iostream> #include <cstdio> #

POJ3345 Bribing FIPA

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5021   Accepted: 1574 Description There is going to be a voting at FIPA (Fédération Internationale de Programmation Association) to determine the host of the next IPWC (International Progra

hdu1561 树形dp+背包

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> using namespace std; #define ll long

P2015 二叉苹果树[树形dp+背包]

题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来描述一根树枝的位置.下面是一颗有4个树枝的树 2 5 \ / 3 4 \ / 1 现在这颗树枝条太多了,需要剪枝.但是一些树枝上长有苹果. 给定需要保留的树枝数量,求出最多能留住多少苹果. 解析 一道很简单的树形dp,然而我调了半天都没调出来,就是菜. 容易看出状态\(dp[x][i]\)表示以\(

hdu 1011 树形dp+背包

题意:有n个房间结构可看成一棵树,有m个士兵,从1号房间开始让士兵向相邻的房间出发,每个房间有一定的敌人,每个士兵可以对抗20个敌人,士兵在某个房间对抗敌人使无法走开,同时有一个价值,问你花费这m个士兵可以得到的最大价值是多少 分析:树形dp,对于点u,dp[u][j]表示以u为根的树消耗j个士兵得到的最大值,dp[i][j]=max(dp[i][j],dp[i][j-k]+dp[son][k]+val[u]) 注意是无向图,vis位置不能随便放,且注意dp不能直接+val,因为这样根节点就加不