Description
Given a Tree of N nodes.
Each node has exactly two labels.(0 and 1).
You must choose which label to assign to give the minimum cost.
The Cost is the sum of two parts:
1. For each node their is a cost assigned to the node for a given label.
2. For each pairs of Node connected their is a cost assigned to it according to it's labels.
Input
In the first line, there are a number T(<=50) denotes the number of test cases.
In the second line, there are a number N(<=200000) denoting the number of Nodes.
In the third line, N numbers denoting the cost for each node assigned to label 0.
In the fourth line, N numbers denoting the cost for each node assigned to label 1.
In the next n-1 lines. each line containing six numbers P Q A B C D, P Q indicates there is a edge between P and Q , and A B C D are cost assigned to pairs of nodes which label is 00 01 10 11 accordingly.
The Tree root is always Node 1
Every cost is between 0 and 100
only 2 big cases
Output
The Minimum cost of the given tree.(A Integer)
Sample Input
1 4 3 1 1 3 4 0 0 4 1 2 0 1 1 0 2 3 0 1 1 0 2 4 0 1 1 0
Sample Output
8
#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int k,a,b,c,d;
};
int s[200005][2],dp[200005][2];
vector<node>vec[200005];
void dfs(int root)
{
if(vec[root].size()==0)
{
dp[root][0]=s[root][0];
dp[root][1]=s[root][1];
return;
}
dp[root][0]=s[root][0];
dp[root][1]=s[root][1];
for(int i=0;i<vec[root].size();i++)
{
node p;
p=vec[root][i];
dfs(p.k);
dp[root][1]+=min(dp[p.k][1]+p.d,dp[p.k][0]+p.c);
dp[root][0]+=min(dp[p.k][1]+p.b,dp[p.k][0]+p.a);
}
}
int main()
{
int n,m,i,x;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d",&s[i][0]);
}
for(i=1;i<=m;i++)
{
scanf("%d",&s[i][1]);
}
for(i=0;i<=m;i++)
{
vec[i].clear();
dp[i][0]=dp[i][1]=0;
}
for(i=1;i<m;i++)
{
node q;
scanf("%d%d%d%d%d%d",&x,&q.k,&q.a,&q.b,&q.c,&q.d);
vec[x].push_back(q);
}
dfs(1);
printf("%d\n",min(dp[1][0],dp[1][1]));
}
}