[HDU] 3660 Alice and Bob's trip-tree DP?

Source: Internet
Author: User

The Harbin field competition was not completed...

My code and ideas refer to this blog:

Http://www.cppblog.com/Yuan/archive/2010/10/03/128074.html? Opt = Admin

In my opinion, this is greedy and violent ..

I just started to use vector, one by one clear (), timeout. Then memset it, MLE. Google knows that the objects in C ++ STL cannot be memset. After all, there are various member variables and functions that we cannot see, which can easily cause memory leakage.

Then we use the pointer to do it. When we need a new node, We Will malloc it and submit it to MLE... then I finally understand what the nodes in the code above is .. It's amazing!

Open an array of nodes in advance. Each time you need a new node, you need it from nodes, so there is no memory waste problem! Then modify, submit, and pass ~

 

Problem: 3660 (Alice and Bob's trip)

Judge Status: Accepted

Runid: 3035244 language: C ++ Author: yueashuxia

Code render status: rendered by hdoj C ++ code render version 0.01 Beta

# Include <stdio. h> <br/> # include <limits. h> <br/> # include <algorithm> <br/> using namespace STD; <br/> # define size 500001 <br/> struct node <br/> {<br/> int V; <br/> int Len; <br/> node * Next; <br/>}; <br/> struct tree <br/>{< br/> node * First; <br/> }; <br/> int Dist [size], L, R, DP [size]; <br/> TREE tree [size]; <br/> node nodes [size], * First; <br/> void DFS (int u, bool flag) <br/>{< br/> If (Dist [u]> R) {Dist [u] = 0; return ;}< br/> DP [u] = flag? 0: int_max; <br/> If (tree [u]. first = NULL) {DP [u] = 0; return ;}< br/> for (node * List = tree [u]. first; List = List-> next) <br/>{< br/> int v = List-> V; <br/> int W = List-> Len; <br/> Dist [v] = DIST [u] + W; <br/> DFS (v ,! Flag); <br/> If (Dist [u] + dp [v] + W <= R & Dist [u] + dp [v] + W> = L) <br/> {<br/> If (FLAG) DP [u] = max (DP [u], DP [v] + W ); <br/> else DP [u] = min (DP [u], DP [v] + W ); <br/>}< br/> int main () <br/>{< br/> int N, I, j, k, A, B, C; <br/> while (scanf ("% d", & N, & L, & R )! = EOF) <br/>{< br/> first = nodes; <br/> for (I = 0; I <n; I ++) tree [I]. first = NULL; <br/> for (I = 0; I <n-1; I ++) <br/>{< br/> scanf ("% d", & A, & B, & C); <br/> node * newnode = first; <br/> newnode-> next = tree [A]. first; <br/> newnode-> V = B; <br/> newnode-> Len = C; <br/> tree [A]. first = newnode; <br/> first ++; <br/>}< br/> Dist [0] = 0; <br/> DFS (0, 1 ); <br/> If (DP [0] <L | DP [0]> R) printf ("oh, my god! /N "); <br/> else printf (" % d/N ", DP [0]); <br/>}< br/> return 0; <br/>}< br/>


 

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.