POJ 1724 "Shortest path problem with additional constraints" "Priority queue"

Source: Internet
Author: User

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"

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.