hdu4340 樹形dp,hdu4340dp

來源:互聯網
上載者:User

hdu4340 樹形dp,hdu4340dp

http://acm.hdu.edu.cn/showproblem.php?pid=4340



Problem DescriptionAnt 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. 
InputThe 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. 
OutputJust 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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.