ROADS

题目:

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

题意:

有n个点(编号为1,2,3,,,n),有m条单向路,每条路有相应的长度 l ,以及每条路的花费 t ;

一个人要从1号点走到n号点,问在不超过花费k的情况下最短路是多少。

思路:

可以直接用深度优先搜索,只不过在访问每条边时可能会超时,所以用到用数组模拟邻接表。

代码:

#include<stdio.h>
#include<string.h>
struct node
{
    int s,d,l,t;
    int next;
}e[10086];
int head[10086],minn,book[10086];
int k,n,m;
void dfs(int x,int s1,int s2)
{//x代表现在的位置,s1代表已经经过的路程,s2表示目前还剩多少钱。
    if(x==n)
    {
        if(s1<minn)
            minn=s1;
        return ;
    }
    for(int i=head[x];i!=-1;i=e[i].next)//跳跃式访问节省时间
    {
    /*  如果这条路中的目标点没访问过,
        且目前访问的路程加上这条路的长度不大于最小长度,
        且加上这条路的花费不大于开始拥有的钱数
    */
        if(!book[e[i].d]&&s1+e[i].l<minn&&s2-e[i].t>=0)
        {
            book[e[i].d]=1;
            dfs(e[i].d,s1+e[i].l,s2-e[i].t);
            book[e[i].d]=0;
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&k,&n,&m))
    {
        minn=99999999;
        memset(head,-1,sizeof(head));
        memset(book,0,sizeof(book));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d%d",&e[i].s,&e[i].d,&e[i].l,&e[i].t);
            e[i].next=head[e[i].s];//用数组模拟邻接表,
            //重新再定义一个next数组也是可以的,为了方便就定义在结构体中。
            head[e[i].s]=i;
        }
        book[1]=1;//标记1号点已经来过
        dfs(1,0,k);
        if(minn==99999999)
            printf("-1\n");
        else
            printf("%d\n",minn);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/admin__/article/details/81280439