hdu 4340 Capturing a country

f for A

f[u][0] as the min-time to conquer subtree u, u-th city is conquered by A and the start node of all the most nearly continuous-conquer path from each son had not been considered yet

f[u][1] as the min-time to conquer subtree u, u-th city is conquered by A and the start node of all the most nearly continuous-conquer path from each son had been considered

the same as g for B


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
using namespace std;
#define Rep(c, a, b) for (int c = (a); c < (b); c++)
#define Nre(c, a, b) for (int c = (a); c > (b); c--)
#define MAXN (200)
#define INFI (0x7fffffff)

struct Edge
{
	int v, next;
};
vector<Edge> E;
int L[MAXN];
void G_ini()
{
	E.clear();
	memset(L, -1, sizeof L);
}
void AE(int u, int v)
{
	Edge te = {v, L[u]};
	L[u] = E.size();
	E.push_back(te);
}

int n, ta[MAXN], tb[MAXN], f[MAXN][2], g[MAXN][2];
bool vis[MAXN];

void TDP(int u)
{	
	vis[u] = 1;
	bool lef = 1;
	int ami = INFI, bmi = INFI; //a/bmi as the minimum increment value to chose a start point given by all path from son
	for (int i = L[u]; i != -1; i = E[i].next)
	{
		int v = E[i].v;
		if (vis[v]) continue;
		lef = 0;
		TDP(v);
		int tma = min(f[v][0], g[v][1]),
			tmb = min(g[v][0], f[v][1]);
		ami = min(ami, f[v][1] - tma);
		bmi = min(bmi, g[v][1] - tmb);
		f[u][0] += tma;
		g[u][0] += tmb;
	}
	if (lef)
	{
		f[u][1] = ta[u];
		g[u][1] = tb[u];
		f[u][0] = ta[u] >> 1;
		g[u][0] = tb[u] >> 1;
	}
	else
	{
		f[u][1] = f[u][0] + min(ta[u], ta[u] / 2 + ami);
		g[u][1] = g[u][0] + min(tb[u], tb[u] / 2 + bmi);
		f[u][0] += ta[u] >> 1;
		g[u][0] += tb[u] >> 1;
	}
}


int main()
{
	while (~scanf("%d", &n))
	{
		G_ini();
		Rep(i, 0, n) scanf("%d", ta + i);
		Rep(i, 0, n) scanf("%d", tb + i);
		Rep(i, 0, n - 1)
		{
			int u, v;
			scanf("%d%d", &u, &v);
			u--; v--;
			AE(u, v);
			AE(v, u);
		}
		memset(vis, 0, sizeof vis);
		memset(f, 0, sizeof f);
		memset(g, 0, sizeof g);
		TDP(0);
		printf("%d\n", min(f[0][1], g[0][1]));
	}

	return 0;
}
it's a liittle complicated :{

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值