bzoj-1096 1096: [ZJOI2007] Warehouse construction (slope optimized DP)

Source: Internet
Author: User

Topic Links:

1096: [ZJOI2007] Warehouse Construction Description

L The company has n factories, which are spread from high to the bottom of a mountain. , factory 1 at the summit, Factory N at the foot of the hill. As the mountain is in the Highlands
Land area (dry less rain), l companies generally put products directly stacked in the open air, to save costs. Suddenly one day, the president of L company Mr. L received the weather
Department phone, was told that there would be a rainstorm after three days, so Mr. L decided to urgently set up some warehouses in some factories to prevent the products from getting badly soaked. Because
Depending on the terrain, the cost of building warehouses in different factories may be different. I plant currently has finished pi parts, in the first plant location to establish a warehouse
The cost is CI. For factories that do not have warehouses, their products should be transported to other warehouses for storage, and the external sales office of the company's products shall be
At the foot of the factory N, so the product can only be transported to the mountain (that is, only to the warehouse of the larger factory), of course, the delivery of products is also a cost,
Suppose the cost of transporting 1 units of a product is 1. Assume that the built-in warehouse capacity is large enough to accommodate all the products. You will get
The following data: 1: Factory I distance from Plant 1 XI (where x1=0); 2: Factory I currently have the number of finished products Pi;:3: The cost of establishing a warehouse at the factory I
Ci; please help L company to find a warehouse construction plan, make the total cost (construction cost + transportation cost) the smallest.

Input

The first line contains an integer n, which indicates the number of factories. The next n rows each row contains two integers in the XI, Pi, Ci, meaning title described.

Output

Contains only one integer, which is the cost to find the best solution.

Sample Input3
0 5 10
5 3 100
9 6Sample Output +Ideas:Formula I will not push, introduced to be a slope optimization dp;AC Code:
#include <bits/stdc++.h>using namespace Std;typedef long long ll;const LL inf=2e18;const int maxn=1e6+10;int n,head =1,tail=1; LL x[maxn],p[maxn],c[maxn],dp[maxn],sump[maxn],sumxp[maxn];int Q[MAXN];d ouble slope (int l,int r) {    double ratio;    Ratio= (Double) (dp[r]+sumxp[r]-dp[l]-sumxp[l])/(double) (sump[r]-sump[l]);    return ratio;} int main () {    scanf ("%d", &n);    for (int i=1;i<=n;i++) scanf ("%lld%lld%lld", &x[i],&p[i],&c[i]);    for (int i=1;i<=n;i++)    {        sump[i]=sump[i-1]+p[i];        Sumxp[i]=sumxp[i-1]+x[i]*p[i];    }    for (int i=1;i<=n;i++)    {while        (Head<tail&&slope (q[head],q[head+1]) < (double) x[i]) head+ +;        Dp[i]=c[i]+x[i]*sump[i]-sumxp[i]+dp[q[head]]-x[i]*sump[q[head]]+sumxp[q[head]];        while (Head<tail&&slope (Q[tail-1],q[tail]) >slope (q[tail],i)) tail--;        q[++tail]=i;    }    printf ("%lld\n", Dp[n]);    return 0;}

  

bzoj-1096 1096: [ZJOI2007] Warehouse construction (slope optimized DP)

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.