Application of POJ 3635 full Tank dynamic programming idea in SPFA algorithm

Source: Internet
Author: User

Test instructions

There are n cities, and M of the road known length, walking 1 units away from the road to spend 1 units of oil, each city can refuel and have their own oil price, now to two cities S,t, ask from S to t at least how much oil to spend.

Ideas:

Most of the online is to take priority queue to do, take SPFA do more interesting, but the general SPFA will time out. DIS[I][J] represents the minimum cost of J unit oil in the tank when it comes to city I. For city X, Y, their distance is W, if there is dis[x][j]<dis[y][i-w], then Y is determined to update, note if for each dis[x][j]<dis[y][j-w],dis[x][j+1]<dis[y][ J-w+1] ... All with the new dis[y][m],dis[y][m+1]...dis[y][c] words will time out, need to use the idea of dynamic planning in the determination of Y update only the total update once, specifically see the Code comment section.

Code:

#include <iostream> #include <queue>using namespace std;const int maxn=1024;const int Maxm=10024;const int Maxc=128;struct edge{int V,w,next;} Edge[maxm*2];int price[maxn];int head[maxn];int inq[maxn];int dis[maxn][maxc];int n,m,e;void spfa (int c,int s,int t) { int i,j;queue<int> Q;memset (inq,0,sizeof (INQ)); for (i=0;i<n;++i) for (j=0;j<=c;++j) Dis[i][j]=int_max; for (J=0;J&LT;=C;++J) dis[s][j]=price[s]*j;inq[s]=1; Q.push (s); while (! Q.empty ()) {int U=q.front (); Q.pop (); inq[u]=0;for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v,w=edge[i].w;int flag=0;for (int j=w;j<= C;++J) if (Dis[u][j]<dis[v][j-w]) {dis[v][j-w]=dis[u][j]; flag=1;} if (flag==1) {for (int j=1;j<=c;++j)//With the idea of dynamic planning is updated only once Dis[v][j]=min (Dis[v][j],dis[v][j-1]+price[v]); if (inq[v]==0) { Inq[v]=1; Q.push (v);}} }/* Timeout Code: for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v,w=edge[i].w;int flag=0;for (int j=w;j<=c;++j) if ( Dis[u][j]<dis[v][j-w]) {dis[v][j-w]=dis[u][j];for (int k=j-w;k<=c;++k)//try to update to full dis[v][k every time]=min (dis[v][k],dis[v][j-w]+ (k (j-w)) *price[v]); flag=1;} if (flag==1&&inq[v]==0) {inq[v]=1; Q.push (v);}} */}int Ans=int_max;for (i=0;i<=c;++i) ans=min (Ans,dis[t][i]), if (Ans==int_max) printf ("impossible\n"); elseprintf ("%d\n", ans); return;} int main () {E=0;memset (head,-1,sizeof (head)), scanf ("%d%d", &n,&m), int i;for (i=0;i<n;++i) scanf ("%d", &price[i]), while (m--) {int u,v,w;scanf ("%d%d%d", &u,&v,&w); edge[e].v=v;edge[e].w=w;edge[e].next= head[u];head[u]=e++;edge[e].v=u;edge[e].w=w;edge[e].next=head[v];head[v]=e++;} int q,c,s,t;scanf ("%d", &q), while (q--) {scanf ("%d%d%d", &c,&s,&t); SPFA (c,s,t);} return 0;}


Application of POJ 3635 full Tank dynamic programming idea in SPFA algorithm

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.