题目链接

题意 : 求从1城市到n城市的最短路。但是每条路有两个属性,一个是路长,一个是花费。要求在花费为K内,找到最短路。

思路 :这个题好像有很多种做法,我用了BFS+优先队列。崔老师真是千年不变的SPFA啊,链接。还有一个神用了好几种方法分析,链接 。

用优先队列控制长度,保证每次加的都是最短的,每次从队列中取元素,沿着取出来的点往下找,如果费用比K少再加入队列,否则不加,这样可以省时间。

  1. //
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <iostream>
  5. #include <queue>
  6.  
  7. using namespace std ;
  8.  
  9. int K,N,R ;
  10. int cnt,ans ;
  11. int head[] ;
  12. const int INF = ;
  13.  
  14. struct node
  15. {
  16. int u,v ;
  17. int len ;
  18. int cost ;
  19. int next ;
  20. } st[];
  21. struct node1
  22. {
  23. int len,cost,u ;
  24. friend bool operator < (node1 a,node1 b)
  25. {
  26. return a.len > b.len ;
  27. }
  28. };
  29.  
  30. void addedge(int u,int v,int len,int cost)
  31. {
  32. st[cnt].u = u ;
  33. st[cnt].v = v ;
  34. st[cnt].len = len ;
  35. st[cnt].cost = cost ;
  36. st[cnt].next = head[u] ;
  37. head[u] = cnt ++ ;
  38. }
  39.  
  40. void bfs()
  41. {
  42. priority_queue<node1>Q ;
  43. struct node1 p ;
  44. p.len = ;
  45. p.cost = ;
  46. p.u = ;
  47. Q.push(p) ;
  48. while(!Q.empty())
  49. {
  50. p = Q.top() ;
  51. Q.pop() ;
  52. if(p.u == N)
  53. {
  54. ans = p.len ;
  55. break ;
  56. //return ans ;
  57. }
  58. for(int i = head[p.u] ; i != - ; i = st[i].next)
  59. {
  60. struct node1 st1 ;
  61. st1.u = st[i].v ;
  62. if(p.cost + st[i].cost <= K)
  63. {
  64. st1.cost = p.cost+st[i].cost ;
  65. st1.len = p.len+st[i].len ;
  66. Q.push(st1) ;
  67. }
  68. }
  69. }
  70. }
  71. int main()
  72. {
  73. int S,D,L,T ;
  74. while(scanf("%d %d %d",&K,&N,&R) != EOF)
  75. {
  76. cnt = ;
  77. memset(head,-,sizeof(head)) ;
  78. for(int i = ; i <= R ; i++)
  79. {
  80. scanf("%d %d %d %d",&S,&D,&L,&T) ;
  81. addedge(S,D,L,T) ;
  82. }
  83. ans = INF ;
  84. bfs() ;
  85. if(ans < INF) printf("%d\n",ans) ;
  86. else printf("-1\n") ;
  87. }
  88. return ;
  89. }

POJ 1724 ROADS(BFS+优先队列)的更多相关文章

  1. 深搜+剪枝 POJ 1724 ROADS

    POJ 1724 ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12766   Accepted: 4722 D ...

  2. POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)

    题目链接: https://cn.vjudge.net/problem/POJ-1724 N cities named with numbers 1 ... N are connected with ...

  3. POJ 1724 ROADS(bfs最短路)

    n个点m条边的有向图,每条边有距离跟花费两个参数,求1->n花费在K以内的最短路. 直接优先队列bfs暴力搞就行了,100*10000个状态而已.节点扩充的时候,dp[i][j]表示到达第i点花 ...

  4. poj 1724(最短路+优先队列)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13436   Accepted: 4921 Descriptio ...

  5. POJ:2049Finding Nemo(bfs+优先队列)

    http://poj.org/problem?id=2049 Description Nemo is a naughty boy. One day he went into the deep sea ...

  6. poj 1724 ROADS 很水的dfs

    题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...

  7. Meteor Shower POJ - 3669 (bfs+优先队列)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26455   Accepted: 6856 De ...

  8. poj 1724 ROADS 解题报告

    题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每 ...

  9. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

随机推荐

  1. 10+ powerful debugging tricks with Visual Studio

    10+ powerful debugging tricks with Visual Studio Original link : http://www.codeproject.com/Articles ...

  2. centos6.5 安装mono

    mono是一个在linux下兼容.net的软件.安装之前要把开发包装好 源码安装mono wget http://download.mono-project.com/sources/mono/mono ...

  3. jQuery 判断所有图片加载完成

    对于图片的处理,例如幻灯片播放.缩放等,都是依赖于在所有图片完成之后再进行操作. 今天来看下如何判断所有的图片加载完成,而在加载完成之前可以使用 loading 的 gif 图表示正在加载中. 一.普 ...

  4. VS2015编译错误:调用的目标发生了异常--->此实现不是Windows平台FLPS验证的加密算法的一部分。

    在Win10下安装好几次VS2015(企业版)了,这次发生了一个奇怪的问题,错误截图如下: 控制台.WPF等项目均有此错误!但是ASP.NET项目却可以编译运行!一开始还以为VS2015安装错误,修复 ...

  5. 基于lnmp.org的xdebug安装

    1. 下载xdebug wget http://xdebug.org/files/xdebug-2.3.3.tgz 2. 创建一个目录: mkdir ./xdebug 3. 复制xdebug包到xde ...

  6. 简单的优化处理 By LINQ TO SQL

    最近在做关于新浪微博授权的一些minisite,数据库并不复杂,所以在数据打交道这块采用了linqtosql,开发起来更快更简单...但是随着用户访问逐渐增多,用户上传的图片也越来越多,因为首页是一个 ...

  7. c++异常详解

    一.什么是异常处理 一句话:异常处理就是处理程序中的错误. 二.为什么需要异常处理,以及异常处理的基本思想 C++之父Bjarne Stroustrup在<The C++ Programming ...

  8. mac升级yosemite后安装gd的freetype扩展

    Mac升级系统到 Yosemite 10.10,对于各位Coder来说,还是需要一些时间来折腾的! @星空之下 同学反映 PHPCMS 的验证码图片不能正常显示,反馈该验证码需要GD库支持FreeTy ...

  9. 微信/QQ机器人的实现

    介绍: Mojo-Webqq和Mojo-Weixin是在github上基于webQQ和网页版WeiXin,用Perl语言实现的开源的客户端框架,它通过插件提供基于HTTP协议的api接口供其他语言或系 ...

  10. 人工智能起步-反向回馈神经网路算法(BP算法)

    人工智能分为强人工,弱人工. 弱人工智能就包括我们常用的语音识别,图像识别等,或者为了某一个固定目标实现的人工算法,如:下围棋,游戏的AI,聊天机器人,阿尔法狗等. 强人工智能目前只是一个幻想,就是自 ...