[题目链接]

http://poj.org/problem?id=3345

[算法]

树形背包

[代码]

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 210
const int INF = 2e9; int i,j,k,root,n,m,tot,u,v,x,ans;
int size[MAXN],fa[MAXN],val[MAXN];
int f[MAXN][MAXN];
char s[MAXN];
map< string,int > mp;
vector< int > e[MAXN]; inline void dp(int u)
{
int i,j,k,v;
f[u][] = ;
size[u] = ;
for (i = ; i < (int)e[u].size(); i++)
{
v = e[u][i];
dp(v);
size[u] += size[v];
for (j = size[u]; j >= ; j--)
{
for (k = ; k <= size[v]; k++)
{
f[u][j] = min(f[u][j],f[u][j - k] + f[v][k]);
}
}
}
if (u != root) f[u][size[u]] = min(f[u][size[u]],val[u]);
} int main()
{ while (scanf("%s",&s) != EOF && strcmp(s,"#") != )
{
scanf("%d",&m);
n = ;
for (i = ; s[i] != '\0'; i++) n = n * + s[i] - '';
tot = ;
mp.clear();
memset(val,,sizeof(val));
memset(fa,,sizeof(fa));
for (i = ; i <= n; i++) e[i].clear();
for (i = ; i <= n; i++)
{
scanf("%s%d",&s,&x);
if (!mp[s]) mp[s] = ++tot;
u = mp[s];
val[u] = x;
while (getchar() != '\n')
{
scanf("%s",&s);
if (!mp[s]) mp[s] = ++tot;
v = mp[s];
e[u].push_back(v);
fa[v] = u;
}
}
root = ;
for (i = ; i <= n; i++)
{
if (!fa[i])
e[root].push_back(i);
}
memset(size,,sizeof(size));
memset(f,0x3f,sizeof(f));
dp(root);
ans = INF;
for (i = m; i <= n; i++) ans = min(ans,f[][i]);
printf("%d\n",ans);
} return ; }

[POJ 3345] Bribing FIPA的更多相关文章

  1. POJ 3345 Bribing FIPA 树形DP

    题目链接: POJ 3345 Bribing FIPA 题意: 一个国家要参加一个国际组织,  需要n个国家投票,  n个国家中有控制和被控制的关系, 形成了一颗树. 比如: 国家C被国家B控制, 国 ...

  2. poj 3345 Bribing FIPA (树形背包dp | 输入坑)

    题目链接:  poj-3345  hdu-2415 题意 有n个国家,你要获取m个国家的支持,获取第i个国家的支持就要给cost[i]的价钱    其中有一些国家是老大和小弟的关系,也就是说,如果你获 ...

  3. poj3345 Bribing FIPA【树形DP】【背包】

    Bribing FIPA Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5910   Accepted: 1850 Desc ...

  4. HDU 2415 Bribing FIPA

    Bribing FIPA Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...

  5. Bribing FIPA

    Bribing FIPA 给出多棵有n个节点的有根树,第i个节点有一个权值\(a_i\),定义一个点能控制的点为其所有的子节点和它自己,询问选出若干个点的最少的权值之和,并且能够控制大于等于m个点,\ ...

  6. POJ3345 Bribing FIPA

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5021   Accepted: 1574 Description There ...

  7. POJ3345 Bribing FIPA 【背包类树形dp】

    题目链接 POJ 题解 背包树形dp板题 就是读入有点无聊,浪费了很多青春 #include<iostream> #include<cstdio> #include<cm ...

  8. poj 3345 树形DP 附属关系+输入输出(好题)

    题目连接:http://acm.hust.edu.cn/vjudge/problem/17665 参考资料:http://blog.csdn.net/woshi250hua/article/detai ...

  9. POJ3345 Bribing FIPA(树形DP)

    题意:有n个国家,贿赂它们都需要一定的代价,一个国家被贿赂了从属这个国家的国家也相当于被贿赂了,问贿赂至少k个国家的最少代价. 这些国家的从属关系形成一个森林,加个超级根连接,就是一棵树了,考虑用DP ...

随机推荐

  1. GitHub上fork别人打代码后如何保持和原作者同步的更新

    1.进入你的GitHub发起Pull  request 2.选择compare across  forks 3.反向操作.base fork改为自己的,head fork改为原作者的 4.点击 cre ...

  2. ASP.NET刷新页面的六种方法

    第一: private void Button1_Click( object sender, System.EventArgs e )   {      Response.Redirect( Requ ...

  3. 三维重建:SLAM相关的一些术语解释

    SLAM是一个工程问题,再次复习一下工程中可能用到的名词解释. 还是不要看了,高翔的科普读物已经出版了,读他的<slam十四讲>就可以了. 一.度量相关: 世界坐标系:描述图像的平面坐标系 ...

  4. (转)Arcgis for Js之鼠标经过显示对象名的实现

    http://blog.csdn.net/gisshixisheng/article/details/41889345 在浏览地图时,移动鼠标经过某个对象或者POI的时候,能够提示该对象的名称对用户来 ...

  5. C# tostring("0000000")

    public string ConverNo(string str) { string result = ""; ]; ; i < chars.Length; i++) ch ...

  6. python 处理中文 读取数据库输出全是问号

    ref:http://www.cnblogs.com/zhoujie/archive/2013/06/07/problem1.html 1.python连接mssql数据库编码问题 python一直对 ...

  7. 前端工具gulp2

    var gulp = require('gulp'); var less = require('gulp-less'); var htmlmin = require('gulp-htmlmin'); ...

  8. https证书安装无效的主要原因

    https证书的作用是为了确认服务端身份,但网络上充满了无效的证书,浏览器对使用无效证书的访问,给出危险.不安全警告,将是否选择继续访问由用户选择,而大多数用户是无法区分这是配置还是真的存在安全问题. ...

  9. PAT_A1128#N Queens Puzzle

    Source: PAT A1128 N Queens Puzzle (20 分) Description: The "eight queens puzzle" is the pro ...

  10. spotlight on mysql 监控

    . 安装 下载地址:https://pan.baidu.com/s/1qYi3lec 官网地址——https://www.quest.com/common/registration.aspx?requ ...