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 ;}