BZOJ 2286: [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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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

1
2
3
12
32
22

HINT

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

Source

Stage2 day2

Solution

算是虚树入门题了 建树的方法挺好的 维护最右链然后扩展 非常Interesting

建完树后跑DP即可

注意初始化! 虚树学习笔记【转载】

Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 250005;
const LL INF = 1000000007ll*1000000007ll;
inline int getint() {
    int r = 0; bool b = true; char c = getchar();
    while ('0' > c || c > '9') {if (c == '-') b = false; c = getchar();}
    while ('0' <= c && c <= '9') {r = r * 10 + (c - '0'); c = getchar();}
    if (b) return r;
    return -r;
}
struct edge_T { int to, next; LL w; };
bool vis[maxn];
LL query(int x, int y);
namespace VTree
{
    edge_T edge[maxn];
    int h[maxn], cnte, dsth[maxn], tot;
    inline void ins(int x, int y) {
        dsth[tot++] = x;
        edge[++cnte].to = y;
        edge[cnte].next = h[x];
        edge[cnte].w = query(x, y);
        h[x] = cnte;
    }
    LL dp(int now, LL W) {
        if (vis[now]) return W;
        LL sum = 0ll;
        for (int i = h[now]; i; i = edge[i].next) sum += dp(edge[i].to, edge[i].w);
        return min(sum, W);
    }
    inline void clear() {
        for (int i = 0; i < tot; ++i) h[dsth[i]] = 0;
        cnte = tot = 0;
    }
}

edge_T  edge[maxn << 1];
int n, m, q, cnte, sta[maxn], tail, h[maxn], dep[maxn], fa[maxn], top[maxn], siz[maxn], son[maxn], clo, dfn[maxn], a[maxn];
inline bool cmpdfn(int x, int y) {return dfn[x] < dfn[y];}

inline void ins(int x, int y, LL z) {
    edge[++cnte].to = y;
    edge[cnte].next = h[x];
    edge[cnte].w = z;
    h[x] = cnte;
}
inline int lca(int x, int y) {
    while (top[x] != top[y]) {
        if (dep[top[x]] > dep[top[y]]) x = fa[top[x]];
        else y = fa[top[y]];
    }
    if (dep[x] < dep[y]) return x;
    return y;
}
int anc[maxn][20];
LL ST[maxn][20];
void dfs1(int now, int father, int deep) {
    dep[now] = deep;
    anc[now][0] = fa[now] = father;
    siz[now] = 1;
    dfn[now] = ++clo;
    for (int i = h[now]; i; i = edge[i].next) {
        if (edge[i].to == father) continue;
        ST[edge[i].to][0] = edge[i].w;
        dfs1(edge[i].to, now, deep + 1);
        siz[now] += siz[edge[i].to];
        if (siz[edge[i].to] > siz[son[now]]) son[now] = edge[i].to;
    }
}
void dfs2(int now, int tp) {
    top[now] = tp;
    if(son[now]) {
        dfs2(son[now], tp);
        for (int i = h[now]; i; i = edge[i].next) {
            if (edge[i].to == fa[now] || edge[i].to == son[now]) continue;
            dfs2(edge[i].to, edge[i].to);
        }
    }
}
int main() {
    int x, y, z;
    n = getint();
    for (int i = 1; i < n; ++i) {
        x = getint(); y = getint(); z = getint();
        ins(x, y, z); ins(y, x, z);
    }
    dfs1(1, 0, 1);
    dfs2(1, 1);
    ST[1][0] = INF;
    for (int k = 1; k < 20; ++k)
        for (int i = 1; i <= n; ++i) {
            anc[i][k] = anc[anc[i][k-1]][k-1];
            if (anc[i][k] != 0) ST[i][k] = min(ST[i][k-1], ST[anc[i][k-1]][k-1]);
            else ST[i][k] = INF;
        }
    sta[0] = 1;
    q = getint();
    while (q--) {
        m = getint();
        for (int i = 0; i < m; ++i) {a[i] = getint(); vis[a[i]] = true;}
        sort(a, a + m, cmpdfn);
        for (int i = 0; i < m; ++i) {
            x = lca(a[i], sta[tail]);
            while (tail > 0 && dep[sta[tail-1]] >= dep[x]) {
                y = sta[tail--];
                VTree::ins(sta[tail], y);
            }
            if (sta[tail] != x) {
                VTree::ins(x, sta[tail--]);
                sta[++tail] = x;
            }
            sta[++tail] = a[i];
        }
        while (tail) {
            y = sta[tail--];
            VTree::ins(sta[tail], y);
        }
        printf("%lld\n", VTree::dp(1, INF));
        for (int i = 0; i < m; ++i) vis[a[i]] = false;
        VTree::clear();
    }
}
inline LL query(int x, int y) {
    LL ret = INF;
    for (int k = 19; k >= 0; --k) {
        if (dep[anc[y][k]] >= dep[x]) {
            ret = min(ret, ST[y][k]);
            y = anc[y][k];
        }
    }
    return ret;
}