Test instructions: Give K a weight value. A graph containing n points, one-way edges of R bars.
Each edge has two weights, one of which is long and the other is an additional weight.
Ask for the shortest path if the sum of the additional weights of the road does not exceed K.
Ideas:
His thinking is too narrow, this question still see Daniel's ideas.
Using the priority queue, in the premise of the additional weights not exceeding the limit, each point leads to the point to go, each time to find the shortest path length point, if found the nth point algorithm end.
#include <stdio.h>#include<string.h>#include<queue>using namespacestd;intK,n,r,ednum;structedge{intId,w,mon; Edge*next;}; Edge*adj[ the];edge edges[10050];structst{intid,mon,w; St (intAintBintc) {id=a;mon=b;w=c;}};structcmp{BOOL operator()(ConstSt &a,ConstSt &b) {returnA.w>B.W; }};inlinevoidAddedge (intAintBintCintd) {Edge*AA; AA= &Edges[ednum]; Ednum++; AA->id=b; AA->w=C; AA->mon=D; AA->next=Adj[a]; Adj[a]=AA;}intsolve () {priority_queue<st,vector<st>,cmp>Q; for(Edge *p=adj[1];p;p=p->next) { if(p->mon<k) {St TMP (P->id,p->mon,p->W); Q.push (TMP); } } while(!Q.empty ()) {St TMP=Q.top (); Q.pop (); if(tmp.id==N)returnTMP.W; for(Edge *p=adj[tmp.id];p;p=p->next) { if(p->mon+tmp.mon<=k) {St Ttmp (P->id,p->mon+tmp.mon,p->w+TMP.W); Q.push (TTMP); } } } return-1;}intMain () {inta,b,c,d;//ok=0;scanf"%d%d%d",&k,&n,&R); for(intI=1; i<=r;i++) {scanf ("%d%d%d%d",&a,&b,&c,&d); Addedge (A,B,C,D); } printf ("%d\n", Solve ());}
POJ 1724 "Shortest path problem with additional constraints" "Priority queue"