• 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

    【数据规模和约定】

    对于10%的数据,2<=n<=10,1<=m<=5,1<=ki<=n-1

    对于20%的数据,2<=n<=100,1<=m<=100,1<=ki<=min(10,n-1)

    对于40%的数据,2<=n<=1000,m>=1,sigma(ki)<=500000,1<=ki<=min(15,n-1)

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

    又是虚树

    不会写虚树的戳这里http://www.cnblogs.com/chenyushuo/p/5069169.html 

    这题比上一题还要水,记一下每个节点到1号节点的路径的最小值,然后随便搞搞就行了

    code:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #define maxn 250005
     7 #define inf 4557430888798830399LL
     8 using namespace std;
     9 typedef long long int64;
    10 char ch;
    11 bool ok;
    12 void read(int &x){
    13     for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1;
    14     for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
    15     if (ok) x=-x;
    16 }
    17 int n,a,b,c,k,q;
    18 int idx,dfn[maxn],dep[maxn],fa[maxn][20];
    19 int64 f[maxn],path[maxn];
    20 int list[maxn],stack[maxn],top;
    21 bool flag[maxn];
    22 bool cmp(int a,int b){return dfn[a]<dfn[b];}
    23 struct Graph{
    24     int tot,now[maxn],son[maxn<<1],pre[maxn<<1];
    25     int64 val[maxn<<1];
    26     void put(int a,int b){pre[++tot]=now[a],now[a]=tot,son[tot]=b;}
    27     void put(int a,int b,int c){pre[++tot]=now[a],now[a]=tot,son[tot]=b,val[tot]=c;}
    28     void dfs1(int u){
    29         dfn[u]=++idx;
    30         for (int i=0;fa[u][i];i++) fa[u][i+1]=fa[fa[u][i]][i];
    31         for (int p=now[u],v=son[p];p;p=pre[p],v=son[p])
    32             if (v!=fa[u][0]) fa[v][0]=u,dep[v]=dep[u]+1,path[v]=min(path[u],val[p]),dfs1(v);
    33     }
    34     void dfs2(int u){
    35         f[u]=path[u];
    36         int64 sum=0;
    37         for (int p=now[u],v=son[p];p;p=pre[p],v=son[p]) dfs2(v),sum+=f[v];
    38         if (sum&&!flag[u]) f[u]=min(f[u],sum);
    39         now[u]=0;
    40     }
    41 }G1,G2;
    42 void swim(int &u,int h){for (int i=18;h;i--) if (h>=(1<<i)) h-=(1<<i),u=fa[u][i];}
    43 int calc_lca(int u,int v){
    44     if (dep[u]<dep[v]) swap(u,v);
    45     swim(u,dep[u]-dep[v]);
    46     if (u==v) return u;
    47     for (int i=18;i>=0;i--) if (fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];
    48     return fa[u][0];
    49 }
    50 int main(){
    51     read(n);
    52     for (int i=1;i<n;i++) read(a),read(b),read(c),G1.put(a,b,c),G1.put(b,a,c);
    53     path[1]=inf,G1.dfs1(1);
    54     for (read(q);q;q--){
    55         read(k),top=0,G2.tot=0;
    56         for (int i=1;i<=k;i++) read(list[i]),flag[list[i]]=1;
    57         sort(list+1,list+k+1,cmp);
    58         for (int i=1;i<=k;i++){
    59             if (!top){stack[++top]=list[i];continue;}
    60             int lca=calc_lca(list[i],stack[top]);
    61             while (dfn[lca]<dfn[stack[top]]){
    62                 if (dfn[lca]>=dfn[stack[top-1]]){
    63                     G2.put(lca,stack[top]);
    64                     if (stack[--top]!=lca) stack[++top]=lca;
    65                     break;
    66                 }
    67                 G2.put(stack[top-1],stack[top]),top--;
    68             }
    69             stack[++top]=list[i];
    70         }
    71         while (top>1) G2.put(stack[top-1],stack[top]),top--;
    72         G2.dfs2(stack[1]);
    73         printf("%lld
    ",f[stack[1]]);
    74         for (int i=1;i<=k;i++) flag[list[i]]=0;
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    Mybatisplus<一> Springboot框架使用MybatisPlus代码自动生成器
    今日收获
    今日收获
    字典特征提取
    sklearn数据集的导入及划分
    文本特征提取
    MySQL基础笔记
    docker笔记
    BOM 中的location对象和history对象
    完善 原生Js 实现的简单无缝滚动轮播图
  • 原文地址:https://www.cnblogs.com/chenyushuo/p/5071057.html
Copyright © 2020-2023  润新知