hdu4340 树形dp
来源:互联网 发布:博优化纤招聘 编辑:程序博客网 时间:2024/05/29 18:37
http://acm.hdu.edu.cn/showproblem.php?pid=4340
Problem Description
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.
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
31 2 53 8 11 21 3
Sample Output
3
/**hdu 4340 树形dp题目大意: 给定一棵树,为每一个点染色,有a和b两种颜色,染色的费用分别为a[i]和b[i],如果与i点相邻的点被染上了 a色那么染i点只需花费a[i]/2,b色同a。问把所有点全部染色需要的最小费用。解题思路:dp[i][j][k](i:1~n;j;0~1;k:0~1)表示以i为根节点的树i点染j色并且该子树中有k个点全价染j色。 x1为i点染a色时,它的儿子节点的最小花费(此时子树中与i连通的点均还是半价,还没有全价点) y1为i点染a色时,以它的儿子节点为根的子树中,从全部不全价的点到有一个全价点的改变所需最小费用 x2,y2同理。 状态转移方程: dp[u][0][0]=a[u]/2+x1; dp[u][0][1]=min(x1+a[u],x1+a[u]/2+y1);*/#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;const int maxn=105;int head[maxn],ip;int n,a[maxn],b[maxn],dp[maxn][2][2];struct note{ int v,next;}edge[maxn*4];void init(){ memset(head,-1,sizeof(head)); ip=0;}void addedge(int u,int v){ edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;}void dfs(int u,int pre){ int flag=0; int x1=0,x2=0,y1=0x3f3f3f3f,y2=0x3f3f3f3f; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==pre)continue; flag=1; dfs(v,u); x1+=min(dp[v][0][0],dp[v][1][1]); x2+=min(dp[v][1][0],dp[v][0][1]); y1=min(y1,dp[v][0][1]-min(dp[v][0][0],dp[v][1][1])); y2=min(y2,dp[v][1][1]-min(dp[v][1][0],dp[v][0][1])); } if(flag==0) { dp[u][0][0]=a[u]/2; dp[u][0][1]=a[u]; dp[u][1][0]=b[u]/2; dp[u][1][1]=b[u]; } else { dp[u][0][0]=a[u]/2+x1; dp[u][1][0]=b[u]/2+x2; dp[u][0][1]=min(x1+a[u],x1+a[u]/2+y1); dp[u][1][1]=min(x2+b[u],x2+b[u]/2+y2); }}int main(){ while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } for(int i=1;i<=n;i++) { scanf("%d",&b[i]); } init(); for(int i=0;i<n-1;i++) { int u,v; scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } memset(dp,0,sizeof(dp)); dfs(1,-1); printf("%d\n",min(dp[1][1][1],dp[1][0][1])); } return 0;}
0 0
- hdu4340 树形dp
- 【树形DP】 hdu4340 Capturing a country
- HDU4340 Capturing a country 树形dp
- hdu4340
- HDU4340 Capturing a country 树状DP经典
- 树形dp
- 树形DP
- 树形dp
- 树形DP
- 树形dp
- 树形DP
- 树形DP
- 树形DP
- 树形DP
- 树形dp
- 树形dp
- 树形dp
- 树形dp
- POJ 3159 Candies(spfa最短路,差分约束)
- (十一)延时执行、圆角(可实现圆形label)、代理设计模式
- uboot中 Tiny 4412和smdk 4212的一些不同
- 原理图与PCB设计基础
- unity3d引用android第三方sdk
- hdu4340 树形dp
- 移动UI设计中的7种主要导航模式
- pring MVC常用的注解
- HDU 4454 Stealing a Cake(三分暴力枚举)
- 给字符指针赋值, ” “双引号与' '单引号的区别
- TF-IDF及其算法
- 一款简单的右侧客服代码(带二维码扫描图)
- 模拟网络状态不好的情况
- 蒙版和调整图层的使用—用PS调出黑白背景