Hdu-3660-Alice and Bob's Trip

Source: Internet
Author: User

A tree with n knots starts from 0 (root node), Alice and Bob go from 0 to the leaf node, Alice goes through the shortest path, and Bob goes through the longest path, bob first selects the next node, then the two come together to that node, then Alice selects the next node ......, The total length must be within [L, R] and the longest route length (1 <= n <= 500000, 0 <= L, R <= 1000000000, 1 <= distance between adjacent nodes <= 1000 ).


--> Angry brush tree dp...

Set da [I] to the shortest distance from node I to the leaf, then

The state transition equation is da [x] = min (da [x], db [v [e] + w [e]).

Set db [I] As Bob and start from node I to reach the longest distance of the leaf, then

The state transition equation is db [x] = max (db [x], da [v [e] + w [e]).

I/O optimization is added and submitted in C ++ ~

 

#include <cstdio>#include <cstring>#include <algorithm>#include <cctype>using namespace std;const int maxn = 500000 + 10;const int INF = 0x3f3f3f3f;int n, L, R, head[maxn], nxt[maxn], u[maxn], v[maxn], w[maxn], ecnt, da[maxn], db[maxn];void init(){    ecnt = 0;    memset(head, -1, sizeof(head));}void addEdge(int uu, int vv, int ww){    u[ecnt] = uu;    v[ecnt] = vv;    w[ecnt] = ww;    nxt[ecnt] = head[uu];    head[uu] = ecnt;    ecnt++;}int nextInt(){    char c = getchar();    while(!isdigit(c)) c = getchar();    int ret = 0;    while(isdigit(c)){        ret = ret * 10 + c - '0';        c = getchar();    }    return ret;}void read(){    int uu, vv, ww, i;    for(i = 0; i < n-1; i++){        uu = nextInt();        vv = nextInt();        ww = nextInt();        addEdge(uu, vv, ww);    }}void dp(int x, int cur){    da[x] = head[x] == -1 ? 0 : INF;    db[x] = 0;    for(int e = head[x]; e != -1; e = nxt[e]){        int len = cur + w[e];        if(len <= R) dp(v[e], len);        if(len + db[v[e]] >= L && len + db[v[e]] <= R) da[x] = min(da[x], db[v[e]] + w[e]);        if(len + da[v[e]] >= L && len + da[v[e]] <= R) db[x] = max(db[x], da[v[e]] + w[e]);    }}void solve(){    dp(0, 0);    if(db[0]) printf("%d\n", db[0]);    else puts("Oh, my god!");}int main(){    while(scanf("%d%d%d", &n, &L, &R) == 3){        init();        read();        solve();    }    return 0;}

 

Related Article

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.