HDU 2415 Bribing FIPA
Bribing FIPA
This problem will be judged on HDU. Original ID: 2415
64-bit integer IO format: %I64d Java class name: Main
Input
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
Sample Input
3 2
Aland 10
Boland 20 Aland
Coland 15
#
Sample Output
20
Source
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
unordered_map<string,int>ump;
vector<int>g[maxn];
int n,m,w[maxn],son[maxn],dp[maxn][maxn];
bool in[maxn];
void dfs(int u){
dp[u][] = ;
son[u] = ;
for(int i = g[u].size()-; i >= ; --i){
dfs(g[u][i]);
son[u] += son[g[u][i]];
for(int j = son[u]; j >= ; --j)
for(int k = ; k <= j && k <= son[g[u][i]]; ++k)
dp[u][j] = min(dp[u][j],dp[u][j-k] + dp[g[u][i]][k]);
}
dp[u][son[u]] = min(dp[u][son[u]],w[u]);
}
int main(){
char str[];
int id = ,tmp,u,v;
while(gets(str) && str[] != '#'){
ump.clear();
memset(in,false,sizeof in);
for(int i = ; i < maxn; ++i) g[i].clear();
sscanf(str,"%d%d",&n,&m);
for(int i = id = ; i <= n; ++i){
scanf("%s%d",str,&tmp);
if(!(u = ump[str])) u = ump[str] = id++;
w[u] = tmp;
while(getchar() != '\n'){
scanf("%s",str);
if(!(v = ump[str])) v = ump[str] = id++;
in[v] = true;
g[u].push_back(v);
}
}
w[] = INF;
for(int i = ; i < id; ++i)
if(!in[i]) g[].push_back(i);
memset(dp,0x3f,sizeof dp);
dfs();
int ret = INF;
for(int i = m; i <= n; ++i)
ret = min(ret,dp[][i]);
printf("%d\n",ret);
}
return ;
}
HDU 2415 Bribing FIPA的更多相关文章
- POJ 3345 Bribing FIPA 树形DP
题目链接: POJ 3345 Bribing FIPA 题意: 一个国家要参加一个国际组织, 需要n个国家投票, n个国家中有控制和被控制的关系, 形成了一颗树. 比如: 国家C被国家B控制, 国 ...
- poj3345 Bribing FIPA【树形DP】【背包】
Bribing FIPA Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5910 Accepted: 1850 Desc ...
- Bribing FIPA
Bribing FIPA 给出多棵有n个节点的有根树,第i个节点有一个权值\(a_i\),定义一个点能控制的点为其所有的子节点和它自己,询问选出若干个点的最少的权值之和,并且能够控制大于等于m个点,\ ...
- POJ3345 Bribing FIPA
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5021 Accepted: 1574 Description There ...
- poj 3345 Bribing FIPA (树形背包dp | 输入坑)
题目链接: poj-3345 hdu-2415 题意 有n个国家,你要获取m个国家的支持,获取第i个国家的支持就要给cost[i]的价钱 其中有一些国家是老大和小弟的关系,也就是说,如果你获 ...
- POJ3345 Bribing FIPA 【背包类树形dp】
题目链接 POJ 题解 背包树形dp板题 就是读入有点无聊,浪费了很多青春 #include<iostream> #include<cstdio> #include<cm ...
- POJ3345 Bribing FIPA(树形DP)
题意:有n个国家,贿赂它们都需要一定的代价,一个国家被贿赂了从属这个国家的国家也相当于被贿赂了,问贿赂至少k个国家的最少代价. 这些国家的从属关系形成一个森林,加个超级根连接,就是一棵树了,考虑用DP ...
- [POJ 3345] Bribing FIPA
[题目链接] http://poj.org/problem?id=3345 [算法] 树形背包 [代码] #include <algorithm> #include <bitset& ...
- 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】
树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...
随机推荐
- SQL server触发器、存储过程操作远程数据库插入数据,解决服务器已存在的问题
近期弄了一个小项目,也不是非常复杂,须要将一个数据库的一些数据备份到另外一个库.不是本地,可能是网络上其它的数据库.想了一下,用了存储过程和触发器. 也不是非常复杂,首先我须要操作远程数据库,于是写了 ...
- Mongo-JS-导出数据为insert语句
======2014-11-14 数据提取======= [query@query-server hurl]$ cat test.sh tlist=`cat ./username.txt`; for ...
- bzoj5130: [Lydsy1712月赛]字符串的周期
这道题很有意思啊. 字符串循环节用KMP(手推一下) 假如是26^12肯定很不滋磁 但是可以发现ABA和BCB和BAB这些都是等价的 那就把最小的拿出来搞再乘个排列数就好了 #include<c ...
- pandas把多个sheet读进一个DataFrame
#!/usr/bin/python import pandas as pd import collections df_dict = pd.read_excel('c:\data\machines.x ...
- string 类型的翻转
#include <string>#include <iostream>#include <stack> int main() { std::string str= ...
- diaowen Maven Webapp
五月 , :: 上午 org.apache.catalina.startup.VersionLoggerListener log INFO: Server version: Apache Tomcat ...
- buf.readInt32LE函数详解
offset {Number} 0 noAssert {Boolean} 默认:false 返回:{Number} 从该 Buffer 指定的带有特定尾数格式(readInt32BE() 返回一个较大 ...
- 【技术累积】【点】【git】【10】.gitignore和.gitattributes
.gitignore 告诉git忽略一些文件,git status会显示不到这些文件的状态. 一般放在项目根目录,以对全局控制,当然可以放在module下: 具体规则主要是: 以行为单位定义忽略文件类 ...
- react基础篇三
事件处理 React事件绑定属性的命名采用驼峰式写法,而不是小写. 如果采用 JSX 的语法你需要传入一个函数作为事件处理函数,而不是一个字符串(DOM元素的写法) 例如,传统的 HTML: < ...
- fileupload 上传控件
<div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:But ...