POJ3345
http://poj.org/problem?id=3345
大意:
大意是说现在有n个城市来给你投票,你需要至少拿到m个城市的赞成票。想要获得第i个城市的赞成需要花费w[i],有个条件就是某些城市是在其他某个城市的统治下的,当获得某个城市的赞成票后,那么他所统治的其他城市都会投票给你(不再需要花费),球最小花费
题解:
我们很容易看出这个统治关系组成了一个树的结构,就等于拿到i整个子树的所有节点的花费就是节点i的花费,求最后拿到>=m个节点的最小花费
我的状态也是按照题目要求设的,F[i]表示到目前为止得到i个城市的赞成的最小花费,然后进行树上DP。
F[i] = min{F[i-num[u]] + w[u]}
其中u表示当前所处的节点,num[u]表示节点u子树上节点数量
这里这样DP是会有问题的,就是我们不能在子节点的F值算出来后再算父节点的F值,否则就可能存在用子节点选择了的状态更新父节点选择的状态,也就是说在DFS时,只能先算父节点的F值,再进行dfs算子节点的
另外这里的F[i-num[u]]实际上只能是由节点u的已经算出的兄弟节点推到(就是说在考虑节点u加入时,他的父节点是不能被加入了的)
上面两个要求统一起来就是说计算父节点的F值(吧父节点加入),子节点的不可以先被计算出;计算子节点的F值时,父节点不可以先加入。解决办法就是另外开一个数组G[i][j],用来临时存放节点i往下递归前的结果,回溯时再更新
上面的F,G对应下面的DP,DP2
dfs1用来计算num数组,dfs2是DP过程,复杂度O(n^2)
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e9
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } typedef __int64 LL;
//typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
const LL MOD = ; bool G[][];
map<string, int>M;
int cnt, w[], dp[], dp2[][], n, m;
char str[], s[];
int num[], tot; int get_index(char* s)
{
if(M[s]) return M[s];
return M[s] = ++cnt;
} void init()
{
int x, no;
cnt = ; M.clear();tot = ;
mem0(G); mem0(dp); mem0(num);
sscanf(s, "%d %d", &n, &m);
for(int i = ; i <= n; i ++) G[][i] = ;
for(int i = ; i <= n; i ++)
{
gets(s);
sscanf(s,"%s %d%*c", str, &x);
if( !M[str] ) M[str] = ++cnt;
w[no = M[str]] = x;
int len = strlen(s), p = len - , index = ;
while( !isdigit(s[p]) ) p --; p ++;
if(p == len) continue;
p++;
while(p <= len)
{
if(p == len || s[p] == ' ')
{
str[index] = ;
int id = get_index(str);
G[no][id] = ;
G[][id] = ;
index = ;
}
else str[index++] = s[p];
++p;
}
}
} void dfs1(int u)
{
dp[u] = INF;
num[u] = ;
for( int i = ; i <= n; i ++) if( G[u][i] )
{
dfs1(i);
num[u] += num[i];
}
dp[] = ;
} void dfs2(int u)
{
memcpy(dp2[u], dp, sizeof(dp));
if(u) for(int i = tot; i >= ; i --)
{
dp2[u][i + num[u]] = min(dp[i + num[u]], dp[i] + w[u]);
}
for(int i = ; i <= n; i ++) if(G[u][i])
dfs2(i);
for(int i = ; i <= n; i ++)
dp[i] = min(dp[i], dp2[u][i]);
tot ++;
} void print()
{
int ans = INF;
for(int i = m; i <= n; i ++)
ans = min(ans, dp[i]);
printf("%d\n", ans);
} int main()
{
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(gets(s) && s[] != '#')
{
init();
dfs1();
dfs2();
print();
}
return ;
}
POJ3345的更多相关文章
- poj3345 Bribing FIPA【树形DP】【背包】
Bribing FIPA Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5910 Accepted: 1850 Desc ...
- POJ3345 Bribing FIPA 【背包类树形dp】
题目链接 POJ 题解 背包树形dp板题 就是读入有点无聊,浪费了很多青春 #include<iostream> #include<cstdio> #include<cm ...
- POJ3345 Bribing FIPA(树形DP)
题意:有n个国家,贿赂它们都需要一定的代价,一个国家被贿赂了从属这个国家的国家也相当于被贿赂了,问贿赂至少k个国家的最少代价. 这些国家的从属关系形成一个森林,加个超级根连接,就是一棵树了,考虑用DP ...
- POJ3345 Bribing FIPA
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5021 Accepted: 1574 Description There ...
- POJ1155 TELE(树形DP)
题目是说给一棵树,叶子结点有负权,边有正权,问最多能选多少个叶子结点,使从叶子到根的权值和小于等于0. 考虑数据规模表示出状态:dp[u][k]表示在u结点为根的子树中选择k个叶子结点的最小权值 最后 ...
- ACM学习大纲
1 推荐题库 •http://ace.delos.com/usaco/ 美国的OI 题库,如果是刚入门的新手,可以尝试先把它刷通,能够学到几乎全部的基础算法极其优化,全部的题解及标程还有题目翻译可以b ...
- poj 3345 Bribing FIPA (树形背包dp | 输入坑)
题目链接: poj-3345 hdu-2415 题意 有n个国家,你要获取m个国家的支持,获取第i个国家的支持就要给cost[i]的价钱 其中有一些国家是老大和小弟的关系,也就是说,如果你获 ...
- ACM训练大纲
1. 算法总结及推荐题目 1.1 C++ STL • STL容器: set, map, vector, priority_queue, queue, stack, deque, bitset• STL ...
- 树形DP小结
树形DP1.简介:树是一种数据结构,因为树具有良好的子结构,而恰好DP是从最优子问题更新而来,那么在树上做DP操作就是从树的根节点开始深搜(也就是记忆化搜索),保存每一步的最优结果.tips:树的遍历 ...
随机推荐
- HDU 1231 最大连续子序列
和前面两道题一样 不过这题要求输出子序列首尾的元素的值,而且如果所有元素都小于0的话,规定子序列的和为0,并输出整个序列的首尾元素. //#define LOCAL #include <iost ...
- bzoj2982: combination
借(cao)鉴(xi)自popoqqq大爷的lucas定理的写法 #include<cstdio> #include<cstring> #include<cctype&g ...
- codeforces 430 A Points and Segments (easy)
题意:给出n个点,m个区间,需要给这些点涂上蓝色或者红色,使得每个区间里面的点的红色的点的个数与蓝色的点的个数的差值小于1 唉,题目的标题就标注了一个easy= = 最开始做的时候对点还有区间都排序了 ...
- wdcp v3 Forbidden :You don't have permission to access /phpmyadmin on this server
First edit the file /www/wdlinux/apache/conf/vhost/00000.default.conf and add the additional line to ...
- 百度编辑器解决span被过滤, 自动加P标签
editor_all.js: 自动加P标签去除: enterTag: 'p', 改成: enterTag: '', span被过滤: //从编辑器出去的内容处理 me.addOutputR ...
- (六)6.17 Neurons Networks convolutional neural network(cnn)
之前所讲的图像处理都是小 patchs ,比如28*28或者36*36之类,考虑如下情形,对于一副1000*1000的图像,即106,当隐层也有106节点时,那么W(1)的数量将达到1012级别,为了 ...
- 【自动化测试】关于UI自动化的疑问(记录ing)
1. 数据变动问题导致业务需要增加新的逻辑,这是增加case的健壮性还是浪费时间? 2. 如何做好PO? 不断数据抽离不断优化方法? 3. 如何提高调试代码的效率? /web可以 4. 主管不理解自动 ...
- 隐藏 php apache 的版本号
隐藏Apache版本信息 找到apache安装路径,譬如\Apache\conf\httpd.conf 修改httpd.conf ServerTokens ProductOnly ServerSign ...
- 【转】eclipse怎么设置字体大小
原文网址:http://jingyan.baidu.com/article/f96699bb9442f3894e3c1b15.html 1. 打开eclipse,找到window 2. 点击后在下拉 ...
- JBPM4入门——5.流程定义的发布、查询、删除
本博文只是简要对JBPM4进行介绍,如需更详细内容请自行google 链接: JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流 ...