Part ways and part ways

Source: Internet
Author: User

Part ways and part ways
Description

1... N of N cities are connected by a one-way road. Each road has two parameters: the length of the road and the cost of passing through the road.

Bob and Alice live in city 1, but when Bob finds Alice deceiving him when playing poker, he decides to turn his face with her and leave city 1 to go to city N. Bob wants to arrive at city N as soon as possible, but he does not have much money.

We hope that you can help Bob find the shortest path where the total fee from City 1 to city N does not exceed Bob's capacity.

Input

The first line of the input is 3 integers K, N, R, where:

K: Maximum charge that Bob can afford. 0 ≤ K ≤ 10000

N: The total number of cities. 2 ≤ N ≤ 100.

R: Number of roads. 1 ≤ R ≤ 10000.

In the next R row, each line uses s d l t (separated by spaces) to represent a path:

S: The departure city of the road, 1 ≤ S ≤ N

D: The target city of the road, 1 ≤ D ≤ N

L: the length of the road, 1 ≤ L ≤ 100

T: indicates the cost to be paid through this road. 0 ≤ T ≤ 100.

Output

Output a line of results for each test case: the length of the shortest path with the total cost less than or equal to the maximum cost K. If this path does not exist, only-1 is 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

Hint

The test data may containDuplicate edgeYesSelf-ring.

This question is difficult, but after learning DFS and BFS, you must be able to AC it. It is a typical search question and requires strong pruning (removing the currently known invalid search paths) to speed up search.

A look at the problem, found like PTA in a problem: <a href = "https://pta.patest.cn/pta/test/558/exam/4/question/9931"> Tourism Planning </a>, but the solution is different, the question of tourism planning is the transformation of the dijela algorithm. The idea of this question is to search for the possible paths from 1 to N in all cities, and then select a shortest path with a fee not greater than K, if yes, the length of the output Shortest Path is used. If no value is used, the output value is-1. Pseudocode:
1 DFS (LGraph G, int I, int K) 2 {3 if (I = N) // you can find 4 bestDist = currDist in N cities; // Shortest Path = current path 5 else 6 {7 cycle each G is labeled as the adjacent vertex of I 8 if (visited [adjvertex] = false & ck + adjvertex. cost <= K 9 & currDist + adjvertex. dist <bestDist) 10 {/* If the adjacent contact is not accessed, pruning: the current cost of ck + to the adjacent contact is less than or equal to K, the current distance + to the adjacent point is less than the shortest distance */11 ck + = adjvertex. cost; 12 currDist + = adjvertex. dist; 13 DFS (G, adjvertex, K); 14 ck-= adjvertex. cost; 15 currDist-= adjvertex. dist; 1617} 18} 19 20 return bestDist; 21}
1 // the DFS function is implemented by itself. 2 # include <stdio. h> 3 # include <stdlib. h> 4 # include <stdbool. h> 5 # include <string. h> 6 7 # define MaxVertexNum 101/* the maximum number of vertices is set to 101 */8 typedef int Vertex;/* the Vertex subscript is used to represent the Vertex, set the weight of the integer */9 typedef struct {/* edge to distance and cost */10 int dist, cost; 11} WeightType; 12 13/* edge definition */14 typedef struct ENode * PtrToENode; 15 struct ENode {16 Vertex V1, V2;/* directed edge <V1, v2> */17 WeightType Weight;/* Weight */18}; 19 typedef PtrToENode Edge; 20 21/* adjacent point definition */22 typedef struct AdjVNode * PtrToAdjVNode; 23 struct AdjVNode {24 Vertex AdjV; /* neighboring point subscript */25 WeightType Weight;/* Edge Weight */26 PtrToAdjVNode Next;/* pointer to the Next adjacent point */27 }; 28 29/* vertex header node definition */30 typedef struct Vnode {31 PtrToAdjVNode FirstEdge;/* edge header pointer */32} AdjList [MaxVertexNum]; /* AdjList is an adjacent table type */33 34/* graph node definition */35 typedef struct GNo De * PtrToGNode; 36 struct GNode {37 int Nv;/* Number of vertices */38 int Ne;/* Number of edges */39 AdjList G; /* adjacent table */40}; 41 typedef PtrToGNode LGraph;/* graph type stored in the form of an adjacent table */42 43 LGraph BuildGraph (); 44 LGraph CreateGraph (int VertexNum); 45 void InsertEdge (LGraph Graph, Edge E); 46 int DFS (LGraph G, int n); 47 48 bool visited [MaxVertexNum]; 49 int K; 50 int ck; // The current cost is 51 int bestDist; // The best distance is 52 int currDist; // when Previous route 53 54 int main () 55 {56 int result; 57 scanf ("% d", & K); 58 LGraph G = BuildGraph (); 59 memset (visited, false, sizeof (visited); 60 ck = currDist = 0; 61 visited [1] = true; 62 bestDist = 0 xffff; 63 result = DFS (G, 1 ); 64 if (bestDist = 0 xffff) // if there is no matching path,-1 65 result =-1; 66 printf ("% d \ n", result) is returned ); 67 getchar (); 68 return 0; 69} 70 71 LGraph CreateGraph (int VertexNum) 72 {/* Initialize a Graph with VertexNum vertices but no edges */73 Vertex V; 74 LGraph Graph; 75 76 Graph = (LGraph) malloc (sizeof (struct GNode )); /* Create a Graph */77 Graph-> Nv = VertexNum; 78 Graph-> Ne = 0; 79/* initialize the adjacent header pointer */80/* Note: the default vertex number starts from 1 to (Graph-> Nv) */81 for (V = 1; V <= Graph-> Nv; V ++) 82 Graph-> G [V]. firstEdge = NULL; 83 84 return Graph; 85} 86 87 void InsertEdge (LGraph Graph, Edge E) 88 {89 PtrToAdjVNode NewNode; 90 91/* Insert <V1, V2> */92/* Create a New adjacent contact for V2 */93 NewNode = (PtrToAdjVNode) malloc (sizeof (struct AdjVNode )); 94 NewNode-> AdjV = E-> V2; 95 NewNode-> Weight = E-> Weight; 96/* Insert V2 into the V1 header */97 NewNode-> Next = Graph-> G [E-> V1]. firstEdge; 98 Graph-> G [E-> V1]. firstEdge = NewNode; 99} 100 101 LGraph BuildGraph () 102 {103 LGraph Graph; 104 Edge E; 105 Vertex V; 106 int Nv, I; 107 108 scanf ("% d", & Nv);/* Number of read vertices */109 Graph = CreateGraph (Nv);/* initialize a Graph with Nv vertices but no edges */110 111 scanf ("% d", & (Graph-> Ne )); /* Number of read edges */112 if (Graph-> Ne! = 0) {/* If an Edge exists */113 E = (Edge) malloc (sizeof (struct ENode);/* Create an Edge node */114/* to read an Edge, the format is "Start Point and end point weight". Insert the adjacent table */115 for (I = 0; I <Graph-> Ne; I ++) {116 scanf ("% d", & E-> V1, & E-> V2, & E-> Weight. dist, & E-> Weight. cost); 117 InsertEdge (Graph, E); 118} 119 return Graph; 120}

 

Related Article

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.