POJ 1724 ROADS(BFS+优先队列)
题意 : 求从1城市到n城市的最短路。但是每条路有两个属性,一个是路长,一个是花费。要求在花费为K内,找到最短路。
思路 :这个题好像有很多种做法,我用了BFS+优先队列。崔老师真是千年不变的SPFA啊,链接。还有一个神用了好几种方法分析,链接 。
用优先队列控制长度,保证每次加的都是最短的,每次从队列中取元素,沿着取出来的点往下找,如果费用比K少再加入队列,否则不加,这样可以省时间。
- //
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- #include <queue>
- using namespace std ;
- int K,N,R ;
- int cnt,ans ;
- int head[] ;
- const int INF = ;
- struct node
- {
- int u,v ;
- int len ;
- int cost ;
- int next ;
- } st[];
- struct node1
- {
- int len,cost,u ;
- friend bool operator < (node1 a,node1 b)
- {
- return a.len > b.len ;
- }
- };
- void addedge(int u,int v,int len,int cost)
- {
- st[cnt].u = u ;
- st[cnt].v = v ;
- st[cnt].len = len ;
- st[cnt].cost = cost ;
- st[cnt].next = head[u] ;
- head[u] = cnt ++ ;
- }
- void bfs()
- {
- priority_queue<node1>Q ;
- struct node1 p ;
- p.len = ;
- p.cost = ;
- p.u = ;
- Q.push(p) ;
- while(!Q.empty())
- {
- p = Q.top() ;
- Q.pop() ;
- if(p.u == N)
- {
- ans = p.len ;
- break ;
- //return ans ;
- }
- for(int i = head[p.u] ; i != - ; i = st[i].next)
- {
- struct node1 st1 ;
- st1.u = st[i].v ;
- if(p.cost + st[i].cost <= K)
- {
- st1.cost = p.cost+st[i].cost ;
- st1.len = p.len+st[i].len ;
- Q.push(st1) ;
- }
- }
- }
- }
- int main()
- {
- int S,D,L,T ;
- while(scanf("%d %d %d",&K,&N,&R) != EOF)
- {
- cnt = ;
- memset(head,-,sizeof(head)) ;
- for(int i = ; i <= R ; i++)
- {
- scanf("%d %d %d %d",&S,&D,&L,&T) ;
- addedge(S,D,L,T) ;
- }
- ans = INF ;
- bfs() ;
- if(ans < INF) printf("%d\n",ans) ;
- else printf("-1\n") ;
- }
- return ;
- }
POJ 1724 ROADS(BFS+优先队列)的更多相关文章
- 深搜+剪枝 POJ 1724 ROADS
POJ 1724 ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12766 Accepted: 4722 D ...
- POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)
题目链接: https://cn.vjudge.net/problem/POJ-1724 N cities named with numbers 1 ... N are connected with ...
- POJ 1724 ROADS(bfs最短路)
n个点m条边的有向图,每条边有距离跟花费两个参数,求1->n花费在K以内的最短路. 直接优先队列bfs暴力搞就行了,100*10000个状态而已.节点扩充的时候,dp[i][j]表示到达第i点花 ...
- poj 1724(最短路+优先队列)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13436 Accepted: 4921 Descriptio ...
- POJ:2049Finding Nemo(bfs+优先队列)
http://poj.org/problem?id=2049 Description Nemo is a naughty boy. One day he went into the deep sea ...
- poj 1724 ROADS 很水的dfs
题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...
- Meteor Shower POJ - 3669 (bfs+优先队列)
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26455 Accepted: 6856 De ...
- poj 1724 ROADS 解题报告
题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每 ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
随机推荐
- 10+ powerful debugging tricks with Visual Studio
10+ powerful debugging tricks with Visual Studio Original link : http://www.codeproject.com/Articles ...
- centos6.5 安装mono
mono是一个在linux下兼容.net的软件.安装之前要把开发包装好 源码安装mono wget http://download.mono-project.com/sources/mono/mono ...
- jQuery 判断所有图片加载完成
对于图片的处理,例如幻灯片播放.缩放等,都是依赖于在所有图片完成之后再进行操作. 今天来看下如何判断所有的图片加载完成,而在加载完成之前可以使用 loading 的 gif 图表示正在加载中. 一.普 ...
- VS2015编译错误:调用的目标发生了异常--->此实现不是Windows平台FLPS验证的加密算法的一部分。
在Win10下安装好几次VS2015(企业版)了,这次发生了一个奇怪的问题,错误截图如下: 控制台.WPF等项目均有此错误!但是ASP.NET项目却可以编译运行!一开始还以为VS2015安装错误,修复 ...
- 基于lnmp.org的xdebug安装
1. 下载xdebug wget http://xdebug.org/files/xdebug-2.3.3.tgz 2. 创建一个目录: mkdir ./xdebug 3. 复制xdebug包到xde ...
- 简单的优化处理 By LINQ TO SQL
最近在做关于新浪微博授权的一些minisite,数据库并不复杂,所以在数据打交道这块采用了linqtosql,开发起来更快更简单...但是随着用户访问逐渐增多,用户上传的图片也越来越多,因为首页是一个 ...
- c++异常详解
一.什么是异常处理 一句话:异常处理就是处理程序中的错误. 二.为什么需要异常处理,以及异常处理的基本思想 C++之父Bjarne Stroustrup在<The C++ Programming ...
- mac升级yosemite后安装gd的freetype扩展
Mac升级系统到 Yosemite 10.10,对于各位Coder来说,还是需要一些时间来折腾的! @星空之下 同学反映 PHPCMS 的验证码图片不能正常显示,反馈该验证码需要GD库支持FreeTy ...
- 微信/QQ机器人的实现
介绍: Mojo-Webqq和Mojo-Weixin是在github上基于webQQ和网页版WeiXin,用Perl语言实现的开源的客户端框架,它通过插件提供基于HTTP协议的api接口供其他语言或系 ...
- 人工智能起步-反向回馈神经网路算法(BP算法)
人工智能分为强人工,弱人工. 弱人工智能就包括我们常用的语音识别,图像识别等,或者为了某一个固定目标实现的人工算法,如:下围棋,游戏的AI,聊天机器人,阿尔法狗等. 强人工智能目前只是一个幻想,就是自 ...