POJ 1724 ROADS

ROADS
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9118 Accepted: 3383

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

Source

CEOI 1998
  用spfa即可
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define N1 101
#define N2 10001
#define INF 0x7ffffff
using namespace std;
bool inque[N1][N2];
int sum[N1][N2];
class num
{
    public:
    int id,take;
    num(int l,int r):id(l),take(r){}
};
struct link
{
    int e,take,len,next;
}a[N2];
int b[N1],k,n,m,Top;
int main()
{
    //freopen("data1.in","r",stdin);
    void addeage(int s,int d,int l,int t);
    int spfa();
    while(scanf("%d %d %d",&k,&n,&m)!=EOF)
    {
        Top = 0;
        memset(b,-1,sizeof(b));
        for(int i=0;i<=m-1;i++)
        {
            int s,d,l,t;
            scanf("%d %d %d %d",&s,&d,&l,&t);
            addeage(s,d,l,t);
        }
        int t=spfa();
        printf("%d\n",t);
    }
    return 0;
}
void addeage(int s,int d,int l,int t)
{
    a[Top].e = d;
    a[Top].len = l;
    a[Top].take = t;
    a[Top].next = b[s];
    b[s] = Top;
    Top++;
}
int spfa()
{
    memset(inque,false,sizeof(inque));
    for(int i = 1;i <= n; i++)
    {
        for(int j=0; j <= k; j++)
        {
            sum[i][j] = INF;
        }
    }
    queue<num> que;
    que.push(num(1,0));
    inque[1][0] = true;
    sum[1][0] = 0;
    while(!que.empty())
    {
        num tag =  que.front();
        int id1 = tag.id;
        int take1 = tag.take;
        que.pop();
        inque[id1][take1] = false;
        for(int i = b[id1]; i!=-1; i=a[i].next)
        {
            int id2 = a[i].e;
            int len2 = a[i].len;
            int take2 = a[i].take;
            if(take1+take2<=k&&sum[id2][take1+take2]>sum[id1][take1]+len2)
            {
                sum[id2][take1+take2]=sum[id1][take1]+len2;
                if(!inque[id2][take1+take2])
                {
                    que.push(num(id2,take1+take2));
                    inque[id2][take1+take2] = true;
                }
            }
        }
    }
    int Min = INF;
    for(int i=0;i<=k;i++)
    {
        Min = min(sum[n][i],Min);
    }
    if(Min==INF)
    {
        return -1;
    }
    return Min;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值