您的位置:首页 > 其它

poj 3345 Bribing FIPA 【树形dp + 01背包】

2015-08-13 21:34 423 查看
Bribing FIPA

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 4274Accepted: 1337
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

RE一天了,最后才发现原来映射fp忘记初始化了,想哭。
题意:有N个国家,你想要用收买M个国家为你投票,现在已经给出收买第i个国家代价val[i]。但有些国家之间存在从属关系,如果B从属于A国,则收买了A也意味着买通了B,而且这些关系是传递的。问你最小要付出的代价是多少?

分析:典型的树形dp + 01背包,感觉主要在于处理字符串问题,毕竟模拟比较弱。。。

思路:把所有国家虚拟为节点。用dp[i][j]表示以第i节点为根的子树 选择j个点的最小代价。
则状态转移方程为:dp[u][j] = min(dp[u][j], dp[u][j-k] + dp[v][k])。

AC代码:
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define MAXN 200+10
#define INF 0x3f3f3f3f
using namespace std;
int dp[MAXN][MAXN];
int N, M;
int pre[MAXN];
vector<int> G[MAXN];
map<string, int> fp;
char a[11000], b[11000];
int k;
void init()
{
    for(int i = 1; i <= N; i++)
        G[i].clear(), pre[i] = i;
    G[0].clear();
}
int val[MAXN];
int num[MAXN];//统计节点数
void DFS(int u)
{
    num[u] = 1;//统计树的节点数目
    dp[u][0] = 0;//初始不选 根
    for(int i = 0; i < G[u].size(); i++)
    {
        int v = G[u][i];
        DFS(v);
        num[u] += num[v];
        for(int j = num[u]; j >= 1; j--)//u节点下面 共num[u]个节点
        {
            for(int k = 0; k <= num[v] && k <= j; k++)//v节点下面 共num[v]个节点
                dp[u][j] = min(dp[u][j], dp[u][j - k] + dp[v][k]);
                //犯二了,写成dp[u][v]了。。。
        }
    }
    for(int i = 1; i <= num[u]; i++)//更新
        dp[u][i] = min(dp[u][i], val[u]);
}
void solve()//输入好恶心
{
    scanf("%d", &M);
    k = 0;
    int x, y, v;
    fp.clear();
    for(int i = 1; i <= N; i++)
    {
        scanf("%s%d", a, &v);
        if(!fp[a]) fp[a] = ++k;
        x = fp[a];
        val[x] = v;
        while(getchar() != '\n')
        {
            scanf("%s", b);
            if(!fp[b]) fp[b] = ++k;
            y = fp[b];
            G[x].push_back(y);
            pre[y] = x;
        }
    }
    for(int i = 1; i <= N; i++)
    {
        if(pre[i] == i)
            G[0].push_back(i);
    }
    val[0] = INF;//注意这里
    memset(dp, INF, sizeof(dp));
    DFS(0);
    printf("%d\n", dp[0][M]);
}
int main()
{
    while(scanf("%s", a), strcmp(a, "#"))
    {
        N = (int)atof(a);
        init();
        solve();
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: