ZOJ 3469 food Delivery zone DP

Source: Internet
Author: User

This problem I will not, look at the Internet, the key to the position, the current stage or feel the interval DP is more difficult, mainly is too weak ... Qaq

Thinking in fact there is greedy meaning, n a household plus a shop, distributed in a one-dimensional straight line, should be from the shop, first to the two sides near the delivery, and then to the far send.

This certainly saves time, because went to the far place, near the place naturally sends, cannot return to send again, so we can begin to define the state

DP[I][J] represents the minimum cost of delivering from I to J, but we find that the state has a very difficult time to write the state transition equation,

Because I to J is sent, the last either stop at I, or stop at J, so define Dp[i][j][0] represents stop at I, Dp[i][j][1] stands for stop at J

Remember, sort the n+1 points (n households plus a store) and from near from the store to the two sides.

To update dp[i][j][0], consider that the end is stop at I, so

Dp[i][j][0]=min (Dp[i+1][j][0]+t,dp[i][j][0]) T represents the cost of waiting for the rest of us

Dp[i][j][0]=min (Dp[i+1][j][1]+t,dp[i][j][0])

To update dp[i][j][1], consider eventually stopping at J, so

Dp[i][j][1]=min (Dp[i][j-1][0]+t,dp[i][j][0]) T represents the cost of waiting for the rest of us

Dp[i][j][1]=min (Dp[i][j-1][1]+t,dp[i][j][0])

#include <cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cstdlib>#include<cmath>#include<cstdlib>#include<vector>#include<queue>using namespaceStd;typedefLong LongLL;Const intmaxn=1005;ConstLL inf=0x3f3f3f3f; LL dp[maxn][maxn][2],SUM[MAXN];structnode{LL X,val; BOOL operator< (ConstNode &e)Const    {        returnx<e.x; }} O[MAXN];intMain () {LL n,v,x;  while(~SCANF ("%lld%lld%lld",&n,&v,&x)) { for(intI=1; i<=n; ++i) scanf ("%lld%lld",&o[i].x,&o[i].val); ++N; O[n].val=0, o[n].x=x; Sort (o+1, o+1+N); sum[0]=0;  for(intI=1; i<=n; ++i) sum[i]=sum[i-1]+O[i].val; memset (Dp,inf,sizeof(DP)); intRes;  for(intI=1; i<=n;++i) {if(o[i].x==x) {res=i;  Break; }} dp[res][res][0]=dp[res][res][1]=0;  for(inti=res;i>=1;--i) { for(intj=res;j<=n;++j) {if(I==J)Continue; dp[i][j][0]=min (dp[i][j][0],dp[i+1][j][0]+ (Sum[i]+sum[n]-sum[j]) * (o[i+1].x-o[i].x)); dp[i][j][0]=min (dp[i][j][0],dp[i+1][j][1]+ (Sum[i]+sum[n]-sum[j]) * (o[j].x-o[i].x)); dp[i][j][1]=min (dp[i][j][1],dp[i][j-1][1]+ (sum[n]-sum[j-1]+sum[i-1]) * (o[j].x-o[j-1].x)); dp[i][j][1]=min (dp[i][j][1],dp[i][j-1][0]+ (sum[n]-sum[j-1]+sum[i-1]) * (o[j].x-o[i].x)); }} printf ("%lld\n", V*min (dp[1][n][0],dp[1][n][1])); }    return 0;}
View Code

ZOJ 3469 food Delivery zone 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.