Algorithm: UVA 1484 Alice and Bob ' s trip (tree DP)

Source: Internet
Author: User
Tags final int size min

Meaning

For a tree n nodes, the node number is 0~n-1, and the vertex is 0.

Each side has a weight value.

Alice and Bob start at the apex and go down to the leaf node.

The first time is from Bob to choose which child knot, the second turn to Alice, in turn go down ...

Each edge gets the right value, and Bob hopes that the bigger the path, the better, and that Alice hopes the smaller the better.

Each time they will choose the optimal solution.

The final total weight should be within the range [L,r].

What is the maximum weight that Bob wants in the end?

Ideas

F (U, 0) represents the maximum value of the first U point when Bob chooses

F (U, 1) represents the maximum value of the first U point when Alice chooses

Tot (u) represents the sum of weights that go from vertex to point I

W (u,v) represents the weights of the edges of the junction U and V

So

F (U, 0) = max{F (V, 1) + W (u,v) | V is the son of U node && L <= F (v,1) +w (u) +tot (u) <= R}

F (U, 1) = min{F (V, 0) + W (u,v) | V is the son of U node && L <= F (v,0) +w (u) +tot (u) <= R}

The final answer is F (0, 0)

In addition, this problem in the HDU problem submission, with C + + can be AC, but with g++ but tle, I do not know why.

And in the UVA and La submissions, is submissionerr, then ask the UVA administrator that the system problem is temporarily unresolved ...

Code

/**========================================== * is a solution for ACM/ICPC problem * * @source: uva-1484 Alice A nd Bob ' s trip * @type: Tree DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: zengshuangde@gmail.com *===== ======================================*/#include <iostream> #include <cstdio> #include <algorithm > #include <vector> #include <queue> #include <cmath> #include <cstring> typedef long Long INT6
   
4;
const int INF = 0X3F3F3F3F;
Const double PI = ACOs (-1.0);
   
const int MAXN = 500010;
   
namespace adj{int HEAD[MAXN]; int size; struct node{int V, W, next;}
   
E[MAXN];
   
inline void Initadj () {size = 0; memset (head,-1, sizeof (head));}
inline void Addedge (int u, int v, int w) {e[size].v = v;
E[SIZE].W = W;
E[size].next = Head[u];
Head[u] = size++;
The using namespace Adj;
   
using namespace Std;
int N, l, R;
int f[maxn][2];
   
int DIST[MAXN]; inline bool Check (int sum) {return sum >=l && sum <= R; } void Dfs (int u, int tot) {f[u][0] = 0; f[u][1] = Head[u]==-1?0:inf; for (int e = head[u]; e!=-1; e = e[e].next) {in
T v = e[e].v;
   
int w = E[E].W;
   
DFS (v, tot+w);
   
if (check (tot+w+f[v][1])) {f[u][0] = max (f[u][0], w+f[v][1]);
if (check (tot+w+f[v][0])) {f[u][1] = min (f[u][1], w+f[v][0]);} //Read in acceleration//http://www.bianceng.cn Inline int nextint () {char c = getchar (); while (!isdigit (c)) C = GetChar (); int
x = 0;
while (IsDigit (c)) {x = x*10+c-' 0 '; c = GetChar ();} return x;  int main () {while (~scanf ("%d%d%d", &n, &l, &r)) {Initadj ()-for (int i = 0; i < n-1; ++i)
   
{int U, V, w; u = nextint (); v = Nextint (); w = Nextint (); Addedge (u,v,w);}
   
DFS (0, 0);
if (!check (F[0][0])) {puts ("Oh, My God!");} else {printf ("%d\n", F[0][0]);}
return 0; }

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.