fzu2157(树形)

F - ProgramCaicai's Trees
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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]));
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值