POJ 1724 Roads(dijkstra变形)

来源:互联网 发布:北京供销大数据怎么样 编辑:程序博客网 时间:2024/05/24 03:21

题目描述:
ROADS
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12510 Accepted: 4608
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.
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

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号城市开始走,要去n,但是每段路需要花硬币,他身上有k个硬币,问从1到n在硬币不超过k的情况下的最短路径。

题目分析:
乍一看以为是最短路径,单反先不是,样例画画图就知道不是最短路径,那么为什么要说是dijkstra变形呢?
要做这道题,自己要明白dijkstra的原理是什么。
(1)找到最短路径已经确定的顶点,从它出发更新相邻顶点的最短距离
(2)此后不需要再关心1中的最短距离已经确定的顶点
首先先看一下下面原始的dijkstra的代码

struct edge{    int to,cost;};typedef pair<int,int> P;//first是最短距离,second是顶点编号int V;vector<edge> G[MAX_V];int d[MAX_V];void dijkstra(int s){    priority_queue<P,vector<P>,greater<P> > que;    fill(d,d+V,INF);    d[s]=0;    que.push(P(0,s));    while(!que.empty())    {        P p=que.top();que.pop();        int v=p.second;        if(d[v]<p.first) continue;        for(int i=0;i<G[v].size();i++)        {            edge e=G[v][i];            if(d[e.to]>d[v]+e.cost){                d[e.to]=d[v]+e.cost;                que.push(P(d[e.to],e.to));            }        }    }}

从代码中可以看到,这个dijkstra的代码是通过优先队列来实现的,每次从队列中弹出最短距离最小的,如果这个最短距离比记录的最短距离大,那么久跳过(说明之前已经有比这个小的距离来更新过了),否则的话就用这个最短距离来更新从这个点通向的顶点。如此往复下去就可以了

接下来分析这道题目,我们要找的是金币不超过k的最短路径,那么我们可以在压进优先队列的元组中添加一个目前花费的金币这个值,然后只要金币没超过k,就把当前的最短距离,花费金币,和顶点编号压进队列(优先排距离的大小,其次是金币),每次弹出时由于最短路径可能金币不满足,所以并不存在一个真正最短的问题,所以就不需要d[MAX_V]这个数组了。因为弹出的点都是金币满足的,所以只要顶点编号是N了,那么说明就搜到了结果,推出即可,如果队列都空了还没找到,那好,老公只好在家里陪老婆了。

代码:

#include "stdio.h"#include "vector"#include "queue"using namespace std;struct node{    int to,l,t;    node(int to1,int l1,int t1)    {        to=to1,l=l1,t=t1;    }node(){}     //构造函数    bool operator < (const node &other)const    {        if(l==other.l) return other.t<t;// 小的优先        return other.l<l;    }};vector<node> G[100+5];int K,n,r;int dijkstra(int s){    priority_queue<node> que;    que.push(node(s,0,0));        while(!que.empty())    {        node p=que.top();que.pop();        int v=p.to;        if(v==n){            return p.l;        }        for (int i = 0; i < G[v].size(); ++i)        {            node e=G[v][i];            if(p.t+e.t<=K){//金币满足就压进去                que.push(node(e.to,p.l+e.l,p.t+e.t));            }        }    }    return -1;}int main(){    int i,from;    while(scanf("%d",&K)!=EOF)    {        scanf("%d%d",&n,&r);        node e;        for(i=0;i<r;i++)        {            scanf("%d%d%d%d",&from,&e.to,&e.l,&e.t);            G[from].push_back(e);        }        printf("%d\n",dijkstra(1));    }    return 0;}

有错误的话欢迎评论指正。

0 0
原创粉丝点击