HDU 4313 matrix tree DP

Source: Internet
Author: User

Question:

Tree with N points and m black spots

The following n-1 lines show the cost of the edge and delete the edge.

The following m black spots are marked as [0, n-1]

Delete some edges so that any two black spots are not connected.

The minimum cost for deletion.

Ideas:

Tree DP

Each vertex has two states: black or white.

If the dot itself is a black spot, there is only a black spot.

Otherwise, it can be considered that a black dot in the subtree is transferred up.

Therefore, DP [I] [0] is the status where I points are black spots.


#pragma comment(linker, "/STACK:1024000000,1024000000")#include <stdio.h>#include <algorithm>#include <iostream>#include <queue>using namespace std;typedef long long ll;const ll inf = 1e13;#define N 100100struct Edge{    int to; ll dis; int nex;    void put(){printf(" (%d,%lld)\n", to, dis);}}edge[N*2];int head[N], edgenum;void init(){memset(head, -1, sizeof head); edgenum = 0 ;}void add(int u, int v, ll d){    Edge E = {v, d, head[u]};    edge[edgenum] = E;    head[u] = edgenum++;}typedef long long ll;template <class T>inline bool rd(T &ret) {    char c; int sgn;    if(c=getchar(),c==EOF) return 0;    while(c!='-'&&(c<'0'||c>'9')) c=getchar();    sgn=(c=='-')?-1:1;    ret=(c=='-')?0:(c-'0');    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');    ret*=sgn;    return 1;}template <class T>inline void pt(T x) {    if (x <0) {        putchar('-');        x = -x;    }    if(x>9) pt(x/10);    putchar(x%10+'0');}int n, black_num;bool black[N];ll dp[N][2];void dfs(int u, int fa){    ll tmp = 0;    for(int i = head[u]; ~i; i = edge[i].nex){        int v = edge[i].to; if(v == fa)continue;        dfs(v, u);        tmp += min(dp[v][0] + edge[i].dis, dp[v][1]);    }    dp[u][0] = dp[u][1] = inf;    if(black[u]){        dp[u][0] = tmp;    }    else {        dp[u][1] = tmp;        for(int i = head[u]; ~i; i = edge[i].nex){            int v = edge[i].to; if(v == fa)continue;            dp[u][0] = min(dp[u][0], dp[v][0] + tmp - min(dp[v][0] + edge[i].dis, dp[v][1]));        }    }}ll solve(){    dfs(0, 0);    if(black[0])        return dp[0][0];    else        return min(dp[0][0], dp[0][1]);}void input(){    init();    rd(n); rd(black_num);    ll d;    for(int i = 1, u, v; i < n; i++)    {        rd(u); rd(v); rd(d);        add(u, v, d);        add(v, u, d);    }    memset(black, 0, sizeof black);    while(black_num--)    {        int u; rd(u);        black[u] = 1;    }}int main(){    int T; rd(T);    while(T--){        input();        pt(solve()); putchar('\n');    }    return 0;}/*9916 50 1 11 2 61 3 1002 4 32 5 14 12 14 6 15 8 18 13 38 15 45 7 114 7 13 9 19 10 19 11 11 3 4 13 159 50 1 10 2 22 6  61 3 31 4 41 5 54 7 21 8 4835672 20 1 10001 01 01 112 10 1 100005 20 1 51 2 32 3 43 4 50 45 30 1 51 2 32 3 43 4 50 4 25 40 1 51 2 32 3 43 4 50 4 2 35 50 1 51 2 32 3 43 4 50 1 2 3 411 60 1 100 2 90 3 80 4 71 5 42 6 53 7 13 10 23 9 34 8 65 6 7 9 8 10ans:107101000000371217*/


HDU 4313 matrix tree DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.