Bribing FIPA
给出多棵有n个节点的有根树,第i个节点有一个权值\(a_i\),定义一个点能控制的点为其所有的子节点和它自己,询问选出若干个点的最少的权值之和,并且能够控制大于等于m个点,\(1 ≤ n ≤ 200\)
解
此题卡输入,插点题外话
c++语法讲解:
c++11将gets()函数弃用了,而代以更加安全的fgets(),原因为gets的读入为一定要读完一行,而实际上可以读的数据是无限的,而电脑的内存空间是有限的,用无限的数据去填满有限的内存,那是自然会死机的,于是fgets加了一个限制,一定要按你给的字符数组大小读入,自然不支持string。
fgets(char数组名,sizeof(char数组名),stdin)
streamstring是为了方便读入而引入的,比较老的c++版本是不会支持的,但放心的是oi肯定支持(类似一个类型)
用法(建议运行试一试)
参考代码
#include <iostream>
#include <cstdio>
#include <sstream>
using namespace std;
int main(){
char s[1000];
stringstream a;//a为变量名
//读入7 afsfs 0.125
fgets(s,sizeof(s),stdin);
a.clear(),a.str(s);//清空a,用s来填充a
int b;string c;double d;
a>>b,a>>c,a>>d;
cout<<b<<' '<<c<<' '<<d;
return 0;
}
- .clear()
清空状态标志,每次用完stringstream一定要清空,但未释放内存,可以做个小测试,打开内存显示,运行一下下面的程序,等待5s
#include <iostream>
#include <cstdio>
#include <sstream>
using namespace std;
int main(){
register stringstream io;
while(true)io<<"lsy akioi",io.clear();
return 0;
}
- .str()
用字符串s来重置stringstream,释放了内存
- \(>>,<<\)分别表示输入到和输出到
废话完结
回到题目,显然是树形递推,因为是森林,故用一个超级根0连上所有树根,变为一棵树,肯定设两维\(f[i][j]\)表示以i为节点的子树中,控制了j个点的选择权值之和的最小值,显然有
\]
其中l为i的一个子节点,k为其控制的点,也可以用分组背包来理解,i只不过代替不同的背包,j是容量,而子树就是每一组物品,k就是物品,只是压了子树这一维状态,设\(s[x]\)为x的子树中节点个数。
边界:\(f[x][0]=0,f[x][s[x]]=a_x,x=0,1,2,3,...,n\)特别地\(a_0=INT\_MAX\)。
参考代码:
#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <sstream>
#define il inline
#define ri register
#define lsytql true
#define intmax 0x7fffffff
using namespace std;
struct point{
int next,to;
}ar[401];
bool check[201];
int c[201],at,head[201],
in[201],dp[201][201],
st[201],mt,n,m;
char s[30000];
stringstream oi;
map<string,int>M;
void dfs(int);
il int min(int,int);
il void link(int,int);
int main(){
while(true){
mt&=0,M.clear(),memset(head,0,sizeof(head)),at&=0;
memset(in,0,sizeof(in)),memset(check,0,sizeof(check));
memset(dp,2,sizeof(dp)),memset(st,0,sizeof(st));
fgets(s,sizeof(s),stdin),oi.clear(),oi.str(s),oi>>n>>m;
if(s[0]=='#')break;for(int i(1),j;i<=n;++i){
fgets(s,sizeof(s),stdin);
oi.clear(),oi.str(s),oi>>s;
if(M.find(s)==M.end())M[s]=++mt;
j=M[s],oi>>c[j];
while(oi>>s){
if(M.find(s)==M.end())M[s]=++mt;
link(j,M[s]),++in[M[s]];
}
}int ans(intmax);
for(int i(1);i<=n;++i)if(!in[i])link(0,i);dfs(0);
for(int i(m);i<=n;++i)if(dp[0][i]<ans)ans=dp[0][i];
printf("%d\n",ans);
}
return 0;
}
il int min(int a,int b){
return a<b?a:b;
}
void dfs(int x){
check[x]|=true,dp[x][0]=0;
for(int i(head[x]);i;i=ar[i].next)
if(!check[ar[i].to]){
dfs(ar[i].to),st[x]+=st[ar[i].to];
for(int j(n),k;j>0;--j)
for(k=1;k<=j;++k)
dp[x][j]=min(dp[x][j],dp[x][j-k]+dp[ar[i].to][k]);
}++st[x],dp[x][st[x]]=min(dp[x][st[x]],c[x]);
}
il void link(int u,int v){
ar[++at].to=v;
ar[at].next=head[u],head[u]=at;
}
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 ...
- HDU 2415 Bribing FIPA
Bribing FIPA Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...
- 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大全(转)
动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...
随机推荐
- IDEA Caused by: java.lang.OutOfMemoryError: PermGen space
错误:OutOfMemoryError: PermGen space 非堆溢出(永久保存区域溢出) 解决方法: 在Run/Debug configuration 的你要运行行的tomcat里面的 vm ...
- 前端用vue,后端需要nodejs吗?
其实用脚趾头想想也知道:后端用不用node,和前端用不用vue,二者完全没关系. 那为何用vue和vue插件开发前端的时候,经常需要各种npm install呢?为何要在本地安装node环境? 本地需 ...
- js 单行间隙滚动
代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...
- 新建门脸Facade类
1.App\Contract目录下新建 CommonContract 类 <?php namespace App\Contract; use Carbon\Carbon; use \Dimsav ...
- Buffering Data
We keep telling you that you always need to close your files after you're done writing to them. Here ...
- 如何修改magento分类页面的产品的显示个数
经常的有客户问,怎么修改分类页面的产品的个数 这个是magneto后台操作的设置问题 打开后台,在英文状态下: system-->configuration 进入后,点击catalog Prod ...
- CentOS7编译安装MPLAYER!!!
Linux装软件就是折磨人!! Mplayer官网下好release版本 然后./configure --[options] 注意:--prefix=/usr/local/mplayer 是安装路径- ...
- (转)Linux C 多线程编程----互斥锁与条件变量
转:http://blog.csdn.net/xing_hao/article/details/6626223 一.互斥锁 互斥量从本质上说就是一把锁, 提供对共享资源的保护访问. 1. 初始化: 在 ...
- Idea 导入项目不能运行
1.项目结构里面配置sdk,配置output输出目录 2.配置语言等级 配置src源文件目录 配置目录里面添加application,添加main class
- HA配置
主T800 eth0 192.168.2.32备T600 eth1 192.168.2.33安装nginx yum install -y nginx 关闭主备的防火墙iptables.selinux ...