【树形DP】 hdu4340 Capturing a country

Capturing a country

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.
 

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



#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define MAX 105
#define inf ((1<<30)-1)
int a[MAX],b[MAX];
int dp[MAX][2][2];
//dp[i][0][0] 代表A占领i点,i点取一半且树不完整
//dp[i][0][1] 代表A占领i点,树完整
//dp[i][1][0] 代表B占领i点,i点取一半且树不完整
//dp[i][1][1] 代表B占领i点,树完整
vector<int> edge[MAX];
void dfs(int x,int pre)
{
    int k,childMin[2],childRoot[2];
    //存储子树一半值的和
    childMin[0]=childMin[1]=0;
    //存储子树中非根结点的结点的最小值
    childRoot[0]=childRoot[1]=inf;
    //初始化
    dp[x][0][0]=(a[x]>>1);
    dp[x][1][0]=(b[x]>>1);
    dp[x][0][1]=a[x];
    dp[x][1][1]=b[x];
    //树叶且不是树根时返回
    if(edge[x].size()==1&&pre!=0) return;
    for(int i=0;i<edge[x].size();++i)
    {
        k=edge[x][i];
        if(k==pre) continue;
        dfs(k,x);
        childMin[0]+=min(dp[k][0][0],dp[k][1][1]);
        childMin[1]+=min(dp[k][0][1],dp[k][1][0]);
        //min(dp[k][0][0],dp[k][1][1])是选入最优解里面的,然后由根据此求出非根结点的最小代价入口
        childRoot[0]=min(childRoot[0],dp[k][0][1]-min(dp[k][0][0],dp[k][1][1]));
        //min(dp[k][1][0],dp[k][0][1])是选入最优解里面的,然后由根据此求出非根结点的最小代价入口
        childRoot[1]=min(childRoot[1],dp[k][1][1]-min(dp[k][1][0],dp[k][0][1]));
    }
    dp[x][0][0]+=childMin[0];
    dp[x][1][0]+=childMin[1];
    dp[x][0][1]=min(childMin[0]+a[x],childMin[0]+(a[x]>>1)+childRoot[0]);
    dp[x][1][1]=min(childMin[1]+b[x],childMin[1]+(b[x]>>1)+childRoot[1]);
}
int main()
{
    int n,x,y;
    for(;~scanf("%d",&n);)
    {
        for(int i=0;i<=n;++i) edge[i].clear();
        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)
        {
            scanf("%d%d",&x,&y);
            edge[x].push_back(y);
            edge[y].push_back(x);
        }
        dfs(1,0);
        printf("%d\n",min(dp[1][0][1],dp[1][1][1]));
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值