「6月雅礼集训 2017 Day11」tree

【题目大意】

给出一棵带权树,有两类点,一类黑点,一类白点。

求切断黑点和白点间路径的最小代价。

$n \leq 10^5$

【题解】

直接最小割能过。。但是树形dp明显更好写

设$f_{x,0/1/2}$表示$x$这个点的子树中,0表示没有带颜色的点连到这个子树的根$x$,1表示黑点连到$x$,2表示白点连到$x$。

直接转移即可。具体看代码,挺好推得。。

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int N = 1e5 + 10, M = 2e5 + 10;
const ll inf = 1e17;

int n, d[N];
int head[N], nxt[M], to[M], w[M], tot = 0;
inline void add(int u, int v, int _w) {
    ++tot; nxt[tot] = head[u]; head[u] = tot; to[tot] = v; w[tot] = _w;
}
inline void adde(int u, int v, int _w) {
    add(u, v, _w), add(v, u, _w);
}

ll f[N][3];
// 没有有颜色点连到根上,只有黑点连到根上,只有白点连到根上
inline void dp(int x, int fa = 0) {
    f[x][0] = inf, f[x][1] = f[x][2] = 0;
    if(d[x] == 0) f[x][0] = 0;
    if(d[x] == 2) f[x][1] = inf;
    if(d[x] == 1) f[x][2] = inf;
    for (int i=head[x]; i; i=nxt[i]) {
        if(to[i] == fa) continue;
        int y = to[i];
        dp(y, x);
        f[x][0] += min(f[y][0], min(f[y][1], f[y][2]) + w[i]);
        if(f[x][0] > inf) f[x][0] = inf;
        f[x][1] += min(min(f[y][0], f[y][1]), f[y][2] + w[i]);
        if(f[x][1] > inf) f[x][1] = inf;
        f[x][2] += min(min(f[y][0], f[y][2]), f[y][1] + w[i]);
        if(f[x][2] > inf) f[x][2] = inf;
    }
//    printf("x = %d, f[x][0] = %I64d, f[x][1] = %I64d, f[x][2] = %I64d\n", x, f[x][0], f[x][1], f[x][2]); 
}

int main() {
    freopen("tree.in", "r", stdin);
    freopen("tree.out", "w", stdout);
    cin >> n;
    for (int i=1, u, v, _w; i<n; ++i) {
        scanf("%d%d%d", &u, &v, &_w);
        adde(u, v, _w);
    }
    int T; cin >> T; 
    for (int i=1, x; i<=T; ++i) {
        scanf("%d", &x); 
        d[x] = 1;
    }
    cin >> T;
    for (int i=1, x; i<=T; ++i) {
        scanf("%d", &x);
        d[x] = 2;
    }
    dp(1);
    cout << min(f[1][0], min(f[1][1], f[1][2]));
    return 0;
}
/*
6
1 2 5
2 4 4
2 5 1
1 3 2
3 6 7
1
4
2
5 6
*/
View Code

 

转载于:https://www.cnblogs.com/galaxies/p/20170627_a.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值