ROADS POJ - 1724(最短路+邻接表+dfs)

题意:

N个城市,编号1到N。城市间有R条单向道路。有长度和过路费两个属性。Bob只有K块钱,他想从城市1走到城市N。问最短共需要走多长的路。如果到不了N,输出-1。

题目:

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到城市n,求最短距离,要是这样就是一道简单的最短路问题,但题目有限制要求,在满足花费即不超过k的情况下的最短路
1.首先考虑得用搜索,迭代寻找,在找到所有满足路径后,选出最短的(注意剪枝)。
2.选用邻接表来存储无向图的时间空间复杂度是O(M)。
如果只是查询两个点之间是否相邻,邻接矩阵当然更快,但如果是做dfs的话,找当前节点相邻的点,如果用邻接矩阵的话每次都要从1扫到n,如果用邻接表遍历每个顶点的边

AC代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int M=1e4+10;
int n,m,k;
ll ans;
int a[M],b[M],c[M],d[M],fir[M],nex[M],book[M];
void dfs(int x/*当前到达城市*/,int y/*费用*/,int z/*路程*/)
{if(y>n||z>=ans)return ;if(x==m){ans=z;return ;}for(int i=fir[x];i;i=nex[i])if(!book[i]){book[i]=1;dfs(b[i],y+d[i],z+c[i]);book[i]=0;}return ;
}
int main()
{scanf("%d%d%d",&n,&m,&k);for(int i=1;i<=k;i++)/*care 变量要从1开始,邻接表定义*/{scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);nex[i]=fir[a[i]];fir[a[i]]=i;}ans=inf;dfs(1,0,0);if(ans==inf) printf("-1\n");else printf("%lld\n",ans);return 0;               
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/309654.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MongoDB副本集

参考&#xff1a;https://www.cnblogs.com/littleatp/p/8562842.html https://www.cnblogs.com/ilifeilong/p/14347008.html MongoDB副本集 MongoDB副本集是由一组Mongod实例&#xff08;进程&#xff09;组成&#xff0c;包含一个Primary节点和多个Secondary节点。客户端的所…

博客系统知多少:揭秘那些不为人知的学问(一)

点击上方蓝字关注“汪宇杰博客”导语在我们生活的年代&#xff0c;博客并不稀奇&#xff0c;甚至可以说是随处可见。从最早的搜狐、新浪博客&#xff0c;再到每个人都曾记录青春的 QQ 空间&#xff0c;再到现在的 Vlog 与 Plog&#xff0c;似乎拥有一个自己的博客并不是什么难事…

MongoDB 分片

MongoDB 分片 高数据量&#xff08;消耗内存&#xff09;和高吞吐量&#xff08;消耗CPU&#xff09;的数据库应用会对单机的性能造成较大压力&#xff0c;为了解决这些问题&#xff0c;一般采用两种方法&#xff1a;水平扩展&#xff08;将数据集分布在多个服务器上&#xff…

How many ways HDU - 1978(记忆化搜索关于求多少种方式模板)

题目&#xff1a; 这是一个简单的生存游戏&#xff0c;你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m)。游戏的规则描述如下&#xff1a; 1.机器人一开始在棋盘的起始点并有起始点所标有的能量。 2.机器人只能向右或者向下走&#xff0c;并且每走一步消耗一单位…

Sql Server之旅——第七站 复合索引和include索引到底有多大区别?

索引和锁&#xff0c;这两个主题对我们开发工程师来说&#xff0c;非常的重要。。。只有理解了这两个主题&#xff0c;我们才能写出高质量的sql语句&#xff0c;在之前的博客中&#xff0c;我所说的索引都是单列索引。。。当然数据库不可能只认单列索引&#xff0c;还有我这篇的…

Go中new和make的区别

Go中new和make的区别 变量声明 当我们声明变量时可以使用var关键字&#xff0c;当不指定变量的默认值时&#xff0c;这些变量的默认值就是他们的零值&#xff0c;比如int的默认值为0&#xff0c;string的默认值为""&#xff0c;引用类型的零值为nil。 但是当我们在…

