"Poj 1724" ROADS Shortest path (dijkstra+ priority queue)

Source: Internet
Author: User

ROADS
Time limit:1000ms Memory limit:65536k
Total submissions:12436 accepted:4591

Description
N cities named with numbers 1 ... N is connected with one-way roads. Each road has a parameters associated with It:the road length and the toll that needs to be paid for the road (express Ed in the number of coins).
Bob and Alice used to live in the city 1. After noticing that Alice is cheating in the card game they liked to play, Bob broke up with she and decided to move away -to the city N. He wants to get there as quickly as possible, but he's short on cash.

We want to help Bob to find the shortest path from the City 1 to the city N so he can afford with the amount of money he Has.

Input
The first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins so Bob can spend on H Is the.
The second line contains the integer n, 2 <= n <=, and the total number of cities.

The third line contains the integer r, 1 <= R <= 10000, and the total number of roads.

Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :

S is the source city, 1 <= S <= ND is the destination city, 1 <= D <= NL is the road length, 1 <= L <= 100T is the toll (expressed in the number of coins), 0 <= T <=100

Notice that different roads could have the same source and destination cities.

Output
The first and the only line of the "the" output should contain the total length of the shortest path from the City 1 to the city N whose total toll are less than or equal K coins.
If Such path does not exist, only number-1 should is written to the output.

Sample Input

5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2

Sample Output

11

Source
CEOI 1998

title link : http://poj.org/problem?id=1724

Test Instructions : You have k points, there are n points, M edge, a maxcost. The Edge is a forward edge (Len,cost), not maxcost a shortest path

idea : spfa+ an If limit like tle
Optimization with dijkstra+ priority queue

Code :

#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <queue>using namespace STD;intN,m;structnode{intY,di,fee; NodeintYyintDiiintFe) {y=yy,di=dii,fee=fe; }node () {}BOOL operator< (Const structNode A)Const{if(A.di==di)returna.fee<fee;returna.di<di; }}now,nex;intMaxxintdis[ the]; vector<node>lin[ the];p riority_queue<node> q;intDJ () {Q.push (1,0,0)); while(!q.empty ())        {now=q.top (); Q.pop ();if(now.y==n)returnNow.di; for(intI=0; I<lin[now.y].size (); i++) {if(Now.fee+lin[now.y][i].fee<=maxx) Q.push (node (lin[now.y][i].y,now.di+lin[now.y][i].di,now.fee+lin[now.y][        I].fee)); }    }return-1;}intMain () {scanf("%d%d%d", &maxx,&n,&m); for(intI=1; i<=m;i++) {intAA,BB,CC,DD;scanf("%d%d%d%d", &AMP;AA,&AMP;BB,&AMP;CC,&AMP;DD);    Lin[aa].push_back (Node (BB,CC,DD)); }printf("%d\n", DJ ());}

POJ 1724 ROADS Shortest Path (dijkstra+ 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.