//State: POJ1724 Accepted 1188K 32MS C++ 1968B /* *题目大意: * 给定总费用,还有n个城市,m条边,构成的图为单向图,然后 * m条边有费用,还有距离,求从1->n的最小距离,要求走边时 * 费用要小于边的费用。 *解题思路: * 用二维dij即可。 */
View Code
#include <queue> #include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; const int MAXN = 105; const int MAXE = 10005; const int MAX_COST = 10005; const int inf = 0x3f3f3f3f; typedef struct _node { int v, next; int l, c; }N; N edge[MAXE]; int cntEdge, head[MAXN]; typedef struct _no { int v; int c, dis; _no(): dis(inf) {} _no(int a, int b, int c1): v(a), dis(b), c(c1) {} friend bool operator < (const struct _no &n1, const struct _no &n2) { return n1.dis > n2.dis; } }priN; void init() { cntEdge = 0; for(int i = 0; i < MAXN; i++) head[i] = -1; } void addEdge(int u, int v, int l, int c) { edge[cntEdge].v = v; edge[cntEdge].l = l; edge[cntEdge].c = c; edge[cntEdge].next = head[u]; head[u] = cntEdge++; } int dis[MAXN]; int dijkstra(int s, int n, int tol)//1是起点,n是终点 { int vst[MAXN] = {0}; for(int i = 0; i <= n; i++) dis[i] = inf; priority_queue<priN> Q; Q.push(priN(s, 0, 0)); //dis[s] = 0; while(!Q.empty()) { priN pre = Q.top(); Q.pop(); if(pre.v == n) return pre.dis; for(int f = head[pre.v]; f != -1; f = edge[f].next) { int son = edge[f].v; int l = edge[f].l; int c = edge[f].c; if(pre.c + c <= tol) { //dis[son] = dis[pre.v] + l; Q.push(priN(son, pre.dis + l, pre.c + c)); } } } return -1; } int main(void) { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif int n, tol; while(scanf("%d", &tol) == 1) { int m; scanf("%d %d", &n, &m); init(); int u, v, l, c; for(int i = 0; i < m; i++) { scanf("%d %d %d %d", &u, &v, &l, &c); addEdge(u, v, l, c); } int sol = dijkstra(1, n, tol); printf("%d\n", sol); } return 0; }