hdu trilogy ROADS

 

Problem Description
N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins). Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash.
We want to help Bob to find the shortest path from the city 1 to the city N that 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 that Bob can spend on his way. The second line contains the integer N, 2 <= N <= 100, the total number of cities.
The third line contains the integer R, 1 <= R <= 10000, 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 <= N
  • D is the destination city, 1 <= D <= N
  • L is the road length, 1 <= L <= 100
  • T is the toll (expressed in the number of coins), 0 <= T <=100
Notice that different roads may have the same source and destination cities.
 
Output
The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins. If such path does not exist, only number -1 should be 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
*********************************************************************************************
spfa algorithm for finding the shortest path
*********************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 struct  edge
 7 {
 8     int rp,len,wast,next;
 9 }e[10111];
10 struct  point
11 {
12     int sp,len,wast;
13     bool  operator<(const struct point c)const//重载运算符
14       {
15            IF (len == c.len)
 16             return c.wast < WAST;
 . 17            return c.len < len;
 18 is        }
 . 19  } P1, P2;
 20 is  int totalmoney, head [ 1001 ], NUM, DIS [ 1001 ], numcity, numpath, I, J;
 21 is  void the Add ( int X, int Y, int LL, int V) // prior to addition to the star side chain 
22 is        {
 23 is          [NUM]. RP = E Y;
 24          E [NUM] = .LEN LL;
 25         E [NUM] .wast = V;
 26 is          E [NUM] = .next head [X];
 27          head [X] NUM = ++ ;
 28        }
 29 The priority_queue <Point> QQ;
 30  void SPFA () // find the source to the from each side 
31 is  {
 32      the while (! qq.empty ())
 33 is        qq.pop ();
 34 is      p1.sp = . 1 ; p1.len = 0 ; p1.wast = 0 ;
 35      qq.push (P1);
 36      the while (! qq.empty ())
 37 [       {
 38 is           P2 =qq.top();
39          if((p2.sp)==numcity)
40          {
41              cout<<p2.len<<endl;
42              return;
43          }
44          qq.pop();
45          for(int i=head[(p2.sp)];i!=-1;i=e[i].next)
46           {
47              int y=e[i].rp;
48              if(p2.wast+e[i].wast<=totalmoney)
49                {
50                    p1.len=p2.len+e[i].len;
51                    p1.wast=p2.wast+e[i].wast;
52                    p1.sp=y;
53                    qq.push(p1);
54                }
55           }
56 
57      }
58     cout<<"-1"<<endl;
59     return;
60 }
61 int main()
62 {
63     cin>>totalmoney;
64     memset(head,-1,sizeof(head));
65     cin>>numcity>>numpath;
66     num=0;
67     int x,y,ll,v;
68     for(int i=1;i<=numpath;i++)
69      {
70          cin>>x>>y>>ll>>v;
71          add(x,y,ll,v);
72      }
73      spfa();
74      return 0;
75 }
View Code
 

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3317606.html

Guess you like

Origin blog.csdn.net/weixin_34310127/article/details/93727628