POJ 1724 ROADS(優先佇列+spfa)

畫船聽雨發表於2014-04-10

就是給你n個點,m條邊。然後找到錢最少的情況下,走過了多少路。一個點有可能多次走過,搜的時候保證到達每個點的花費值最小。

ROADS
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10122   Accepted: 3750

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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
#define LL __int64
//#define LL long long
#define INF 0x3fffffff
#define PI 3.1415926535898

using namespace std;

const int maxn = 10005;

int head[maxn];
int n, m, ans;
int total;
int num[maxn];

struct node1
{
    int d, l, t;
    int next;
} f[maxn];


struct node
{
    int n, d, c;
    bool operator < (const struct node a)const
    {
        if(a.d == d) return a.c < c;
        return a.d < d;
    }
};

void add(int s, int d, int l, int t)
{
    f[ans].d = d;
    f[ans].t = t;
    f[ans].l = l;
    f[ans].next = head[s];
    head[s] = ans++;
}
void bfs()
{
    priority_queue<node>que;
    struct node temp, xtemp;
    int cnt = INF;
    temp.d = 0;
    temp.n = 1;
    temp.c = 0;
    while(!que.empty())
        que.pop();
    que.push(temp);
    while(!que.empty())
    {
        temp = que.top();
        que.pop();
        if(temp.n == n)
        {
            cnt = temp.d;
            break;
        }
        int p = head[temp.n];
        while(p != -1)
        {
            if(temp.c+f[p].t <= total)
            {
                xtemp.n = f[p].d;
                xtemp.c = temp.c+f[p].t;
                xtemp.d = temp.d+f[p].l;
                que.push(xtemp);
            }
            p = f[p].next;
        }
    }
    if(cnt == INF)
        cout<<"-1"<<endl;
    else
        cout<<cnt<<endl;
}

int main()
{
    while(cin >>total>>n>>m)
    {
        int s, d, l, t;
        memset(head, -1, sizeof(head));
        ans = 0;
        for(int i = 0; i < m; i++)
        {
            cin >>s>>d>>l>>t;
            add(s, d, l, t);
        }
        bfs();
    }
    return 0;
}


相關文章