ROADS

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 12436 Accepted: 4591

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

Source

CEOI 1998

题目链接

id=1724">http://poj.org/problem?

id=1724

题意:你有K个点数,有N个点,M条边,一个maxcost。边为有向边(len。cost),求不超maxcost一条最短路

思路:spfa+一个if限制好像TLE

用dijkstra+优先队列优化

代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
int n,m;
struct node{
int y,di,fee;
node(int yy,int dii,int fe)
{
y=yy,di=dii,fee=fe;
}node(){}
bool operator < (const struct node a)const
{
if(a.di==di) return a.fee<fee;
return a.di<di;
}
}now,nex;
int maxx;
int dis[110];
vector<node> lin[110];
priority_queue<node> q;
int dj()
{
q.push(node(1,0,0));
while(!q.empty())
{
now=q.top(); q.pop();
if(now.y==n) return now.di;
for(int i=0;i<lin[now.y].size();i++)
{ if(now.fee+lin[now.y][i].fee<=maxx)
q.push(node(lin[now.y][i].y,now.di+lin[now.y][i].di,now.fee+lin[now.y][i].fee));
}
}
return -1;
} int main()
{
scanf("%d%d%d",&maxx,&n,&m);
for(int i=1;i<=m;i++)
{
int aa,bb,cc,dd;
scanf("%d%d%d%d",&aa,&bb,&cc,&dd);
lin[aa].push_back(node(bb,cc,dd));
}
printf("%d\n",dj()); }

【poj 1724】 ROADS 最短路(dijkstra+优先队列)的更多相关文章

  1. poj 1724 ROADS 最短路

    题目链接 n个节点, m条边, 一开始有K这么多的钱, 每条边有len, cost两个属性, 求1到n的最短距离, 花费要小于k. dis数组开成二维的, dis[u][cost]表示到达u花费为co ...

  2. 深搜+剪枝 POJ 1724 ROADS

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

  3. ROADS POJ - 1724 约束最短路 暴搜 加剪枝

    http://poj.org/problem?id=1724 题意:最短路的模板,不过每条边加上一个费用,要求总费用不超过k 题解:不能用dijkstra ,直接暴力,dfs维护len和cost. 普 ...

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

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

  5. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

  6. POJ 1724 ROADS(bfs最短路)

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

  7. POJ - 2387 Til the Cows Come Home (最短路Dijkstra+优先队列)

    Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before ...

  8. POJ 1724 ROADS【最短路/搜索/DP】

    一道写法多样的题,很具有启发性. 具体参考:http://www.cnblogs.com/scau20110726/archive/2013/04/28/3050178.html http://blo ...

  9. Codeforces Gym101502 I.Move Between Numbers-最短路(Dijkstra优先队列版和数组版)

    I. Move Between Numbers   time limit per test 2.0 s memory limit per test 256 MB input standard inpu ...

  10. HDU 1874-畅通project续(最短路Dijkstra+优先队列)

    畅通project续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. Android基础TOP7_1:ListView制作列表

    结构: Activity: activity_main: <RelativeLayout xmlns:android="http://schemas.android.com/apk/r ...

  2. python计算auc指标

    1.安装scikit-learn 1.1Scikit-learn 依赖 Python (>= 2.7 or >= 3.3), NumPy (>= 1.8.2), SciPy (> ...

  3. SQLState: 23000

    今天登陆项目的时候,报500,日志显示如下: 解决办法是: 首先,删除序列:DROP SEQUENCE sys_log_seq 然后,新建序列:CREATE SEQUENCE sys_log_seq ...

  4. android studio使用中碰到Failure [INSTALL_FAILED_OLDER_SDK] 问题

    第一次使用Android studio开发.直接新建一个默认项目运行出现:Failure [INSTALL_FAILED_OLDER_SDK] , 网上很多人说修改build.gradle中的mins ...

  5. RocketMQ学习笔记(16)----RocketMQ搭建双主双从(异步复制)集群

    1. 修改RocketMQ默认启动端口 由于只有两台机器,部署双主双从需要四个节点,所以只能修改rocketmq的默认启动端口,从官网下载rocketmq的source文件,解压后使用idea打开,全 ...

  6. DWG转PDF

    DWG转PDF DWG转换PDF有两种方法,一种是利用PDF打印机,一种是利用专业软件: 利用PDF打印机最直接,但是不能批量打印,下面讲一下利用专业软件如何进行批量转换,在这里以梦想CAD软件(Mx ...

  7. vue基础---计算属性和侦听器

    [一]计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div id="example"> ...

  8. oracle 备份/恢复

    oracle备份是为了有问题能够快速恢复:

  9. Vue.js 模板语法

    本章节将详细介绍 Vue.js 模板语法,如果对 HTML +Css +JavaScript 有一定的了解,学习起来将信手拈来. Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 ...

  10. css--小白入门篇2

    一.css基础选择器 html负责结构,css负责样式,js负责行为. css写在head标签里面,容器style标签. 先写选择器,然后写大括号,大括号里面是样式. 1 <style type ...