程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 1724 ROADS

POJ 1724 ROADS

編輯:C++入門知識

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

11Source

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;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved