就是在最短路的基础上 多加了一个时间的限制 , 多一个限制多一维就好了 记住 分层最短路要用dijistra !!!
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int k, n, m;
int dis[maxn][], vis[maxn][], head[maxn];
int cnt; struct edge
{
int u, v, d, t;
edge(int u, int v, int d, int t) : u(u), v(v), d(d), t(t) {}
}; vector<edge> Edge;
vector<int> G[maxn << ];
struct node
{
int u, d, t;
node(int u, int d, int t) : u(u), d(d), t(t) {}
bool operator < (const node &a) const {
return d > a.d;
}
}; void add(int u, int v, int d, int t)
{
Edge.push_back(edge(u, v, d, t));
G[u].push_back(Edge.size() - );
} void dijkstra()
{
priority_queue<node> Q;
for(int i = ; i < maxn; i++) for(int j = ; j < ; j++) dis[i][j] = INF;
mem(vis, );
Q.push(node(, , ));
dis[][] = ;
// vis[0][1] = 1;
while(!Q.empty())
{
node x = Q.top(); Q.pop();
int u = x.u;
int t = x.t;
if(vis[t][u]) continue;
vis[t][u] = ;
for(int i = ; i < G[u].size(); i++)
{
edge e = Edge[G[u][i]];
if(dis[t + e.t][e.v] > dis[t][u] + e.d && t + e.t <= k)
{
dis[t + e.t][e.v] = dis[t][u] + e.d;
Q.push(node(e.v, dis[t + e.t][e.v], t + e.t));
}
}
}
} int main()
{
int u, v, d, t;
rd(k), rd(n), rd(m);
for(int i = ; i <= m; i++)
{
rd(u), rd(v), rd(d), rd(t);
add(u, v, d, t);
}
dijkstra();
int maxx = INF;
for(int i = ; i <= k; i++)
maxx = min(maxx, dis[i][n]);
if(maxx == INF)
return puts("-1"), ;
cout << maxx << endl; return ;
}