DP slope optimization on the bzoj1767 tree + two points __ Tree DP

Source: Internet
Author: User
Tags prepare
Topic Description

Oi Country is a warm and beautiful place, its map is a tree structure, the root node of the tree is Oi City Capital--orzboshi City. Orzboshi City is the place where King Boshi works, he receives information from other cities every day. The way in which information is transmitted by the state of Oi is this:
In addition to Orzboshi City, every city has a messenger, when I city generated a message, messenger I need to spend Si s_i time to prepare, and then to the speed of Vi v_i to the father of the I city, to the next city J, Messenger can choose to continue to run, or inform the City J Messenger J, Let him continue to deliver the mail. Messenger J to spend SJ S_j time to prepare, and then to SJ S_j speed to send letters.
Boshi want to know how long it will take at least to get the information from all the towns to Orzboshi city. Input Description

First line: N, representing Oi state-owned N-City
Next n-1 line: X,y,z says there's a Z-meter road between X and Y city.
Next N-1 Line: Indicates the preparation time of the Messenger from 2 to N city and the speed V (the time to run 1 meters to spend v) output Description

One row, n-1 number, representing 2 to N city information to the shortest time data range of Orzboshi city

3≤n≤105 3≤n≤10^5, 0≤si≤109, 0≤si≤10^9, 1≤vi≤109 1≤vi≤10^9 topic Analysis

The first look may feel like a bit of water DP, presumably F[i]=min (f[j]+v[i]* (Dis[i]-dis[j]) +s[i]), Dis[i] represents the distance from 1 to I, and then J is the ancestor node of I.
Then look to the data range ... I'm sure it'll tle.
Although the idea of slope optimization, but this problem does not make decision Monotonicity, with monotonous queue maintenance is not feasible.
So look to the English solution ... decided to boshi with the big guy (although most of the boshi of the credit of the big guy ...). So to say a solution bar: push slope type

If you are thinking about I, Dep[j]>dep[k], (DEP: depth) and the decision to choose J is better, then there are:
F[j]+v[i]∗ (Dis[i]−dis[j]) +s[i]<f[j]+v[i]∗ (Dis[i]−dis[k]) +s[i] f[j]+v[i]* (DIS[I]-DIS[J)) +s[i]
Wow, this is a really good formula.
F[j]−f[k]dis[j]−dis[k]<v[i] \frac{f[j]-f[k]}{dis[j]-dis[k]}
Note that now we have f[j]−f[k]dis[j]−dis[k] \frac{f[j]-f[k]}{dis[j]-dis[k]} as g (J,k)

Maintenance Stack

To traverse this tree will be DFS, but ... No decision Monotonicity (if the parent of a is b,b is C, and so on, then choose c when considering B, it is possible to choose B or D in the case of a, if you are maintaining with a monotone queue, you cannot easily eject the first element of the team)
So it's good to maintain the stack.
In short, we want to maintain the monotony of the slope, monotonically increasing, as shown in the figure.

Since the monotony has been maintained, we need to find the best solution. Two points to find the team head (...) The top of the stack. The optimal decision (how to find the next thing to say), update f[i], and then we'll put I on the stack. But... Recursion back after the stack is still to be used, how can be arbitrarily changed to go to it.
So we found the position in the update stack where I want to put (the second method is next), modify the top of the stack to that position and replace that position with I. After all the child nodes of I are DFS, we move the top pointer back and back the original element of the position (stored temporarily with the intermediate variable) to backtrack. How do you get two points?

1. Two points to find the first best decision of the team

int sfind (int l,int r,ll x) {
    int mid=0;
    while (l<=r) {
        mid= (l+r) >>1;
        if (g (Q[mid+1],q[mid]) <v[x]) l=mid+1;
        else if (g (Q[mid],q[mid-1]) >v[x]) r=mid-1;
        else return mid;
    }
    return mid;
}

Think about the premise of pushing the slope type

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.