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)

  1. #include <map>
  2. #include <set>
  3. #include <stack>
  4. #include <queue>
  5. #include <cmath>
  6. #include <ctime>
  7. #include <vector>
  8. #include <cstdio>
  9. #include <cctype>
  10. #include <cstring>
  11. #include <cstdlib>
  12. #include <iostream>
  13. #include <algorithm>
  14. using namespace std;
  15. #define INF 1e9
  16. #define inf (-((LL)1<<40))
  17. #define lson k<<1, L, mid
  18. #define rson k<<1|1, mid+1, R
  19. #define mem0(a) memset(a,0,sizeof(a))
  20. #define mem1(a) memset(a,-1,sizeof(a))
  21. #define mem(a, b) memset(a, b, sizeof(a))
  22. #define FOPENIN(IN) freopen(IN, "r", stdin)
  23. #define FOPENOUT(OUT) freopen(OUT, "w", stdout)
  24. #define rep(i, a, b) for(int i = a; i <= b; i ++)
  25. template<class T> T CMP_MIN(T a, T b) { return a < b; }
  26. template<class T> T CMP_MAX(T a, T b) { return a > b; }
  27. template<class T> T MAX(T a, T b) { return a > b ? a : b; }
  28. template<class T> T MIN(T a, T b) { return a < b ? a : b; }
  29. template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
  30. template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; }
  31.  
  32. typedef __int64 LL;
  33. //typedef long long LL;
  34. const int MAXN = ;
  35. const int MAXM = ;
  36. const double eps = 1e-;
  37. const LL MOD = ;
  38.  
  39. bool G[][];
  40. map<string, int>M;
  41. int cnt, w[], dp[], dp2[][], n, m;
  42. char str[], s[];
  43. int num[], tot;
  44.  
  45. int get_index(char* s)
  46. {
  47. if(M[s]) return M[s];
  48. return M[s] = ++cnt;
  49. }
  50.  
  51. void init()
  52. {
  53. int x, no;
  54. cnt = ; M.clear();tot = ;
  55. mem0(G); mem0(dp); mem0(num);
  56. sscanf(s, "%d %d", &n, &m);
  57. for(int i = ; i <= n; i ++) G[][i] = ;
  58. for(int i = ; i <= n; i ++)
  59. {
  60. gets(s);
  61. sscanf(s,"%s %d%*c", str, &x);
  62. if( !M[str] ) M[str] = ++cnt;
  63. w[no = M[str]] = x;
  64. int len = strlen(s), p = len - , index = ;
  65. while( !isdigit(s[p]) ) p --; p ++;
  66. if(p == len) continue;
  67. p++;
  68. while(p <= len)
  69. {
  70. if(p == len || s[p] == ' ')
  71. {
  72. str[index] = ;
  73. int id = get_index(str);
  74. G[no][id] = ;
  75. G[][id] = ;
  76. index = ;
  77. }
  78. else str[index++] = s[p];
  79. ++p;
  80. }
  81. }
  82. }
  83.  
  84. void dfs1(int u)
  85. {
  86. dp[u] = INF;
  87. num[u] = ;
  88. for( int i = ; i <= n; i ++) if( G[u][i] )
  89. {
  90. dfs1(i);
  91. num[u] += num[i];
  92. }
  93. dp[] = ;
  94. }
  95.  
  96. void dfs2(int u)
  97. {
  98. memcpy(dp2[u], dp, sizeof(dp));
  99. if(u) for(int i = tot; i >= ; i --)
  100. {
  101. dp2[u][i + num[u]] = min(dp[i + num[u]], dp[i] + w[u]);
  102. }
  103. for(int i = ; i <= n; i ++) if(G[u][i])
  104. dfs2(i);
  105. for(int i = ; i <= n; i ++)
  106. dp[i] = min(dp[i], dp2[u][i]);
  107. tot ++;
  108. }
  109.  
  110. void print()
  111. {
  112. int ans = INF;
  113. for(int i = m; i <= n; i ++)
  114. ans = min(ans, dp[i]);
  115. printf("%d\n", ans);
  116. }
  117.  
  118. int main()
  119. {
  120. // freopen("in.txt", "r", stdin);
  121. // freopen("out.txt", "w", stdout);
  122. while(gets(s) && s[] != '#')
  123. {
  124. init();
  125. dfs1();
  126. dfs2();
  127. print();
  128. }
  129. return ;
  130. }

POJ3345的更多相关文章

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

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

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

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

  3. POJ3345 Bribing FIPA(树形DP)

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

  4. POJ3345 Bribing FIPA

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

  5. POJ1155 TELE(树形DP)

    题目是说给一棵树,叶子结点有负权,边有正权,问最多能选多少个叶子结点,使从叶子到根的权值和小于等于0. 考虑数据规模表示出状态:dp[u][k]表示在u结点为根的子树中选择k个叶子结点的最小权值 最后 ...

  6. ACM学习大纲

    1 推荐题库 •http://ace.delos.com/usaco/ 美国的OI 题库,如果是刚入门的新手,可以尝试先把它刷通,能够学到几乎全部的基础算法极其优化,全部的题解及标程还有题目翻译可以b ...

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

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

  8. ACM训练大纲

    1. 算法总结及推荐题目 1.1 C++ STL • STL容器: set, map, vector, priority_queue, queue, stack, deque, bitset• STL ...

  9. 树形DP小结

    树形DP1.简介:树是一种数据结构,因为树具有良好的子结构,而恰好DP是从最优子问题更新而来,那么在树上做DP操作就是从树的根节点开始深搜(也就是记忆化搜索),保存每一步的最优结果.tips:树的遍历 ...

随机推荐

  1. 20160206.CCPP体系详解(0016天)

    代码片段(01):.指针.c+02.间接赋值.c 内容概要:内存 ///01.指针 #include <stdio.h> #include <stdlib.h> //01.取地 ...

  2. Linux编程(获取系统时间)

    #include <stdio.h> #include <time.h> int main() { time_t now; struct tm *w; time(&no ...

  3. js match regex

    需要返回成数组元素的要放在括号里头 var arr = /input-([0-9]*)-([0-9]*)/.exec(id); var all = arr[0]; var row = arr[1]; ...

  4. ASIFormDataRequest 登录

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString: @"http: ...

  5. ubuntu 查看系统版本信息

    查看cpu信息cat /proc/cpiinfo 查看ubuntu版本:cat /etc/issue 查看系统是32位还是64位方法1:#查看long的位数,返回32或64 getconf LONG_ ...

  6. 将错误日志记录在txt文本里

    引言 对于已经部署的系统一旦出错对于我们开发人员来说是比较痛苦的事情,因为我们不能跟踪到错误信息,不能 很快的定位到我们的错误位置在哪,这时候如果能像开发环境一样记录一些堆栈信息就可以了,这时候我们就 ...

  7. Android Ant批量打包

    一.配置Ant环境变量 JAVA_HOME=/software/jdk1.6.0_24 ANT_HOME=/software/apache-ant-1.9.2 Android_Home=/softwa ...

  8. 对JAVA集合进行遍历删除时务必要用迭代器

    java集合遍历删除的方法: 1.当然这种情况也是容易解决,实现方式就是讲遍历与移除操作分离,即在遍历的过程中,将需要移除的数据存放在另外一个集合当中,遍历结束之后,统一移除. 2.使用Iterato ...

  9. 【LR】安装LR11后遇到的问题

    (1)问题:录制脚本时无法弹出IE浏览器 解决方法: 正确的是C:\Program Files (x86)\Internet Explorer\iexplore.exe 错误是:C:\Program ...

  10. list 容器 排序函数.xml

    pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...