POJ 3159 Candies(差分约束+SPAF)

题意&#xff1a; 给n个小朋友分发糖果&#xff0c;但小朋友们之间有嫉妒心。接下来m行&#xff0c;每行三个数&#xff0c;分别表示小朋友A希望B得到的糖果不能比他多x个。要求你计算在满足所有小朋友的条件的情况下最多需要准备多少颗糖。 题目&#xff1a; During the ki…

掌握了Docker Layer Caching才敢自称精通Dockerfile

长话短说&#xff1a;本次原创将向您展示在Docker中使用Layer Cache以加快镜像构建。“这个话题的初衷在于&#xff1a;应用打包过程是很慢的(下载并安装框架&第三方依赖包、生成assets)&#xff0c;这个过程在Docker中也不能避免。About Layer Caching in DockerDocker使…

Go unsafe Pointer

Go unsafe Pointer Go被设计为一种强类型的静态语言&#xff0c;强类型意味着类型一旦确定就无法更改&#xff0c;静态意味着类型检查在运行前就做了。 指针类型转换 为了安全考虑&#xff0c;两个不同类型的指针不能相互转换&#xff0c;例如&#xff1a; package mainfun…

How Many Answers Are Wrong HDU - 3038(带权并查集)

题意&#xff1a; TT写一个数列&#xff0c;现在TT会选择一个区间&#xff0c;然后让FF计算这个区间里面所有数的和&#xff0c;FF准备捉弄一下TT&#xff0c;有时候她会故意计算出来一个错的答案&#xff0c;当然TT也比较聪明&#xff0c;他会发现这个答案跟以前的答案会有冲…

ASP.NET Core on K8s学习之旅(14)Ingress灰度发布

【云原生】| 作者/Edison Zhou这是恰童鞋骚年的第236篇原创文章上一篇介绍了Ingress的基本概念和Nginx Ingress的基本配置和使用&#xff0c;然后我还录了一个快速分享小视频介绍了一下蓝绿发布和灰度发布策略的基本概念&#xff0c;本篇介绍一下如何实战使用Nginx Ingress实现…

[汇编语言]实验:应用更灵活的寻址方式来定位内存地址

实验内容: &#xff08;1&#xff09;将datasg段中每个单词的头一个字母改成大写字母。 datasg段中的数据为: &#xff08;2&#xff09; 将datasg段中每个单词的字母改成大写字母。 datasg段中的数据为: ibm dec dos vax …

Redundant Paths POJ - 3177(tarjan+边双连通分量)

题意&#xff1a; 有n个牧场&#xff0c;要求从一个牧场到另一个牧场&#xff0c;要求至少要有2条独立的路可以走。现已有m条路&#xff0c;求至少要新建多少条路&#xff0c;使得任何两个牧场之间至少有两条独立的路。两条独立的路是指&#xff1a;没有公共边的路&#xff0c…

Slice的本质

Slice的本质 我们先看下面的代码&#xff0c;看看它的输出是什么&#xff1a; package mainimport "fmt"type Slice []intfunc (A Slice) Append(value int) {A append(A, value) } func main() {mSlice : make(Slice, 10, 20)mSlice.Append(5)fmt.Println(mSlice…

你需要了解操作系统发展历程

本文我们大概回顾计算机操作系统发展历程&#xff0c;这里不会记录关于操作系统的完整历史记录&#xff0c;只是记录那些里程碑事件&#xff0c;看看各位接触计算机时&#xff0c;操作系统发展正处于哪个年代起初没有操作系统&#xff0c;没有编程语言或编译器&#xff0c;甚至…

[汇编语言]实验:更灵活的寻址方式 -应用si和di

实验内容: &#xff08;1&#xff09; 用寄存器SI和DI实现将字符串‘welcome to masm!’ 复制到它后面的数据区中。 &#xff08;2&#xff09; 用[bx(si或di)idata]的方式&#xff0c;来使程序变得简洁。 &#xff08;1&#xff09; 代码如下: assume ds:datasg,cs:code…