#include<iostream> #include<fstream> #include<cstdio> #include<cstring> #include<cmath> #include<map> #include<queue> #include<stack> #include<vector> #include<set> #include<ctype.h> #include<algorithm> #include<string> #define PI acos(-1.0) #define maxn 1000 #define INF 1<<25 #define mem(a, b) memset(a, b, sizeof(a)) typedef long long ll; using namespace std; struct node { int x, l, t; bool friend operator < (node s, node v) // 先根据路径长度非递减排序,如果长度相同根据费用非递减排序 { if (s.l != v.l) return s.l > v.l; return s.t > v.t; } }; struct E { int d, l, t; }; int k, n, r; vector<E> v[110]; int bfs() { node now, tmp; now.x = 1, now.l = 0, now.t = 0; priority_queue<node> q; q.push(now); while(!q.empty()) { now = q.top(); q.pop(); if (now.x == n) return now.l; for (int u = 0; u < v[now.x].size(); u++) if (now.t + v[now.x][u].t <= k) { tmp.x = v[now.x][u].d, tmp.l = now.l + v[now.x][u].l, tmp.t = now.t + v[now.x][u].t; q.push(tmp); } } return -1; } int main () { scanf("%d%d%d", &k, &n, &r); while(r--) { int s; E e; scanf("%d%d%d%d", &s, &e.d, &e.l, &e.t); v[s].push_back(e); } printf("%d\n", bfs()); return 0; }
原文:http://blog.csdn.net/sio__five/article/details/18996155