ROADS
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11265 | Accepted: 4172 |
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.
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 :
Notice that different roads may have the same source and destination cities.
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.
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
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <set> 11 #include <map> 12 #include <list> 13 #include <iomanip> 14 #include <cstdlib> 15 #include <sstream> 16 using namespace std; 17 typedef long long LL; 18 const int INF=0x5fffffff; 19 const double EXP=1e-6; 20 const int MS1=105; 21 const int MS2=10005; 22 23 struct edge 24 { 25 int t,len,money; 26 }; 27 28 vector<vector<edge> > road(MS1); 29 int vis[MS1]; 30 int ans; 31 int len; 32 int money; 33 int d[MS1][MS2]; 34 int K,N,R; 35 36 void dfs(int cur) 37 { 38 if(cur==N) 39 { 40 ans=min(ans,len); 41 return ; 42 } 43 for(int i=0;i<road[cur].size();i++) 44 { 45 int t=road[cur][i].t; 46 if(!vis[t]) 47 { 48 int cash=road[cur][i].money; 49 int l=road[cur][i].len; 50 if(money+cash>K||len+l>ans) 51 continue; 52 if(len+l>d[t][money+cash]) 53 continue; 54 vis[t]=1; 55 len+=l; 56 money+=cash; 57 d[t][money]=len; 58 dfs(t); 59 vis[t]=0; 60 len-=l; 61 money-=cash; 62 } 63 } 64 } 65 66 int main() 67 { 68 scanf("%d%d%d",&K,&N,&R); 69 int s; 70 edge e; 71 for(int i=0;i<R;i++) 72 { 73 scanf("%d%d%d%d",&s,&e.t,&e.len,&e.money); 74 road[s].push_back(e); 75 } 76 memset(vis,0,sizeof(vis)); 77 for(int i=0;i<MS1;i++) 78 for(int j=0;j<MS2;j++) 79 d[i][j]=INF; 80 vis[1]=1; 81 ans=INF; 82 len=0; 83 money=0; 84 dfs(1); 85 if(ans==INF) 86 printf("-1 "); 87 else 88 printf("%d ",ans); 89 return 0; 90 }