Poj 1724 ROADS【二维限制最短路+SPFA】

ROADS

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 12792

 

Accepted: 4735

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

 

题目大意:

最开始有k个点数,n个点,m条有向边,对于每一条有向边,有四个元素的输入,前两个代表起点和终点,后两个元素代表,路径长度和花费点数,如果没有路径能够从1到n,输出-1,否则输出最短路径。


思路:(针对SPFA算法来讲)


1、有点数限制的最短路,明显开一维的最短路数组dis【】和一维判当前情况是否在队列中数组vis【】是不对的,因为对于一个点不同的剩余点数是需要不同边来进行松弛和继续向下进行的。所以开一维数组vis【】显然会误判很多种能够继续松弛下去的情况。


2、所以我们开二维最短路数组dis【i】【j】表示在现在走到点i剩余点数为j的最短路径值,那么vis【i】【j】其中的i,j也是同样的含义。这个时候我们只要在基础的一维SPFA求最短路过程中的判断当前情况是否在队列中的条件从一维改成二维即可。


AC代码:


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int head[100000];
struct EdgeNode
{
    int to;
    int w;
    int cost;
    int next;
}e[100000];
struct node
{
    int u,val;
}now,nex;
int dis[105][10050];
int vis[105][10050];
int k,n,m,cont;
void add(int from,int to,int w,int cost)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].cost=cost;
    e[cont].next=head[from];
    head[from]=cont++;
}
void SPFA(int ss)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=k;j++)
        {
            dis[i][j]=0x3f3f3f3f;
            vis[i][j]=0;
        }
    }
    now.u=ss;
    now.val=k;
    queue<node>s;
    s.push(now);
    dis[now.u][now.val]=0;
    vis[now.u][now.val]=1;
    while(!s.empty())
    {
        now=s.front();
        s.pop();
        vis[now.u][now.val]=0;
        int u=now.u,val=now.val;
        for(int k=head[u];k!=-1;k=e[k].next)
        {
            int v=e[k].to;
            int cost=e[k].cost;
            int w=e[k].w;
            if(val-cost<0)continue;
            if(dis[v][val-cost]>dis[u][val]+w)
            {
                dis[v][val-cost]=dis[u][val]+w;
                if(vis[v][val-cost]==0)
                {
                    vis[v][val-cost]=1;
                    nex.u=v;
                    nex.val=val-cost;
                    s.push(nex);
                }
            }
        }
    }
    int output=0x3f3f3f3f;
    for(int i=0;i<=k;i++)
    {
        output=min(output,dis[n][i]);
    }
    if(output==0x3f3f3f3f)
    printf("-1\n");
    else printf("%d\n",output);
}
int main()
{
    cont=0;
    memset(head,-1,sizeof(head));
    scanf("%d%d%d",&k,&n,&m);
    for(int i=0;i<m;i++)
    {
        int x,y,len,cost;
        scanf("%d%d%d%d",&x,&y,&len,&cost);
        add(x,y,len,cost);
    }
    SPFA(1);
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值