ROADS
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10742   Accepted: 3949

Description

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

  1. 5
  2. 6
  3. 7
  4. 1 2 2 3
  5. 2 4 3 3
  6. 3 4 2 4
  7. 1 3 4 1
  8. 4 6 2 1
  9. 3 5 2 0
  10. 5 4 3 2

Sample Output

  1. 11
  1. 解决方式:此题我是这样做的,用上优先队列,在费用可行的情况下,不断松弛路径。事实上也相当于bfs+优先队列。首先路径最短的优先级最高,其次是花费,通过不断的把符合费用要求能到达的点增加优先队列,每次出队即更新能到达的点。最后假设出队的点是N,算法结束,得到的路径既是在花费符合的情况下最短的,这题考察的是能不能深刻理解dijkstra的原理,并运用。
  1. code:
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<queue>
  7. #define MMAX 10003
  8. #define Max 103
  9. using namespace std;
  10. int K,N,R,k;
  11. int head[Max];
  12. struct edge
  13. {
  14.     int from,to,len,cost;
  15.     int next;
  16. } E[MMAX];
  17. struct stay
  18. {
  19.     int dis,cost,x;
  20.     bool operator<(const stay &s)const
  21.     {
  22.         if(dis!=s.dis)
  23.         {
  24.             return dis>s.dis;
  25.         }
  26.         else return cost>s.cost;
  27.     }
  28. };
  29. void add(int from,int to,int len,int cost)
  30. {
  31.     E[k].from=from;
  32.     E[k].to=to;
  33.     E[k].len=len;
  34.     E[k].cost=cost;
  35.     E[k].next=head[from];
  36.     head[from]=k++;
  37. }
  38. int dijkstra()
  39. {
  40.     priority_queue<stay> Q;
  41.     stay in;
  42.     in.dis=0,in.cost=0,in.x=1;
  43.     Q.push(in);
  44.     while(!Q.empty())
  45.     {
  46.         stay out=Q.top();
  47.         if(out.x==N) {return out.dis;}
  48.         Q.pop();
  49.         for(int v=head[out.x]; v!=-1; v=E[v].next)
  50.         {
  51.             if(out.cost+E[v].cost<=K)
  52.             {
  53.                stay temp;
  54.                temp.x=E[v].to;
  55.                temp.dis=out.dis+E[v].len;
  56.                temp.cost=out.cost+E[v].cost;
  57.                Q.push(temp);
  58.             }
  59.         }
  60.     }
  61. }
  62. int main()
  63. {
  64.     while(~scanf("%d%d%d",&K,&N,&R))
  65.     {
  66.         memset(head,-1,sizeof(head));
  67.         k=0;
  68.         int from,to,len,cost;
  69.         for(int i=0; i<R; i++)
  70.         {
  71.             scanf("%d%d%d%d",&from,&to,&len,&cost);
  72.             add(from,to,len,cost);
  73.         }
  74.         printf("%d\n",dijkstra());
  75.     }
  76.     return 0;
  77. }
  78.  

ROADS+dijkstra的灵活运用+POJ的更多相关文章

  1. Roads in the North POJ - 2631

    Roads in the North POJ - 2631 Building and maintaining roads among communities in the far North is a ...

  2. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  3. Dijkstra算法:POJ No 3268 Silver Cow Party

    题目:http://poj.org/problem?id=3268 题解:使用 priority_queue队列对dijkstra算法进行优化 #include <iostream> #i ...

  4. 【lightoj-1002】Country Roads(dijkstra变形)

    light1002:传送门 [题目大意] n个点m条边,给一个源点,找出源点到其他点的‘最短路’ 定义:找出每条通路中最大的cost,这些最大的cost中找出一个最小的即为‘最短路’,dijkstra ...

  5. Light oj 1002 Country Roads (Dijkstra)

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1002 题目描述: 有n个城市,从0到n-1开始编号,n个城市之间有m条边,中 ...

  6. Codeforces 806 D. Perishable Roads Dijkstra

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF806D.html 题目传送门 - CF806D 题意 给定一个 n 个点的无向完全图,每一条边有一定的边权. ...

  7. Jungle Roads(kruskar)

    Jungle Roads 题目链接;http://poj.org/problem?id=1251 Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  8. Dijkstra with priority queue 分类: ACM TYPE 2015-07-23 20:12 4人阅读 评论(0) 收藏

    POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra) //================================================= ...

  9. 又是图论.jpg

    BZOJ 2200 道路和航线重讲ww: FJ 正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到 T 个城镇 (1 ≤ T ≤ 25000),编号为 1 到 T.这些城镇之间通过 R 条 ...

随机推荐

  1. 在maven仓库中查找jar

    findmaven.net是一个查找Jar和查找Maven的Maven仓库搜索引擎,它能够依据Java开发人员提供的Class名或者Jar名找到包括它的Jar.同一时候提供Jar的Maven仓库链接. ...

  2. ubuntu下ssh使用 与 SCP 使用

    1 ssh远程登录服务器 ssh username@remote_ip #将username换成自己的用户名,将remote_ip换成远程服务器的ip地址 2 将文件/文件夹从远程服务器拷至本地(sc ...

  3. NPC

    这里的想说的NPC不是Non-Player-Controled,非玩家控制角色,而是Non-determinisitc Polynomial complete problem,它属于一类很特殊的问题, ...

  4. WPF界面设计技巧(11)-认知流文档 & 小议WPF的野心

    原文:WPF界面设计技巧(11)-认知流文档 & 小议WPF的野心 流文档是WPF中的一种独特的文档承载格式,它的书写和呈现方式都很像HTML,它也几乎具备了HTML的绝大多数优势,并提供了更 ...

  5. java api例子网站

    http://www.programcreek.com/java-api-examples/ http://www.apihome.cn/api/list/ http://www.docjar.com ...

  6. atitit查询表改动表字段没反应--解锁锁定的表

    atitit查询表改动表字段没反应--解锁锁定的表 查询表改动表字段没反应 要是使用gui 没反应,最好使用cmd 方式,不卉不个gui 锁上.. ALTER TABLE t_mb_awardweix ...

  7. Z.ExtensionMethods 扩展类库

    Z.ExtensionMethods 一个强大的开源扩展库 今天有意的在博客园里面搜索了一下 Z.ExtensionMethods 这个扩展类库,确发现只搜到跟这个真正相关的才两篇博文而已,我都点进去 ...

  8. 微软 Build 2016

    微软 Build 2016年开发者大会发布多项功能升级 微软Build 2016开发者大会在美国旧金山的莫斯康展览中心开幕.本次大会对一些重点功能进行了完善.如手写笔支持技术Windows Ink.语 ...

  9. Windows Phone开发(37):动画之ColorAnimation

    原文:Windows Phone开发(37):动画之ColorAnimation 上一节中我们讨论了用double值进行动画处理,我们知道动画是有很多种的,今天,我向大家继续介绍一个动画类--Colo ...

  10. .net MVC AutoFac基地的环境建设

    在Nuget在运行安装引用 Install-Package Autofac -Version 3.1.0 Install-Package Autofac.Mvc4 public static void ...