【poj 1724】 ROADS 最短路(dijkstra+优先队列)
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+优先队列)的更多相关文章
- poj 1724 ROADS 最短路
题目链接 n个节点, m条边, 一开始有K这么多的钱, 每条边有len, cost两个属性, 求1到n的最短距离, 花费要小于k. dis数组开成二维的, dis[u][cost]表示到达u花费为co ...
- 深搜+剪枝 POJ 1724 ROADS
POJ 1724 ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12766 Accepted: 4722 D ...
- ROADS POJ - 1724 约束最短路 暴搜 加剪枝
http://poj.org/problem?id=1724 题意:最短路的模板,不过每条边加上一个费用,要求总费用不超过k 题解:不能用dijkstra ,直接暴力,dfs维护len和cost. 普 ...
- POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)
题目链接: https://cn.vjudge.net/problem/POJ-1724 N cities named with numbers 1 ... N are connected with ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- POJ 1724 ROADS(bfs最短路)
n个点m条边的有向图,每条边有距离跟花费两个参数,求1->n花费在K以内的最短路. 直接优先队列bfs暴力搞就行了,100*10000个状态而已.节点扩充的时候,dp[i][j]表示到达第i点花 ...
- 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 ...
- POJ 1724 ROADS【最短路/搜索/DP】
一道写法多样的题,很具有启发性. 具体参考:http://www.cnblogs.com/scau20110726/archive/2013/04/28/3050178.html http://blo ...
- 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 ...
- HDU 1874-畅通project续(最短路Dijkstra+优先队列)
畅通project续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- 记一些关于acm的小知识(自用,粗糙,勿点呀)
#define INF 0x7ffffff,定义一个很大的数
- cout 按进制数出
头文件: #include<iostream> #include<iomanip> //setbase() #include<bitset> //bitset< ...
- 洛谷——P3919 【模板】可持久化数组(可持久化线段树/平衡树)
P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目背景 UPDATE : 最后一个点时间空间已经放大 标题即题意 有了可持久化数组,便可以实现很多衍生的可持久化功能(例如:可持久化并查集 ...
- 洛谷——P1475 控制公司 Controlling Companies
P1475 控制公司 Controlling Companies 题目描述 有些公司是其他公司的部分拥有者,因为他们获得了其他公司发行的股票的一部分.(此处略去一句废话)据说,如果至少满足了以下三个条 ...
- (C/C++学习)15.C语言字符串和字符数组
说明:在C语言中字符串和字符数组有很多相似之处,却又有着一些不同.本文将针对其区别与联系,进行分析总结. 一.字符串 1.在C语言中,字符串是由双引号括起来的任意字符序列,如:"china& ...
- springboot的jsp热部署
使用springboot每次修改jsp都需要重新启动是不是很麻烦呢?以下是解决办法! yml格式 server: servlet: jsp: init-parameters: development: ...
- Session共享实现方案调研
1.背景 随 着互联网的日益壮大,网站的pv和uv成线性或者指数倍的增加.单服务器单数据库早已经不能满足实际需求.目前大多数大型网站的服务器都采用了分布式服务 集群的部署方式,所谓集群,就是让一组计算 ...
- lua排序算法
SEED = ; --随机序列 可任取 NUM = ; --排序规模 --随机序列 初始数据 function GenRnd( seed, n ) --生成随机数 data = {}; local r ...
- 通过混合编程分析的方法和机器学习预测Web应用程序的漏洞
通过混合编程分析的方法和机器学习预测Web应用程序的漏洞 由于时间和资源的限制,web软件工程师需要支持识别出有漏洞的代码.一个实用的方法用来预测漏洞代码可以提高他们安全审计的工作效率.在这篇文章中, ...
- MySql join匹配原理
疑问 表:sl_sales_bill_head 订单抬头表 数据行:8474 表:sl_sales_bill 订单明细 数据行:8839 字段:SALES_BILL_NO 订单号 情 ...