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 ...
随机推荐
- Windows 驱动开发 - 8
最后的一点开发工作:跟踪驱动. 一.驱动跟踪 1. 包括TMH头文件 #include "step5.tmh" 2. 初始化跟踪 在DriverEntry中初始化. WPP_INI ...
- extjs的formpanel横向滚动条的解决办法
关于formpanel设置autoscroll:true同时出现横向和纵向滚动条的解决办法: 不要设置autoscroll属性,或者autoscroll:false,然后设置bodyStyle : ' ...
- js简单函数封装
//每index个字符插入一个str字符串 String.prototype.insertStrPerIndex =function(index,str){ if(this.length>ind ...
- Linux C语言头文件搜索路径
本文介绍在linux中头文件的搜索路径,也就是说你通过include指定的头文件,linux下的gcc编译器它是怎么找到它的呢.在此之前,先了解一个基本概念. 头文件是一种文本文件,使用文本编辑器将代 ...
- SQL Server2012 T-SQL基础教程--读书笔记(5-7章)
SQL Server2012 T-SQL基础教程--读书笔记(5-7章) SqlServer T-SQL 示例数据库:点我 Chapter 05 表表达式 5.1 派生表 5.1.1 分配列别名 5. ...
- go之for循环
一.基于计数器的迭代 格式 for 初始化语句; 条件语句; 修饰语句{} 实例 package main import "fmt" func main(){ for i:=0;i ...
- 基于CGAL的Delaunay三角网应用
目录 1. 背景 1.1 CGAL 1.2 cgal-bindings(Python包) 1.3 vtk-python 1.4 PyQt5 2. 功能设计 2.1 基本目标 2.2 待实现目标 3. ...
- B - Is your horseshoe on the other hoof?
Problem description Valera the Horse is going to the party with friends. He has been following the f ...
- C#之纯数字判断
public bool isNaN(string temp) { ; i <temp.Length; i++) { byte tempByte = Convert.ToByte(temp[i]) ...
- C#三种创建对象方法所需时间比较。。。。。
C#创建对象的三种方法 new().Activator.Assembly,接下来通过代码直接来看看运行的速度.... 首先,先看看三种创建对象实例的方法: //new(); public stati ...