思路:先说下题意,题意第一行给了一个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;elsecout<<"-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;
}

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

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

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

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

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

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

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

  4. poj 1724 有限制的最短距离(优先队列+链表)

    题目链接:http://poj.org/problem?id=1724 题目大意:给你一个最大费用,让你在费用容许的范围内(情况下)求有源点到终点的最短距离,也就是一般的单源最小距离,不同之处在于加了 ...

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

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

  6. 邻接矩阵和邻接表_[力扣743] 带权邻接表的单源最短路

    题目链接 743. 网络延迟时间 题目描述 有 N 个网络节点,标记为 1 到 N. 给定一个列表 times,表示信号经过有向边的传递时间. times[i] = (u, v, w),其中 u 是源 ...

  7. dijkstra邻接表_[力扣743] 带权邻接表的单源最短路

    题目链接 743. 网络延迟时间 题目描述 有 N 个网络节点,标记为 1 到 N. 给定一个列表 times,表示信号经过有向边的传递时间. times[i] = (u, v, w),其中 u 是源 ...

  8. SPFA求单源最短路(邻接表)

    SPFA 介绍 SPFA(Shortest Path Faster Algorithm)(队列优化)算法是求单源最短路径的一种算法,在Bellman-ford算法的基础上加上一个队列优化,减少了冗余的 ...

  9. HDU 4725 层级最短路 思维建图 邻接表 堆优化迪杰斯特拉 真的难想 区域网络赛真题

    题目 题解思路 知道是最短路,怎么建图呢? 一开始想到每层来一个超级源点,但是方向不知道怎么确定,用双向边果然WA了(如果是双向边,那一层的都会变成0费连通了 ). 翻了翻 大佬的博客 大佬定义了一种 ...

最新文章

  1. jwt怎么获取当前登录用户_spring oauth2如何获取当前登录用户信息
  2. OpenMV生成AprilTag码
  3. 给Python加Markdown式排版,在线运行可做Jupyter替身丨谷歌大脑出品
  4. 算法导论6.1-2习题解答
  5. oracle创建用户、授予权限及删除用户
  6. Database之SQLSever:SQLSever数据库管理学习并深入理解SQL命令语句进阶综合篇《初级→中级→高级》(持续更新,建议收藏)
  7. android下拉会谈效果,Android实现下拉展示条目效果
  8. AT2390-[AGC016F]Games on DAG【状压dp,SG函数】
  9. php助手函数自定义,Laravel 添加自定义助手函数
  10. 省份城市区县三级联动html代码,基于Jquery实现省份、城市、区县三级联动
  11. shell学习脚本-tomcat停止脚本
  12. 开源不等于免费!谷歌如何通过安卓开源成为移动时代霸主? | 涛滔不绝
  13. 判断回文(0315)SWUST-OJ
  14. 异步任务利器Celery(一)介绍
  15. POJ 3237 Tree (树链拆分)
  16. 阿里云CDN、DCDN、SCDN的区别
  17. 阿里开放平台接入——开放平台注册与API调用
  18. java程序步骤_java编写程序的步骤是什么?java编写程序步骤实例讲解
  19. 第五章 习惯三 要事第一——自我管理原则
  20. 【C语言典例】——day3:设计魔方阵(数组)

热门文章

  1. IOS学习笔记二十NSSet和NSMutableSet
  2. nodejs项目_多人群聊实现其实很简单:Nodejs+WebSocket+Vue轻松实现Web IM
  3. python绘制饼图双层_有趣!如何用Python-matplotlib绘制双层饼图及环形图?
  4. @order注解_别再用ifelse了,用注解去代替他吧
  5. 有什么看起来很难,但是其实很简单的题目
  6. 【完整版】当大师遇到了理工男,只能吐血了...
  7. 都说Python库千千万,这几个你认识不?
  8. 2017年终奖发放,程序员人均11776元排名第一!
  9. 今天,送你一份交通行业最全数据集(共享单车、自动驾驶、网约出租车、交通信号识别)
  10. 服务器文件每天备份重新命名,定时备份服务器文件至本地电脑