ROADS POJ - 1724(限制条件的最短路)【邻接表+深搜】

 

思路:先说下题意,题意第一行给了一个k,代表你有k的钱数,下一行有一个n,代表n个点,然后一个m,代表m条边,然后接下来m行,每行有四个数,分别代表起点、终点、路径长度和要花费的钱数,题目想问在花的钱不超过k的情况下,从1---n的最短路径是多少。

我们用两种方法来解决这个问题,第一种办法是深搜,深搜的思路就是用邻接表建图,然后搜索这个图一直更新步数的最小值,第二种是用优先队列优化的迪杰斯特拉算法,我们用dis[i][j]表示从点1到点i时花费为j的最短距离,然后用优先队列时路径小的先出队。

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<iostream>
#include<string.h>
#define inf 0x3f3f3f3f
using namespace std;

int a,b,c,ans,s[10010],d[10010],l[10010],t[10010],u[10010],v[10010],flag,book[10010];

void dfs(int x,int y,int z)/*x:城市;y:长度;z:money*/
{
    if(y>=ans||z>a)/*达到钱够用,路程最小,循环遍历,找最小*/
        return ;
    if(x==b)
    {
        ans=y;
        flag=1;
        return;
    }
    for(int i=u[x]; i!=-1; i=v[i]) /*遍历该节点的所有边,知道节点,即可知边的长度,钱,以及下个节点*/
    {
        if(!book[d[i]])
        {
            book[d[i]]=1;
            dfs(d[i],y+l[i],z+t[i]);
            book[d[i]]=0;
        }
    }
    return ;
}
int main()
{
    while(cin>>a>>b>>c)
    {
        flag=0;
        ans=inf;
        memset(book,0,sizeof(book));
        memset(u,-1,sizeof(u));/*care与上面调用领接表i!=-1有关*/
        for(int i=0; i<c; i++)
        {
            cin>>s[i]>>d[i]>>l[i]>>t[i];
            v[i]=u[s[i]];//插入链表,表示每个节点连接的所有边,将节点插到链表首部
            u[s[i]]=i;
        }
        dfs(1,0,0);
        if(flag)
            cout<<ans<<endl;
        else
            cout<<"-1"<<endl;
    }
    return 0;
}
代码2(Dijkstra):

#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 10000007
#define N 100+20
#define M 1000000+10
#define ll long long
using namespace std;
int k,n,m,len,sum;
int first[N],dis[N][10005];
struct node1
{
    int v,dis,cost,next;
} g[10010];
struct node
{
    int num,dis,cost;
    bool friend operator < (node a,node b)//重载运算符
    {
        return a.dis>b.dis;
    }
};
void add_edge(int u,int v,int dis,int cost)//邻接表建图
{
    g[len].v=v;
    g[len].dis=dis;
    g[len].cost=cost;
    g[len].next=first[u];
    first[u]=len++;
}
void dijkstra()
{
    for(int i=1; i<=n; i++)
        for(int j=0; j<=k; j++)
            dis[i][j]=inf;//dis[i][j]表示从点1到点i花费的钱数为j的最短距离
    dis[1][0]=0;
    priority_queue<node>q;
    node now,to;
    now.num=1;
    now.cost=0;
    now.dis=0;
    q.push(now);
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        if(now.num==n)//找到的时候直接输出并返回
        {
            printf("%d\n",now.dis);
            return;
        }
        for(int i=first[now.num]; i!=-1; i=g[i].next)
        {
            int cost=now.cost+g[i].cost;
            if(cost>k)continue;
            if(dis[g[i].v][cost]>now.dis+g[i].dis)//松弛条件
            {
                dis[g[i].v][cost]=now.dis+g[i].dis;
                to.num=g[i].v;
                to.cost=cost;
                to.dis=dis[g[i].v][cost];
                q.push(to);
            }
        }
    }
    puts("-1");
}
int main()
{
    len=0;
    int a,b,c,d;
    mem(first,-1);
    scanf("%d%d%d",&k,&n,&m);
    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d%d",&a,&b,&c,&d);
        add_edge(a,b,c,d);
    }
    dijkstra();
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值