hdu4340
来源:互联网 发布:嵌入式要学java吗 编辑:程序博客网 时间:2024/05/20 01:47
Capturing a country
Ant and Bob two army want to capture a country. The country is consist of N cities. To capture the city i, it takes Ant A[i] minutes, and Bob needs B[i] minutes to capture city i. Due to the similarity of neighboring cities, If the city i and j are neighboring cities, if Ant has captured city i, then the time for Ant to capture city j is A[j]/2. Of course if Ant has captured city j, then the time for Ant to capture city i is A[i]/2. It is the same for Bob. We define the total time to capture a country be the time to capture city 1 + the time to capture city 2 + … + the time to capture city N. Now we want to know the minimal total time.
For simplicity, we assume that there is only one path to go from one city to another city.
Input
The first line contains a integer N(0<\N<100), which is the number of cities.Then following N lines describe A[1], A[2], …, A[N];Then following N lines describe B[1], B[2], …, B[N];Next comes N-1 lines, each contains two integers x, y, meaning that city x and city y are neighboring.
Output
Just output the minimal total time in a single line.
Sample Input
3
1 2 5
3 8 1
1 2
1 3
Sample Output
3
题目描述:
有两个人要攻占树上的所有城市,每个人对于每一个城市都有相应的费用,但是如果一个人攻占的城市与他攻占的另外一个城市相连,那这个城市的费用就可以减半。问最后两个人攻占所有城市的总费用最小是多少。
每一个节点有四种状态:由哪一个人攻占,这个节点是否半价。比较麻烦的是如果只是指示当前节点是否半价的话,无法有效的进行状态转移。假如把颜色相同且相连的节点看做联通块的话那么每一个连通块都至少有一个全价的节点。
因此取dp[i][j][k] 表示第i个节点取j个颜色 (0 < j <2), k表示以当前节点为根的子树里面是否有颜色为j 的全价节点 (0 < k <2)。
k 等于0的时候比较简单,该节点的每一个子节点要么取相同颜色的半价,要么取另外一种颜色,并且该节点的子树里面一定有全价节点。
k等于1的时候就有两种情况了, 首先是自己本身取全价,另外一种是子树里面的某一个节点取全价。这个时候可以用一个变量表示某一个子树由全部半价变成有一个全价的差值。显然要取这个变化量的最小值。详见代码:
参考了:这里
#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#include<vector>#include<algorithm>typedef long long LL;#define clr(x) memset((x),0,sizeof(x))#define inf 0x3f3f3f3fusing namespace std;int a[110];int b[110];vector <int> v[110];int dp[110][2][2];int n;void dfs(int x,int fa){ int t1=0,t2=0; int c1=inf,c2=inf; //变化量 int flag = 0; for(int i = 0;i<v[x].size();i++) if(v[x][i]!=fa) { dfs(v[x][i],x); flag = 1; t1 += min(dp[v[x][i]][0][0],dp[v[x][i]][1][1]); t2 += min(dp[v[x][i]][1][0],dp[v[x][i]][0][1]); c1 = min(c1,dp[v[x][i]][0][1] - min(dp[v[x][i]][0][0],dp[v[x][i]][1][1])); c2 = min(c2,dp[v[x][i]][1][1] - min(dp[v[x][i]][1][0],dp[v[x][i]][0][1])); } if(!flag) { dp[x][0][0] = a[x]/2; dp[x][1][0] = b[x]/2; dp[x][0][1] = a[x]; dp[x][1][1] = b[x]; } else { dp[x][0][0] = a[x]/2+t1; dp[x][1][0] = b[x]/2+t2; dp[x][0][1] = min(a[x]+t1,a[x]/2+t1+c1); dp[x][1][1] = min(b[x]+t2,b[x]/2+t2+c2); }}int main(){ while(~scanf("%d",&n)) { int x,y; for(int i = 1;i<=n;i++) scanf("%d",&a[i]); for(int i = 1;i<=n;i++) scanf("%d",&b[i]); for(int i = 1;i<=n;i++) v[i].clear(); for(int i = 1;i<n;i++) { scanf("%d%d",&x,&y); v[x].push_back(y); v[y].push_back(x); } memset(dp,0,sizeof dp); dfs(1,-1); printf("%d\n",min(dp[1][1][1],dp[1][0][1])); } return 0;}
- hdu4340
- hdu4340 树形dp
- 【树形DP】 hdu4340 Capturing a country
- HDU4340 Capturing a country 树状DP经典
- HDU4340 Capturing a country 树形dp
- 2012 Multi-University Training Contest 5-1001 hdu4340
- 2012 Multi-University Training Contest 5[hdu4340~4349]
- 2012 Multi-University Training Contest 5-1001 hdu4340(Virus原创)
- from zen coding to Emmet
- 机器视觉检测物体定位
- Oracle 动态SQL实现SQL查询子集行转列
- Java在HBase数据库创建表
- 图论07—经过指定两点的最短路
- hdu4340
- Matlab中数组元素引用——三种方法
- 【xUtils】Android快速开发框架之xUtils
- Java中char数组与字符串String类型的转换
- eclipse乱码解决方法
- app功能描述
- leetcode:Combination Sum
- java适配器模式
- 通过预加载器提升网页加载速度