Test instructions: Tell you the distance between the oil price of N cities and the M-way. Q Ask, tell you the starting point, the destination and the maximum capacity of the tank, ask the minimum cost of question and answer.
Ideas: Refer to the online ideas http://blog.csdn.net/sdj222555/article/details/7693093 spfa+ priority Queue
in a thought similar to DP, DP[I][J] represents the minimum cost of reaching the remaining J-liter oil in the city of I. Maintain an advantage queue, at first my idea is that each edge connected to the current node starts from the length of the edge +1 until the limit is added to the tank capacity, and then all the points are enqueued
And then it exploded. Then can only refer to the idea of the online, the current oil volume plus 1 queue, and then if you can reach the next node, the next node to queue, of course, minus the cost of the side. But this time, tle. Because I had a complete SPFA. Because
We maintain a priority queue, so when the destination node first comes out of the team, it must be the minimum, the answer we ask for. and then finally a. 407MS of water over.
In this topic also learned to customize the structure of the priority queue used. Refer to the blog http://blog.csdn.net/dooder_daodao/article/details/5761550
It is necessary to define a CMP struct as a comparison when initializing the priority queue of the struct body.
#include <cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<map>#include<queue>using namespacestd;Const intinf=0x3f3f3f3f;Const intmaxn=1100;Const intmaxm=110000;intVAL[MAXN];intN,m,u,v,w,q,cc,ss,ee,tot;intdp[maxn][ the];intHEAD[MAXN];structedge{intV,w,nex;} EDGE[MAXM*2];structnode{intCost,u,lef;};structcmp{BOOL operator() (Node A,node b) {returnA.cost>B.cost; }};voidAddedge (intUintVintW) {EDGE[TOT].V=v; EDGE[TOT].W=W; Edge[tot].nex=Head[u]; Head[u]=tot++; EDGE[TOT].V=u; EDGE[TOT].W=W; Edge[tot].nex=Head[v]; HEAD[V]=tot++;}voidinit () {tot=0; memset (Head,-1,sizeof(head));} Priority_queue<Node,vector<Node>,cmp>que;voidSPFA () { for(intI=0; i<maxn;i++) for(intj=0;j< the; j + +) Dp[i][j]=INF; while(!que.empty ()) Que.pop (); Node U,node; U.cost=0; U.lef=0; U.U=SS; Que.push (U); while(!Que.empty ()) {u=Que.top (); Que.pop (); if(dp[u.u][u.lef]<=u.cost)Continue; Dp[u.u][u.lef]=U.cost; if(u.u==ee)return ; if(u.lef<cc&&dp[u.u][u.lef+1]>u.cost+VAL[U.U]) {NODE.U=u.u; Node.cost=u.cost+VAL[U.U]; Node.lef=u.lef+1; Que.push (node); } for(inti=head[u.u];i!=-1; i=Edge[i].nex) {Edge e=Edge[i]; if(e.w>U.lef)Continue; Node.cost=U.cost; NODE.U=e.v; Node.lef=u.lef-E.W; Que.push (node); /*for (int j=max (U.LEF,E.W); j<=cc;j++) {node node; node.cost=u.cost+ (J-u.lef) *val[u.u]; NODE.U=E.V; NODE.LEF=J-E.W; Que.push (node); } */ } }}intMain () {Freopen ("In.txt","R", stdin); while(SCANF ("%d%d", &n,&m)! =EOF) {init (); for(intI=0; i<n;i++) scanf ("%d",&Val[i]); for(intI=0; i<m;i++) {scanf ("%d%d%d",&u,&v,&W); Addedge (U,V,W); } scanf ("%d",&q); for(intI=0; i<q;i++) {scanf ("%d%d%d",&cc,&ss,&ee); SPFA (); if(dp[ee][0]!=INF) printf ("%d\n", dp[ee][0]); Elseprintf ("impossible\n"); } } return 0;}
View Code
POJ 3635 full Tank?