POJ 1724 ROADS

本文探讨了在有限资金下,如何寻找从起点城市到终点城市的最短路径。提供了两种算法实现方式:深度优先搜索(DFS)与优先队列(使用广度优先搜索BFS思想)。通过对输入数据的解析和算法流程的详细解释,帮助读者理解并实现这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ROADS
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10202 Accepted: 3786

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

有n个城市,m条路,初始时你有k个金币,每条路都有一个长度以及经过这条路的花费,问在你能够承受的花费之内,从1号城市到n号城市之间的最短距离是多少。
可以用dfs进行递归回溯也可以用bfs优先队列,事实表明,优先队列的效率比较高。
dfs就是从1号开始进行逐层递归遍历所有情况,一旦金币数量不能满足花费,就退出递归,直到找到最短的路径。
优先队列是按照路径从小到大排序,那么没找到一个符合条件的点就加入到队列中,直至找到n号点。
//332K	94MS
#include<stdio.h>
#include<string.h>
#define M 107
#define inf 0x3f3f3f
int head[M],vis[M],nodes,minlen;
int n,m,k;
struct E
{
    int u,v,l,c,next;
} edge[M*M];
void addedge(int u,int v,int l,int c)
{
    edge[nodes].u=u;
    edge[nodes].v=v;
    edge[nodes].l=l;
    edge[nodes].c=c;
    edge[nodes].next=head[u];
    head[u]=nodes++;
}
void dfs(int city,int nowlen,int nowmoney)
{
    if(nowlen>minlen)return;
    if(city==n&&nowlen<minlen&&nowmoney>=0)
    {
        minlen=nowlen;
        return;
    }
    for(int i=head[city]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        int l=edge[i].l;
        int c=edge[i].c;
        if(!vis[v]&&nowmoney>=c)
        {
            vis[v]=1;
            dfs(v,nowlen+l,nowmoney-c);
            vis[v]=0;
        }
    }
    return;
}
int main()
{
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    nodes=0,minlen=inf;
    scanf("%d%d%d",&k,&n,&m);
    int u,v,l,c;
    while(m--)
    {
        scanf("%d%d%d%d",&u,&v,&l,&c);
        addedge(u,v,l,c);
    }
    dfs(1,0,k);
    if(minlen==inf)printf("-1\n");
    else
        printf("%d\n",minlen);
}

//1028K	32MS
#include<stdio.h>
#include<string.h>
#include<queue>
#define M 1007
using namespace std;
int head[M],nodes;
int k,n,m;
struct Q
{
    int pos,len,money;
    bool operator<(const Q t)const
    {
        return t.len<len;
    }
} ;
struct node
{
    int u,v,next,l,c;
}edge[M*M];
void addedge(int u,int v,int l,int c)
{
    edge[nodes].u=u;
    edge[nodes].v=v;
    edge[nodes].l=l;
    edge[nodes].c=c;
    edge[nodes].next=head[u];
    head[u]=nodes++;
}
int bfs(int u)
{
    priority_queue<Q>q;
    Q now,next;
    now.pos=1;
    now.len=0;
    now.money=0;
    q.push(now);
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        int pos=now.pos,len=now.len,money=now.money;
        if(pos==n&&money<=k)return len;
        for(int i=head[pos];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            int l=edge[i].l;
            int c=edge[i].c;
            if(money+c<=k)
            {
                next.pos=v;
                next.len=len+l;
                next.money=money+c;
                q.push(next);
            }
        }
    }
    return -1;
}
int main()
{
    scanf("%d%d%d",&k,&n,&m);
    nodes=0;
    memset(head,-1,sizeof(head));
    int u,v,l,c;
    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d%d",&u,&v,&l,&c);
        addedge(u,v,l,c);
    }
    int ans=bfs(1);
    printf("%d\n",ans);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值