1336-fixing the Great Wall (DP)

Source: Internet
Author: User

The subject is very classic, is the dynamic planning of the "future cost" calculation, because the starting point fixed and maintenance time is ignored, so any time has been repaired point must be a continuous interval. So we use d[i][j][k] to indicate that the interval has been repaired [i,j]

And now is the point K, if K is 0, at I point, if K is 1, at J Point.  This obviously already can represent all States, then how to maintain the amount of time it? We can find that every T-time, no repair points will increase the cost, so we might as well pre-processing to find out the D value of each interval only and then the non-serviced points multiplied by time, so that we can be dynamic maintenance costs. In addition, the final answer must include the C value of all points only and.

Also note that although the answer to the question is not more than 1000000000, but the median may be super int, so we might as well use double.

However I use Cout<<floor (ans) <<endl; The output is always WA, and later realized that once the answer is large, it will automatically turn into scientific notation ...

See the code for details:

#include <bits/stdc++.h>using namespace Std;const double INF = 30000000000;int n,kase = 0,vis[1005][1005][5];    Double v,x,d[1005][1005][5],sum[1005];struct point{double x,c,d;    BOOL operator < (const point& v) Const {return x < v.x;    }}A[1005];d ouble dp (int i,int j,int k) {if (i==1&&j==n) return 0;    double& ans = d[i][j][k];    if (vis[i][j][k] = = Kase) return ans;    Vis[i][j][k] = Kase;    ans = INF;    Double p = (k = = 0? a[i].x:a[j].x);        if (i>1) {Double T = ABS (A[I-1].X-P)/V;        Double U = (sum[i-1]+sum[n]-sum[j]) *t+a[i-1].c;    ans = min (ans,dp (i-1,j,0) +u);        } if (j<n) {Double T = ABS (A[J+1].X-P)/V;        Double U = (sum[i-1]+sum[n]-sum[j]) *t+a[j+1].c;    ans = min (ans,dp (i,j+1,1) +u); } return ans;    int main () {memset (vis,0,sizeof (VIS));        while (cin>>n>>v>>x) {if (!n&&!v&&!x) return 0; for (int i=1;i<=n;i++) scanf ("%lf%lf%lf", & a[i].x,&a[i].c,&a[i].d);        Sort (a+1,a+n+1);        ++kase;        Sum[0] = 0;        for (int i=1;i<=n;i++) {Sum[i] = sum[i-1] + a[i].d; } a[0].x =-inf;        a[n+1].x = INF;        Double ans = INF; for (int i=1;i<=n+1;i++) {if (a[i-1].x<x&&x<a[i].x) {Double T = ABS (a[i-1].x-                x)/V;                Double cur = (sum[n]) *t+a[i-1].c;                if (i>1) ans = min (ans,dp (i-1,i-1,0) +cur);                T = ABS (A[I].X-X)/V;                Cur = (sum[n]) *t+a[i].c;            if (i<=n) ans = min (ans,dp (i,i,1) +cur);    }} printf ("%.0lf\n", Floor (ans)); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

1336-fixing the Great Wall (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.