BZOJ1003 Logistics and Transportation (DIJKSTRA+DP)

Source: Internet
Author: User

Test instructions

For a total of n days, the goods are transported from 1 to M every day, at the cost of the road length

And then every place could be a few days away.

And then you have to change the route to avoid these places that day, which takes a price k

Ask you the minimum price of n days

Ideas:

For a maximum of 100 days, you can n^2 violent time periods, indicating that the path of this time is the same

Then run Dijkstra and get the best solution

Then update the status with DP

For example, the current is from the L day to the R day, Dijkstra results in d[m], then

Dp[r]=min (dp[r],dp[l-1]+ (r-l+1) *d[m]+k);

DP initial value of INF,DP[0] is the initial value of 0, the last Dp[n]-k is the answer (dp[0] update is not k)

/************************************************author:d evil*********************************************** * */#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<cmath>#include<stdlib.h>#defineINF 0x3f3f3f3f#defineLL Long Long#defineRep (i,a,b) for (int i=a;i<=b;i++)#defineDec (i,a,b) for (int i=a;i>=b;i--)#defineOU (a) printf ("%d\n", a)#definePB Push_back#defineMKP Make_pairTemplate<classT>inlinevoidRD (T &x) {CharC=getchar (); x=0; while(!isdigit (c)) C=getchar (); while(IsDigit (c)) {x=x*Ten+c-'0'; c=GetChar ();}}#defineIn Freopen ("In.txt", "R", stdin);#defineOut Freopen ("OUT.txt", "w", stdout);using namespacestd;Const intmod=1e9+7;Const intn=2e3+Ten;intn,m,k,e,x,y,t;intid[n],ax[n],ay[n],dp[ the],d[ +];BOOLvis[ +];vector<pair<int,int> >eg[ +];structnode{intv,d; Node (intA=0,intb=0): V (a), d (b) {}BOOL operator< (ConstNode &a)Const    {        returnD>A.D; }};voidDijkstraintD1,intD2) {memset (Vis,0,sizeof(VIS)); memset (D,inf,sizeof(d)); d[1]=0; Rep (I,1Eif(! (ax[i]>d2| | AY[I]&LT;D1)) vis[id[i]]=1; Priority_queue<node>P; Q.push (Node (1,0));    node tmp;  while(!Q.empty ()) {tmp=Q.top ();        Q.pop (); intu=tmp.v; if(Vis[u])Continue; Vis[u]=1; Rep (I,0, Eg[u].size ()-1)        {            intv=eg[u][i].first,w=Eg[u][i].second; if(!vis[v]&&d[v]>d[u]+W) {D[v]=d[u]+W;            Q.push (Node (v,d[v)); }        }    }}intMain () {RD (n), RD (M), RD (k), RD (E);  while(e--) {Rd (x), RD (y), RD (T);        EG[X].PB (MKP (y,t));    EG[Y].PB (MKP (x,t));    } rd (E); Rep (I,1, E) rd (Id[i]), RD (Ax[i]), RD (Ay[i]); Rep (I,1, N) {Dp[i]=inf; Rep (J,1, I)            {Dijkstra (j,i); if(D[m]!=inf) Dp[i]=min (dp[i],dp[j-1]+ (i-j+1) *d[m]+k); }} ou (Dp[n]-k); return 0;}

BZOJ1003 Logistics and Transportation (DIJKSTRA+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.