[Bzoj 1096] [zjoi2007] Warehouse Construction

Source: Internet
Author: User
Description

Company L has n factories, distributed from high to bottom on a hill ., Factory 1 is at the top of the hill, and factory N is at the foot of the hill. Because the mountain is located in the inland region of the Plateau (dry and light rain), Company L generally stack its products directly in the open air to save costs. Suddenly one day, Mr. L, president of Company L, received a call from the Meteorological Department and was told that there would be a rainstorm three days later, so Mr. l decided to urgently create warehouses in some factories to avoid product exposure. Due to different terrain, the cost of building warehouses in different factories may be different. The PI of finished products already exists in the factory I, and the cost for building a warehouse in the factory I is CI. For factories that do not have warehouses, their products should be shipped to other warehouses for storage, and because l's product's external sales department is set at the factory N, therefore, products can only be shipped to the bottom of the road (that is, they can only be shipped to the warehouse of a larger factory). Of course, the shipping of products also requires a fee. Assume that one unit distance of a product is 1. Assume that the created warehouse capacity is large enough to accommodate all products. You will get the following data: XI (x1 = 0), Pi (finished product quantity), And I (1;  the cost Ci for building a warehouse at the factory I; please help company l find a warehouse construction solution to minimize the total cost (Construction Cost + transportation cost.

Input

The first line contains an integer N, indicating the number of factories. Each row in the next n rows contains two integers, XI, PI, and CI. The meaning is described in the question.

Output

Contains only one integer, which is the cost of finding the optimal solution.

Sample input3
0 5 10
5 3 100
9 6 10
Sample output32
Hint

Create a warehouse in factory 1 and factory 3 at 10 + 10 = 20, transportation costs (9-5) x 3 = 12, and total costs 32. If a warehouse is built only in factory 3, the establishment cost is 10, the transportation cost is (9-0) * 5 + (9-5) * 3 = 57, and the total cost is 67, better than the former. [Data scale] for 20% of data, n ≤ 500; for 40% of data, n ≤ 10000; for 100% of data, n ≤ 1000000. All Xi, PI, and Ci values are within 32-bit signed integers, so that the intermediate calculation result cannot exceed 64-bit signed integers.

Source

Naked DP is very watery... However, O (N ^ 2) cannot afford such a large range. A certain TLE can only adopt a slope smaller than O (n) to optimize DP. Specifically, it is to maintain a bidirectional queue, this allows the vertices in the queue to form a convex function image, so as to remove many non-optimal solutions that do not need to continue looping DP, for details, refer to the application of monotonic Optimization in Dynamic Planning in the jsoi training team paper

It took me nearly half an hour to publish it according to the above paper:

After pushing, you can do it. Each time you drop the team, the first part is definitely not the element of the optimal solution. Maintain the monotonically increasing slope in the queue (remove the monotonically decreasing slope) then add the new f [I], I, [1, N]

# Include <stdio. h ># define maxn 1000500 long int f [maxn], X [maxn], C [maxn], sum [maxn], sump [maxn], p [maxn], Q [maxn]; // F [I] = all the minimum total costs for warehouse repair at the I factory // F [I] = min {f [J] + W [I, j] + C [I]}, C [I] = Cost of warehouse repair at the I factory, W [I, j] = the cost of shipping I to J // sum [I] = the first I factory distance and // Q [] array used to simulate long int getf (int I, int J) // evaluate f [J] + W [I, j] {return f [I] + sum [I]-f [J]-sum [J];} int main () {int I, j, N; // enter scanf ("% d", & N); for (I = 1; I <= N; I ++) {scanf ("% LLD", & X [I], & P [I], & C [I]); sum [I] = sum [I-1] + X [I] * P [I]; sump [I] = sump [I-1] + P [I];} int h = 0, T = 1; for (I = 1; I <= N; I ++) {// DP slope optimization, maintain the lower convex function // F [J] + sum [J]-f [I]-sum [I] // --------------------- <X [I], I> J, prove that the I status is better than the J status // sump [J]-sump [I] // the first part of the team only needs to start from the optimal status, the end part of the team should be removed from the non-monotonically increasing part before joining the team while (H <t & getf (Q [H + 1], Q [H]) <X [I] * (sump [Q [H + 1]-sump [Q [H]) h ++; f [I] = f [Q [H] + sum [Q [H]-X [I] * sump [Q [H] + X [I] * sump [I]-sum [I] + C [I]; while (H <t & getf (Q [T], I) * (sump [Q [T-1]-sump [Q [T]) <= getf (Q [T-1], Q [T]) * (sump [Q [T]-sump [I]) t --; // The team's tail slope is smaller than that of the previous element. Q [++ T] = I; // new team entry} printf ("% LLD \ n", F [N]); 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.