zoukankan      html  css  js  c++  java
  • luogu P2495 [SDOI2011]消耗战 |虚树+LCA+dp

    题目描述

    在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达。现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望。已知在其他k个岛屿上有丰富能源,为了防止敌军获取能源,我军的任务是炸毁一些桥梁,使得敌军不能到达任何能源丰富的岛屿。由于不同桥梁的材质和结构不同,所以炸毁不同的桥梁有不同的代价,我军希望在满足目标的同时使得总代价最小。

    侦查部门还发现,敌军有一台神秘机器。即使我军切断所有能源之后,他们也可以用那台机器。机器产生的效果不仅仅会修复所有我军炸毁的桥梁,而且会重新随机资源分布(但可以保证的是,资源不会分布到1号岛屿上)。不过侦查部门还发现了这台机器只能够使用m次,所以我们只需要把每次任务完成即可。

    输入格式

    第一行一个整数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,表示资源丰富岛屿的编号。

    输出格式

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


    每次按照关键点的dfs序排序,维护栈构建一棵新的树,在新的树上dp

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #define re register int
    #define ll long long 
    const ll inf=1ll<<62;
    using namespace std;
    inline int read(){
        int x=0;char ch=getchar();
        while(ch<'0'||ch>'9')ch=getchar();
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x;
    }
    const int N=250005;
    int bin[20],n,m,cnt,ind,top;
    int last[N],last2[N],fa[N][20];
    ll mn[N],f[N];
    int h[N],mark[N],deep[N],st[N];
    struct edge{
        int to,next,v;
    }e[N<<1],ed[N<<1];
    inline void insert(int u,int v,int w){
        e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;e[cnt].v=w;
        e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;e[cnt].v=w;
    }
    inline void insert2(int u,int v){
        if(u==v)return;
        ed[++cnt].to=v;ed[cnt].next=last2[u];last2[u]=cnt;
    }
    inline bool cmp(int a,int b){
    	return mark[a]<mark[b];
    }
    inline void pre(int x){
    	mark[x]=++ind;
    	for(int i=1;bin[i]<=deep[x];i++)
    		fa[x][i]=fa[fa[x][i-1]][i-1];
    	for(int i=last[x];i;i=e[i].next)
    	if(e[i].to!=fa[x][0]){
    		mn[e[i].to]=min(mn[x],(ll)e[i].v);
    		deep[e[i].to]=deep[x]+1;
    		fa[e[i].to][0]=x;
    		pre(e[i].to);
    	}
    }
    inline int lca(int x,int y){
        if(deep[x]<deep[y])swap(x,y);
        int t=deep[x]-deep[y];
        for(int i=0;bin[i]<=t;i++)
            if(t&bin[i])x=fa[x][i];
        for(int i=19;i>=0;i--)
            if(fa[x][i]!=fa[y][i])
                x=fa[x][i],y=fa[y][i];
        if(x==y)return x;
        return fa[x][0];
    }
    inline void dp(int x){
    	f[x]=mn[x];
    	ll tmp=0;
    	for(int i=last2[x];i;i=ed[i].next){
    		dp(ed[i].to);
    		tmp+=f[ed[i].to];
    	}
    	last2[x]=0;
    	if(tmp==0)f[x]=mn[x];
    	else if(tmp<=f[x])f[x]=tmp;
    }
    inline void solve(){
    	cnt=0; int K=read(),tot=0;
    	for(int i=1;i<=K;i++)h[i]=read();
    	sort(h+1,h+1+K,cmp);//dfs序排序
    	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 now=h[i],f=lca(now,st[top]);
    		while(1){
    			if(deep[f]>=deep[st[top-1]]){
    				insert2(f,st[top--]);
    				if(st[top]!=f)st[++top]=f;
    				break;
    			}
    			insert2(st[top-1],st[top]);top--;
    		}
    		if(st[top]!=now)st[++top]=now;
    	}
    	while(--top)insert2(st[top],st[top+1]);
    	dp(1);
    	printf("%lld
    ",f[1]);
    }
    signed main(){
    	bin[0]=1; for(int i=1;i<20;i++)bin[i]=bin[i-1]<<1;
    	n=read();
    	for(re i=1;i<n;i++){
    		int u=read(),v=read(),o=read();
    		insert(u,v,o);
    	}
    	mn[1]=inf; pre(1);
    	m=read();
    	for(re i=1;i<=m;i++)solve();
    }
    
  • 相关阅读:
    Markdown基本必学语法
    Javascript检测值
    JS中的函数传参
    解决windows下node-sass报错的问题
    scroll的应用
    css中常见几种float方式以及倒计时(刷新页面不清)
    jquery中append与appendTo方法区别
    如何利用sql注入进行爆库
    Smarty静态缓存
    我的DBDA类
  • 原文地址:https://www.cnblogs.com/naruto-mzx/p/12069667.html
Copyright © 2011-2022 走看看