POJ 1947 Rebuilding Roads 树形DP(背包)
来源:互联网 发布:四个字母cn未注册域名 编辑:程序博客网 时间:2024/05/18 09:18
原题:
Rebuilding Roads
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8567 Accepted: 3836
Description
The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows didn't have time to rebuild any extra roads, so now there is exactly one way to get from any given barn to any other barn. Thus, the farm transportation system can be represented as a tree.
Farmer John wants to know how much damage another earthquake could do. He wants to know the minimum number of roads whose destruction would isolate a subtree of exactly P (1 <= P <= N) barns from the rest of the barns.
Farmer John wants to know how much damage another earthquake could do. He wants to know the minimum number of roads whose destruction would isolate a subtree of exactly P (1 <= P <= N) barns from the rest of the barns.
Input
* Line 1: Two integers, N and P
* Lines 2..N: N-1 lines, each with two integers I and J. Node I is node J's parent in the tree of roads.
* Lines 2..N: N-1 lines, each with two integers I and J. Node I is node J's parent in the tree of roads.
Output
A single line containing the integer that is the minimum number of roads that need to be destroyed for a subtree of P nodes to be isolated.
Sample Input
11 61 21 31 41 52 62 72 84 94 104 11
Sample Output
2
Hint
[A subtree with nodes (1, 2, 3, 6, 7, 8) will become isolated if roads 1-4 and 1-5 are destroyed.]
题意+题解:
题意是说给一棵树,如何删除尽量少的边,使得剩下的子树中恰好有P个点。
一道比较基础的树形背包,由于最后的结果一定是以某个结点为根的树干部分
定义状态: dp[i][j]表示在以结点i为根的树中,分配j个结点的指标,所需要删除的最少边【一定包含第i个结点】
p[u][j]= min(dp[u][j],dp[son][k]+dp[u][j-k]);
表明 给结点u分配j的容量, u再把这j个分配给他的各个儿子结点,给当前儿子结点分配k个容量,加上 之前遍历过的所有儿子结点中分配j-k个容量,看这个是否更优。
注意这里 j是从大往小的遍历,利用背包缩小空间维度的优化方法,这里右边min函数中的两个dp[u][]表示的是上一轮中扫过结点u中从0到I-1个儿子结点所得到的dp值,而左边的dp[u][j]表示的是包括的这次第i个儿子结点后的dp值
另外就是要注意循环的边界条件,每次根节点必占一个容量。
初始化时,dp[u][1]= u的儿子个数 也很好理解
代码:
#include<cstdio>#include<cstring> #include<vector>#include<algorithm>using namespace std;vector <int>T[155];int dp[151][151];int parent[151];int n,p;int find_min(int a,int b){return a<b?a:b;}void dfs(int u){dp[u][1]=T[u].size();if(T[u].empty())return ;int i,son,j,k,num=T[u].size();for ( i = 0; i < num; i++){son=T[u][i];if (parent[son]==u){dfs(son);for ( j = p; j >=1 ; j--){for ( k = 0; k<=j-1; k++){dp[u][j]=find_min(dp[u][j],dp[son][k]+dp[u][j-k]);}}}}}int main(){int i,father,son,root,j,ans;while (scanf("%d%d",&n,&p)!=EOF){ans=9999999;memset(parent,0,sizeof(parent));for ( i = 1; i <=n ; i++){dp[i][0]=0;for ( j = 1; j <=p ; j++){dp[i][j]=9999999;}}for ( i = 1; i <=n ; i++){T[i].clear();}for ( i = 1; i <=n-1 ; i++){scanf("%d%d",&father,&son);T[father].push_back(son);T[son].push_back(father);parent[son]=father;}for ( i = 1; i <=n ; i++){if (parent[i]==0){root=i;break;}}dfs(root);for ( i = 1; i <n ; i++){if (dp[i][p]<ans){ans=dp[i][p];}}if (n==1)printf("0\n");else printf("%d\n",ans-2*(p-1));}return 0;}
0 0
- poj 1947 Rebuilding Roads (树形背包dp)
- POJ 1947 Rebuilding Roads 树形DP(背包)
- poj 1947 Rebuilding Roads (树形dp+背包)
- poj 1947 Rebuilding Roads 树形dp背包
- poj 1947Rebuilding Roads树形dp(背包)
- poj 1947 Rebuilding Roads 树形dp加背包
- POJ 1947 Rebuilding Roads(树形DP + 01背包)
- POJ 1947 Rebuilding Roads (树形dp + 01背包)
- POJ 1947 Rebuilding Roads 树形dp+01背包
- POJ 1947 Rebuilding Roads 树形dp+01背包
- poj 1947 Rebuilding Roads 树形背包 ★
- poj 1947 Rebuilding Roads (树形背包)
- POJ--1947[Rebuilding Roads] 树形DP
- POJ 1947 Rebuilding Roads 树形DP
- POJ 1947 Rebuilding Roads 树形dp
- poj 1947 Rebuilding Roads 树形dp
- poj 1947 Rebuilding Roads(树形DP)
- POJ--1947--Rebuilding Roads--树形DP
- Java配置properties文件的加载和读取方法
- PMON 2000启动配置文档说明
- Linux设备驱动模型 .
- hdu 4512 最长公共上升子序列
- linux中断API
- POJ 1947 Rebuilding Roads 树形DP(背包)
- Launch Batch Scripts as Administrator (with GUI UAC prompt)
- Hive性能调校
- Android开发规范详解
- Java调用oracle存储过程通过游标返回临时表数据
- 字典和列表的区别
- 在Java中执行脚本命令以及调用外部程序说明
- 黑马程序员--基础加强--第八篇--代理
- Svn安装和Svn支持http协议配置。