POJ 1274 ROADS (最短路) [Dijkstra+优先队列]

原题传送门


题面:

ROADS

                      Time Limit: 1000MS		Memory Limit: 65536K

Problem 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

题面描述

Bob和Alice原本是一对情侣,但Bob发现Alice为了在他们都喜欢玩的纸牌游戏中取得胜利而作弊了.所以Bob决定和Alice分手.以往Bob是和Alice一起住在城市1,而现在他要远离这个不诚信的女人到遥远的城市N去.为了尽快离开Alice,Bob需要一条从城市1到达城市N的最短路径,但Bob没有很多钱,所以需要在他经济范围能够承受的最短路径.

题目分析

如题,这是一个寻找最短路的问题,只是路有了cost值,需要cost值总和不大于K,路径总数不大于10000,城市数量不大于100,可以用优先队列+Dijkstra来做.
具体过程:以城市1作为当前城市,把当前城市能到达的城市加入优先队列,优先路径短,其次cost值低.( 能加入优先队列的判断条件为当前城市已用cost+到达城市cost <= K , 避免虽然能到城市N但cost不足以达到城市N )若当前城市为N时,则可以退出Dijkstra且输出到达当前城市所走的路径长度,若优先队列为空也没到达城市N则说明无法cost值不能让Bob到达城市N,输出-1.

具体代码

#include <iostream>
#include <queue>
#include <vector>
#include <string.h>
#include <algorithm>

using namespace std;
const int maxn = 1e4+5;
const int inf = 0x3f3f3f3f;

struct node
{
    int D , L , T;
    node (int DD , int LL , int TT)
    {
        D = DD , L = LL , T = TT;
    }node(){}
    bool operator < (const struct node a)const
    {
        if(a.L != L) return a.L < L;
        return a.T < T;
    }
};

int K , N , R;
vector < node > road[105];
priority_queue < node > que;

int Dijkstra()
{
    node sta;
    sta.D = 1, sta.L = 0, sta.T = 0;
    
    que.push(sta);
    
    while(!que.empty())
    {
        node now = que.top();

        que.pop();
        if(now.D == N)
        {
            return now.L;
        }
        for(int i = 0; i < road[now.D].size(); i++)
        {
            if(now.T+road[now.D][i].T <= K)
            {
                node temp;
                temp.D = road[now.D][i].D, temp.L = now.L+road[now.D][i].L, temp.T = now.T+road[now.D][i].T;

                que.push(temp);
            }
        }
    }
    return -1;
}

int main()
{
    cin >> K >> N >> R;

    for(int i = 1; i <= R; i++)
    {
        int sta , fin , len , cost;
        cin >> sta >> fin >> len >> cost;

        node temp;
        temp.D = fin , temp.L = len , temp.T = cost;

        road[sta].push_back(temp);
    }

    cout << Dijkstra() << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43726593/article/details/88407032