poj_1724 ROADS(有费用限制的最短路)

来源:互联网 发布:淘宝违法判刑大全 编辑:程序博客网 时间:2024/05/12 17:12
ROADS
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14250 Accepted: 5184

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 Nthat 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

5671 2 2 32 4 3 33 4 2 41 3 4 14 6 2 13 5 2 05 4 3 2

Sample Output

11
BFS加优先队列,优先队列保存某个到顶点u的方案,包括距离与费用,以距离作优先级。
从顶点1开始BFS,只要费用不超过k,就将当前方案加入优先队列。
当从优先队列出队的是顶点n的方案,该方案就是答案,优先队列的性质可以保证它是最短的。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 10010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct Edge{    int from, to, dist, cost;    Edge(int u, int v, int d, int c) : from(u), to(v), dist(d), cost(c) {}};struct HeapNode{    int d, u, c;    HeapNode(int dd, int uu, int cc) : d(dd), u(uu), c(cc) {}    bool operator < (const HeapNode& rhs) const    {        return d > rhs.d;    }};struct Dijkstra{    int n, m, k;    vector<Edge> edges;    vector<int> G[maxn];    void init(int n, int k)    {        this->n = n;        this->k = k;        for(int i = 1; i <= n; i++) G[i].clear();        edges.clear();    }    void AddEdge(int from, int to, int dist, int cost)    {        edges.push_back(Edge(from, to, dist, cost));        m = edges.size();        G[from].push_back(m-1);    }    int dijkstra(int s)    {        priority_queue<HeapNode> Q;        Q.push(HeapNode(0, s, 0));        while(!Q.empty())        {            HeapNode x = Q.top(); Q.pop();            int u = x.u;            if(u == n) return x.d;            for(int i = 0; i < G[u].size(); i++)            {                Edge& e = edges[G[u][i]];                if(x.c + e.cost <= k) Q.push(HeapNode(x.d+e.dist, e.to, x.c+e.cost));            }        }        return -1;    }};Dijkstra dij;int k, n, m;int main(){    while(~scanf("%d%d%d", &k, &n, &m))    {        dij.init(n, k);        int u, v, d, c;        for(int i = 0; i < m; i++)        {            scanf("%d%d%d%d", &u, &v, &d, &c);            dij.AddEdge(u, v, d, c);        }        printf("%d\n", dij.dijkstra(1));    }    return 0;}


0 0
原创粉丝点击