POJ 1724 maximum short circuit of priority queue

Source: Internet
Author: User

This is essentially a shortest path problem.

The shortest path from 1 to N is less than or equal to K.

Each route has a cost and a length.

We still adopt the greedy idea of dijela. Every time we get greedy, we will update other points based on this point.

I did not pay attention to the nature of the restricted priority queue:

The elements of the priority queue ensure that the cost is less than or equal to K to the target point. In this way, the length of the priority queue is sorted by the first dimension. When the first arrival of the priority queue is the end point, at this time, len is the request.

I am at this point. In addition, because the file has not been removed, WA is read and written twice.

# Include <iostream> # include <queue> # define INF 0x3FFFFFFFusing namespace std; struct ROADS {int v, len, roll; int next; ROADS () {next =-1 ;}}road [22222]; int ptr [111]; struct NODE {// public: int v, len, roll; friend bool operator <(NODE a, NODE B) {return. len> B. len;} NODE (int a = 0, int B = 0, int c = 0) {v = a; len = B; roll = c ;}; int Ecnt; void addEdge (int u, int v, int len, int roll) {road [Ecnt]. v = v; road [Ecnt]. len = Len; road [Ecnt]. roll = roll; road [Ecnt]. next = ptr [u]; ptr [u] = Ecnt ++;} int main () {// freopen ("test. in "," r ", stdin); // freopen (" ans. out "," w ", stdout); int K, N, R; while (scanf (" % d ", & K, & N, & R )! = EOF) {memset (ptr,-1, sizeof (ptr); Ecnt = 0; int u, v, len, roll; for (int I = 0; I <R; I ++) {scanf ("% d", & u, & v, & len, & roll); addEdge (u, v, len, roll); // addEdge (v, u, len, roll);} priority_queue <NODE> PQ; NODE temp (1, 0); PQ. push (temp); int ans =-1; while (! PQ. empty () // extract the shortest {NODE cur = PQ by dijstra each time. top (); PQ. pop (); if (cur. v = N) {ans = cur. len; break;} for (int I = ptr [cur. v]; I! =-1; I = road [I]. next) {if (cur. roll + road [I]. roll <= K) {temp. v = road [I]. v; temp. len = cur. len + road [I]. len; temp. roll = cur. roll + road [I]. roll; PQ. push (temp) ;}} printf ("% d \ n", ans) ;}return 0 ;}

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.