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. 如何利用sql注入进行爆库

    SQL注入能做什么 在<SQL注入基础>一文介绍了SQL注入的基本原理和实验方法,那接下来就要问一下,SQL注入到底能什么? 估计很多朋友会这样认为:利用SQL注入最多只能获取当前表中的所 ...

  2. MyBatis 之一 简介

    什么是 MyBatis ? MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...

  3. QT 杂记

    1.按F4切换designer和Edit视图. 2.加载同目录下的js文件: import "XXX.js" as MyJs //首字母一定要大写 3.qml 引用的js中对象.字 ...

  4. swift 扩展 要素总结

    类: 协议: 泛型及元素类型:扩展约束:

  5. Container Views

    https://developer.apple.com/documentation/uikit/views_and_controls Container Views Organize and pres ...

  6. java_IO_2

    1.字节流  InputStream(抽象类) package ioStudy; import java.io.File; import java.io.FileInputStream; import ...

  7. qemu vm setup network(ssh) with buildroot

    1, build buildroot with buildroot.config, that is 'make qemu_x86_64_defconfig' + some packages, sshd ...

  8. 洛谷——P1156 垃圾陷阱

    P1156 垃圾陷阱 题目描述 卡门――农夫约翰极其珍视的一条Holsteins奶牛――已经落了到“垃圾井”中.“垃圾井”是农夫们扔垃圾的地方,它的深度为D(2 \le D \le 100)D(2≤D ...

  9. uWSGI+nginx+django+virtualenv+supervisor部署项目

    一.前言 在部署项目前,你已有一个能够在你本机测试过,能正常启动的Django项目(毕竟本文主要讲解部署Django项目),以及掌握了Linux系统的一些基本命令. 相关链接: Centos7安装py ...

  10. linux tail-在屏幕上显示指定文件的末尾若干行

    博主推荐:获取更多 linux文件内容查看命令 收藏:linux命令大全 tail命令用于输入文件中的尾部内容.tail命令默认在屏幕上显示指定文件的末尾10行.如果给定的文件不止一个,则在显示的每个 ...