Occupy FLB

Description

Chunge had a dreamlast night. In the dream he became a general leading an army of urban manager(城管 UMto attack the neighbor country FLB.

      FLB is an island country which consistsof N islands. There are N-1 channel between these islands and every two islandexist one channel. The army should take down one of these island firstly toland on and attack the other island until occupy the whole FLB.

      When attack the island, the number of theUM should not less than a specify number. And after the attack, a number of theUM will sacrifice and some UM should stay and defend the island. And the numberof these UM is not the same for different island. Specify, for the ith island,ai UMs is needed to take down it and bi UMs will sacrifice and ci UMs shouldstay and defend the island. And the other UMs will continue to attack the otherisland.

      For some safety reason, each channelcould be go across only 2 times. Once the army land on the new island, the battlewill begin. And we can go through the island which had been occupied.

      Now the map of FLB will be given and theai, bi, ci of each island. Please help Chunge to calculate the minimum numberof UM to occupy FLB successfully.


Input Description
There are multiple test cases.
Each cast starts with an integer N (1<=N<=100) means the number of the island.
Then follows N lines, each line contains 3 integers, a, b, c, which is describe above. (1<=a,b,c<=1000)
In the next N-1 lines, each line has 2 integers u, v indicates there will be one channel between island u and v. (1<=u,v<=N)
Output Description
For each test case, output the minimum number of UM.
Sample Input
3 
5 5 5 
10 5 5 
5 1 1 
1 3 
2 3 
5 
10 5 10 
20 10 5 
10 10 5 
5 5 5 
20 0 10 
1 2 
1 3 
1 4 
3 5 
Sample Output
22
65

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define inf 0x3f3f3f3f
int first[208],nxt[208],vv[208];
struct Island
{
	int ned,ded;
}island[108];
struct Son
{
	int ned,ded,red;
}sson[108][108];
inline int max(int a,int b)
{
	return a>b?a:b;
}
inline int min(int a,int b)
{
	return a>b?b:a;
}

int ned[208],ded[208],red[208];
bool vis[208];
bool cmp(Son a,Son b)
{
	if(a.ned > b.ned) return 1;
	if(a.ned < b.ned) return 0;
	if(a.ned == b.ned) return a.ded < b.ded;
}
void dfs(int x)
{
	int t = 0;
	bool flag = false;
	vis[x] = 1;
	for(int i=first[x];i!=-1;i=nxt[i])
	{
		int v = vv[i];
		if(!vis[v])
		{
			flag = true;
			dfs(v);
			sson[x][t].red = red[v];
			sson[x][t].ded = ded[v];
			sson[x][t++].ned = ned[v];
		}
	}
	if(!flag)
	{
		ned[x] = island[x].ned;
		ded[x] = island[x].ded;
		red[x] = island[x].ned - island[x].ded;
		return;
	}
	sort(sson[x],sson[x]+t,cmp);
	int nedd = island[x].ned,dedd = island[x].ded,res = island[x].ned - island[x].ded;
	for(int i=0;i<t;i++)
	{
		if(res < sson[x][i].ned)//如果剩下的人不够攻打
		{
			nedd += sson[x][i].ned - res;
			res += sson[x][i].ned - res;
		}
		//现在够攻打了
		if(res < sson[x][i].ded)//如果剩下的人不够占领该地
		{
			nedd += sson[x][i].ded - res;
			res += sson[x][i].ded - res;
		}
		res -= sson[x][i].ded;
		dedd += sson[x][i].ded;
	}
	ded[x] = dedd;
	ned[x] = nedd;
}		
int main()
{
	///freopen("in.txt","r",stdin);
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int e = 0;
		memset(first,-1,sizeof(first));
		for(int i=1;i<=n;i++)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			island[i].ned = max(a,b+c);
			island[i].ded = b+c;
		}
		for(int i=1;i<n;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			nxt[e]=first[u],vv[e]=v,first[u]=e++;
			nxt[e]=first[v],vv[e]=u,first[v]=e++;
		}
		int ans = inf;
		for(int i=1;i<=n;i++)
		{
			memset(vis,0,sizeof(vis));
			memset(ned,0,sizeof(ned));
			memset(ded,0,sizeof(ded));
			dfs(i);
			ans = min(ans,ned[i]);
		}
		printf("%d\n",ans);
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值