POJ 3345 Bribing FIPA(树形DP)


很好的树形DP题目,借鉴了http://www.cppblog.com/Yuan/archive/2010/04/28/113833.html
以后有待于研究下红黑树了

#include
<cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <map> #include <string> #include <sstream> using namespace std; #define min(a,b) (((a) < (b)) ? (a) : (b)) const int MAXN = 210; const int INF = 1e9; vector<int> tree[MAXN]; int dp[MAXN][MAXN]; // dp[u][m] 节点u选择m个国家所花费的最小代价 int cost[MAXN], node[MAXN]; // node[i] = 1 represent i is the node of the tree, 0 represent the root of the tree int n, m; int dfs(int u) { int child = 1; for (int j = 1; j <= n; ++j) dp[u][j] = INF; dp[u][0] = 0; for (int i = 0; i < tree[u].size(); ++i) { int v = tree[u][i]; child += dfs(v); for (int j = n; j > 0; --j) for (int k = 0; k <= j; ++k) dp[u][j] = min(dp[u][j], dp[u][j-k] + dp[v][k]); } dp[u][child] = min(dp[u][child], cost[u]); return child; } int main() { char str[1000]; while (gets(str) && str[0] != '#') { sscanf(str, "%d %d", &n, &m); for (int i = 0; i <= n; ++i) tree[i].clear(); map<string, int> mapidx; int id = 0; memset(cost, 0, sizeof(cost)); memset(node, 0, sizeof(node)); for (int i = 1; i <= n; ++i) { scanf("%s", str); if (mapidx.find(str) == mapidx.end()) mapidx[str] = ++id; int u = mapidx[str]; scanf("%d", &cost[u]); gets(str); stringstream s(str); string name; while (s >> name) { if (mapidx.find(name) == mapidx.end()) mapidx[name] = ++id; tree[u].push_back(mapidx[name]); ++node[mapidx[name]]; } } cost[0] = INF; for (int i = 1; i <= n; ++i) if (!node[i]) tree[0].push_back(i); // tree[0] is the root dfs(0); int ans = INF; for (int j = m; j <= n; ++j) if (dp[0][j] < ans) ans = dp[0][j]; printf("%d\n", ans); } return 0; }
优质内容筛选与推荐>>
1、Welcoming you to Zoho Invoice!
2、Java基础知识强化之多线程笔记05:Java中继承thread类 与 实现Runnable接口的区别
3、关于加载font-awesome文字显示不出来
4、让心灵搬家...
5、C#程序设计中特性详细介绍


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号