题意:

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;
}

ROADS POJ - 1724(最短路+邻接表+dfs)相关推荐

  1. 分道扬镳 /// 邻接表 DFS 剪枝 oj1332

    题目大意: 编号为1-N 的N个城市之间以单向路连接,每一条道路有两个参数:路的长度和通过这条路需付的费用. Bob和Alice生活在城市1,但是当Bob发现了Alice玩扑克时欺骗他之后,他决定与她 ...

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

    思路:先说下题意,题意第一行给了一个k,代表你有k的钱数,下一行有一个n,代表n个点,然后一个m,代表m条边,然后接下来m行,每行有四个数,分别代表起点.终点.路径长度和要花费的钱数,题目想问在花的钱 ...

  3. 用邻接表dfs和bfs图

    dfs bfs 树是一种特殊的图,与图的存储方式相同. 对于无向图中的边ab,存储两条有向边a->b, b->a. 因此我们可以只考虑有向图的存储. (1) 邻接矩阵:g[a][b] 存储 ...

  4. 魔法宝石(邻接表+dfs更新)

    魔法宝石 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submissi ...

  5. 迷阵突围 (dijkstra求次短路)邻接表,邻接矩阵

    分析:dijkstra算法求最短路问题.先求最短路,再依次删除最短路上的边,每次删除一条,使用dijkstra求此时的最短路,更新最小值,最后得到第二短路.f标记删除与否,抑或便于寻找无向图的两条边. ...

  6. AcWing 3465. 病毒朔源 (邻接表DFS 详解)

    来源:CCCC天梯赛L2-038 病毒容易发生变异. 某种病毒可以通过突变产生若干变异的毒株,而这些变异的病毒又可能被诱发突变产生第二代变异,如此继续不断变化. 现给定一些病毒之间的变异关系,要求你找 ...

  7. hdu2544 最短路-邻接表+优先队列实现dijkstra

    Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要 ...

  8. PAT甲级1004 Counting Leaves (30分):[C++题解]树、邻接表存储树、dfs遍历树

    文章目录 题目分析 题目链接 题目分析 题意重述:一棵树,求每一层的叶子节点数目. 分析 构造树,使用邻接表来存(相当于存储有向图). 需要一个头结点数组h[N],然后每个头节点往外形成一个单链表e[ ...

  9. Invitation Cards POJ - 1511 SPFA(dijkstra+反向建图+邻接表(下标过大)+输入输出用stdio(iostream超时))

    题目大意: 有编号1-P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接 到达终点站,是单向的,每条路线有它自己的车费.有P个人早上从1出发 ,他们要到达每一个公交站点, 然后到了晚上再返回点 ...

最新文章

  1. android post请求添加公共参数_Java实现通用的Get和Post请求组件
  2. ubuntu再次体验之【美化】--修改主题、字体、字体大小
  3. L1-011 A-B(13行代码AC!!)
  4. 图像的全局特征--HOG特征、DPM特征
  5. Python_大众点评网站数据爬虫
  6. 【干货】python多进程和多线程谁更快
  7. 地理住宅区的特点_高三地理复习专题讲解:民居特点与自然环境的关系
  8. queryrunner对于数据库的快速操作
  9. java oracle中文乱码_解决java oracle中文乱码的方法
  10. 台式机连接校园网-UPC-lan-login
  11. linux 显卡 压力测试软件,显卡压力测试工具 GpuTest
  12. 完美兼容IE,chrome,ff的设为首页、加入收藏及保存到桌面js代码
  13. 51单片机C语言波特率十六进制,8051单片机波特率计算公式(配套C语言例程)
  14. 【唐老狮】C#四部曲之C#基础:习题汇总
  15. 【高阶累积量】基于高阶累积量的信噪比盲估计法的matlab仿真
  16. OKhttp 拦截器Intercept token失效验证
  17. 【Java基础系列教程】第八章 Java面向对象详解(三)_抽象类、接口、内部类、深拷贝与浅拷贝
  18. K8S系列:3种Projected Volume
  19. JEEWX微信企业号管家,开源免费,1.0版本发布
  20. JavaSE - 继承

热门文章

  1. C语言试题三十三之比较两个字符串的长度,(不得调用c语言提供的求字符串长度的函数),函数返回较长的字符串。若两个字符串长度相同,则返回第一个字符串。
  2. Android之给ImageView添加点击效果
  3. Tree前序反序列化
  4. (十)python3 只需3小时带你轻松入门——模块与包
  5. 表头合并_多个Excel表格合并数据麻烦?试试Power Query轻松帮你解决
  6. 这才是老公的正确用法,不吃就往死里打......
  7. 我们计划招收300名学员,免费攻读人工智能专业
  8. 《SAS编程与数据挖掘商业案例》学习笔记之七
  9. python 内存数据库下载,Python 文件存储和数据库
  10. python self 值自动改变,在python中对self的理解