BZOJ2286: [Sdoi2011]消耗战

Description

在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达。现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望。已知在其他k个岛屿上有丰富能源,为了防止敌军获取能源,我军的任务是炸毁一些桥梁,使得敌军不能到达任何能源丰富的岛屿。由于不同桥梁的材质和结构不同,所以炸毁不同的桥梁有不同的代价,我军希望在满足目标的同时使得总代价最小。
侦查部门还发现,敌军有一台神秘机器。即使我军切断所有能源之后,他们也可以用那台机器。机器产生的效果不仅仅会修复所有我军炸毁的桥梁,而且会重新随机资源分布(但可以保证的是,资源不会分布到1号岛屿上)。不过侦查部门还发现了这台机器只能够使用m次,所以我们只需要把每次任务完成即可。

Input

第一行一个整数n,代表岛屿数量。

接下来n-1行,每行三个整数u,v,w,代表u号岛屿和v号岛屿由一条代价为c的桥梁直接相连,保证1<=u,v<=n且1<=c<=100000。

第n+1行,一个整数m,代表敌方机器能使用的次数。

接下来m行,每行一个整数ki,代表第i次后,有ki个岛屿资源丰富,接下来k个整数h1,h2,…hk,表示资源丰富岛屿的编号。

Output

输出有m行,分别代表每次任务的最小代价。

 

Sample Input

10
1 5 13
1 9 6
2 1 19
2 4 8
2 3 91
5 6 8
7 5 4
7 8 31
10 7 9
3
2 10 6
4 5 7 8 3
3 9 4 6

Sample Output

12
32
22

HINT

 对于100%的数据,2<=n<=250000,m>=1,sigma(ki)<=500000,1<=ki<=n-1

Source

刚看到这题以为是多次询问树上最小割
然而一看数据范围爆炸
膜拜黄学长题解http://hzwer.com/6188.html
在虚树上树形dp dp[x]=min(dp[x],∑dp[son[x]])
关键怎么建虚树 考虑用dfs序
具体做法是维护一个栈 按照dfs序模拟dfs 这样就可以建一棵K-1的lca和K个关键节点的虚树 然后dp
用memset绝对TLE 应该在dp一个点结束后把head[x]置为0
#include <bits/stdc++.h>

using namespace std;

const int maxn = 250050;
const long long inf = 1e16;

int n,head[maxn],h[maxn],dfn[maxn],cnt,tot,head2[maxn],f[maxn][21];

int st[maxn],top,dep[maxn];

long long mn[maxn],dp[maxn];

struct Edge
{
	int to,nxt,val;
}e[maxn << 1] ,edge[maxn << 1];

inline void addedge(int x,int y,int w)
{
	e[ ++cnt ].to = y;
	e[ cnt ].nxt = head[ x ];
	head[ x ] = cnt;
	e[ cnt ].val = w;
}

inline void addedge(int x,int y)
{
	if( x == y ) return ;
	edge[ ++cnt ].to = y;
	edge[ cnt ].nxt = head2[ x ];
	head2[ x ] = cnt;
}

inline int read()
{
	int tmp = 0;
	char ch = getchar();
	while( ch < '0' || ch > '9' ) ch = getchar();
	while( ch >= '0' && ch <= '9' ) tmp = tmp * 10 + ch - '0' , ch = getchar();
	return tmp;
}

void dfs(int x)
{
	dfn[ x ] = ++tot;
	for( int i = head[ x ] ; i ; i = e[ i ].nxt )
	{
		int y = e[ i ].to;
		if( f[ x ][ 0 ] != y )
		{
			f[ y ][ 0 ] = x;
			mn[ y ] = min( mn[ x ],(long long)e[ i ].val);
			dep[ y ] = dep[ x ] + 1;
			dfs(y);
		}
	}
}

inline void init()
{
	for( int j = 1 ; j <= 20 ; j++ )
		for( int i = 1 ; i <= n ; i++ )
			f[ i ][ j ] = f[ f[ i ][ j - 1] ][ j - 1];
}

inline void pushup(int &x,int d)
{
	for( int i = 20 ; i >= 0 ; i-- )
		if( dep[ x ] - d >= (1 << i) )
			x = f[ x ][ i ];
}

inline int lca(int x,int y)
{
	if( dep[ x ] < dep[ y ] ) swap(x,y);
	if( dep[ x ] ^ dep[ y ] ) pushup(x,dep[ y ]);
	if( x == y ) return x;
	for( int i = 20 ; i >= 0 ; i-- )
		if( f[ x ][ i ] != f[ y ][ i ] )
			x = f[ x ][ i ],y = f[ y ][ i ];
	return f[ x ][ 0 ];
}

inline bool cmp(int x,int y) { return dfn[ x ] < dfn[ y ]; }

inline void cal(int x)
{
	dp[ x ] = mn[ x ];
	long long tmp = 0;
	for( int i = head2[ x ] ; i ; i = edge[ i ].nxt )
		cal( edge[ i ].to),tmp += dp[ edge[ i ].to ];
	head2[ x ] = 0;
	if( tmp && tmp <= dp[ x ] ) dp[ x ] = tmp;
}

inline void solve()
{
	cnt = tot = top = 0;
	int K = read();
	for( int i = 1 ; i <= K ; i++ ) h[ i ] = read();
	sort(h+1,h+K+1,cmp);
	h[ ++tot ] = h[ 1 ];
	for( int i = 2 ; i <= K ; i++ ) if( lca(h[ tot ],h[ i ]) != h[ tot ]) h[ ++tot ] = h[ i ];
	st[ ++top ] = 1;
	for( int i = 1 ; i <= tot ; i++ )
	{
		int LCA = lca(h[ i ], st[ top ]);
		while( true )
		{
			if( dep[ LCA ] >= dep[ st[ top - 1 ] ])
			{
				addedge(LCA,st[ top-- ]);
				if( st[ top ] != LCA ) st[ ++top ] = LCA;
				break;
			}
			addedge(st[ top - 1 ], st[ top ]);
			top--;
		}
		if( st[ top ] != h[ i ] ) st[ ++top ] = h[ i ];
	}
	while( --top ) addedge( st[ top ], st[ top + 1 ] );
	cal(1);
	printf("%lld\n",dp[ 1 ]);
}

int main()
{
	n = read();
	for( int i = 1 ; i < n ; i++ )
	{
		int x = read() , y = read() , w = read();
		addedge(x,y,w); addedge(y,x,w);
	}
	mn[ 1 ] = inf;
	dfs(1);
	init();
	int Q = read();
	while( Q-- )
		solve();
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值