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

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
解决方式:此题我是这样做的,用上优先队列,在费用可行的情况下,不断松弛路径。事实上也相当于bfs+优先队列。首先路径最短的优先级最高,其次是花费,通过不断的把符合费用要求能到达的点增加优先队列,每次出队即更新能到达的点。最后假设出队的点是N,算法结束,得到的路径既是在花费符合的情况下最短的,这题考察的是能不能深刻理解dijkstra的原理,并运用。
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define MMAX 10003
#define Max 103
using namespace std;
int K,N,R,k;
int head[Max];
struct edge
{ int from,to,len,cost;
int next;
} E[MMAX];
struct stay
{ int dis,cost,x;
bool operator<(const stay &s)const
{
if(dis!=s.dis)
{
return dis>s.dis;
}
else return cost>s.cost; } };
void add(int from,int to,int len,int cost)
{
E[k].from=from;
E[k].to=to;
E[k].len=len;
E[k].cost=cost;
E[k].next=head[from];
head[from]=k++; }
int dijkstra()
{
priority_queue<stay> Q;
stay in;
in.dis=0,in.cost=0,in.x=1;
Q.push(in);
while(!Q.empty())
{
stay out=Q.top(); if(out.x==N) {return out.dis;}
Q.pop();
for(int v=head[out.x]; v!=-1; v=E[v].next)
{
if(out.cost+E[v].cost<=K)
{
stay temp;
temp.x=E[v].to;
temp.dis=out.dis+E[v].len;
temp.cost=out.cost+E[v].cost;
Q.push(temp);
}
}
} }
int main()
{
while(~scanf("%d%d%d",&K,&N,&R))
{
memset(head,-1,sizeof(head));
k=0;
int from,to,len,cost;
for(int i=0; i<R; i++)
{
scanf("%d%d%d%d",&from,&to,&len,&cost);
add(from,to,len,cost);
}
printf("%d\n",dijkstra()); }
return 0;
}

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. atitit查询表改动表字段没反应--解锁锁定的表

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

  2. in与exist , not in与not exist 的区别(转)

    in和exists  in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询.一直以来认为exists比in效率高的说法是不准确的.  如果查询的 ...

  3. iframe属性參数

    iframe属性參数 当点击一个子页面的链接时, 怎样将还有一个子页面嵌入到当前iframe中 仅仅要给这个iframe命名就能够了. <iframe width=420 height=330 ...

  4. 比較Swift与HDFS话Ceph本质(by quqi99)

    作者:张华  发表于:2014-06-21版权声明:能够随意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99 ) 作者将又 ...

  5. cocos2d-x V3.0 呼叫加速度计 Acceleration

    今天克服了一个问题,我觉得非常酷 哈哈. 今天得到解决cocos2d-x 3.0 呼叫重力加速器问题,上网查了很多资料 发现是不够,不解决这个问题,我不知道如果我使用3.0 这一问题的版本号,但是,这 ...

  6. EXCEL随机密码生成函数

    =CHAR(INT(RAND()*+))&INT(RAND()*+)&CHAR(INT(RAND()*+))&INT(RAND()*+)&CHAR(INT(RAND() ...

  7. MVC模型与FishiGUI应用层MVC型号

    MVC概要: MVC (Modal View Controler)M是指数据模型,V是指用户界面,C则是控制器. 使用MVC的目的是将M和V的实现代码分离,从而使同一个程序能够使用不同的表现形式.比方 ...

  8. 重新想象 Windows 8 Store Apps (30) - 信息: 获取包信息, 系统信息, 硬件信息, PnP信息, 常用设备信息

    原文:重新想象 Windows 8 Store Apps (30) - 信息: 获取包信息, 系统信息, 硬件信息, PnP信息, 常用设备信息 [源码下载] 重新想象 Windows 8 Store ...

  9. JSP+Ajax站点开发小知识

    一.JSP基础 1.<select  name="love"  size="3">当中的size属性指定了列表框显示选项的条数.假设全部选项多于这个 ...

  10. 采用Eclipse中间Maven构建Web项目错误(一)

    采用Eclipse中间Maven构建Web工程 1.在进行上述操作时.pom.xml一直报错 <project xmlns="http://maven.apache.org/POM/4 ...