#图# #dijkstra# ----- OpenJudge 726:ROADS
OpenJudge 726:ROADS
总时间限制: 1000ms内存限制: 65536kB
- 描述
- 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.
- 输入
- 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.
- 输出
- 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. - 样例输入
-
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 - 样例输出
-
11 求单源最短路。优先队列排序,路长为第一关键字,消耗费用为第二关键字。
#include<cstdio>
#include<queue>
using namespace std;
int k,n,R;
int head[];
struct node{
int v;
int l,w;
int d;
int next;
bool operator < (const node & a) const{//重载运算符,多关键字排序
if(d==a.d)return w>a.w;
else return d>a.d;
}
}edge[]; priority_queue<node>Q; int dijkstra(){
node now1;
now1.v=;
now1.w=;
now1.d=;
Q.push(now1);
while(!Q.empty()){
node now=Q.top();
if(now.v==n)return now.d;
Q.pop();
for(int i=head[now.v];i;i=edge[i].next)
if(k>=now.w+edge[i].w){
node now2;
now2.v=edge[i].v;
now2.w=now.w+edge[i].w;
now2.d=now.d+edge[i].l;
Q.push(now2);
}
}
} int main(){
scanf("%d%d%d",&k,&n,&R);
for(int i=;i<=R;++i){
int x;
scanf("%d%d%d%d",&x,&edge[i].v,&edge[i].l,&edge[i].w);
edge[i].next=head[x];
head[x]=i;
}
printf("%d\n",dijkstra());
return ;
}
#图# #dijkstra# ----- OpenJudge 726:ROADS的更多相关文章
- 【LibreOJ】#6354. 「CodePlus 2018 4 月赛」最短路 异或优化建图+Dijkstra
[题目]#6354. 「CodePlus 2018 4 月赛」最短路 [题意]给定n个点,m条带权有向边,任意两个点i和j还可以花费(i xor j)*C到达(C是给定的常数),求A到B的最短距离.\ ...
- [USACO09FEB] Revamping Trails 【分层图+Dijkstra】
任意门:https://www.luogu.org/problemnew/show/P2939 Revamping Trails 题目描述 Farmer John dutifully checks o ...
- BZOJ3073: [Pa2011]Journeys(线段树优化建图 Dijkstra)
题意 \(n\)个点的无向图,构造\(m\)次边,求\(p\)到任意点的最短路. 每次给出\(a, b, c, d\) 对于任意\((x_{a \leqslant x \leqslant b}, y_ ...
- [NOI2019]弹跳(KD-Tree/四分树/线段树套平衡树 优化建图+Dijkstra)
本题可以用的方法很多,除去以下三种我所知道的就还有至少三种. 方法一:类似线段树优化建图,将一个平面等分成四份(若只有一行或一列则等分成两份),然后跑Dijkstra即可.建树是$O(n\log n) ...
- 【BZOJ-3627】路径规划 分层图 + Dijkstra + spfa
3627: [JLOI2014]路径规划 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 186 Solved: 70[Submit][Status] ...
- bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级——分层图+dijkstra
Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M<=50,000)条双向泥土道路,编号为1..M. 道路i连接牛棚P1_i和P2_i ...
- POJ 2374 线段树建图+Dijkstra
题意: 思路: 线段树+Dijkstra(要堆优化的) 线段树要支持打标记 一个栅栏 拆成两个点 :左和右 新加一个栅栏的时候 看看左端点有没有被覆盖过 如果有的话 就分别从覆盖的那条线段的左右向当前 ...
- 倒水问题UVA 10603——隐式图&&Dijkstra
题目 给你三个容量分别为 $a,b,c$ 的杯子,最初只有第3个杯子装满了水,其他两个杯子为空.最少需要到多少水才能让一个某个杯子中的水有 $d$ 升呢?如果无法做到恰好 $d$ 升,就让某个杯子里的 ...
- HDU-3499Flight (分层图dijkstra)
一开始想的并查集(我一定是脑子坏掉了),晚上听学姐讲题才知道就是dijkstra两层: 题意:有一次机会能使一条边的权值变为原来的一半,询问从s到e的最短路. 将dis数组开成二维,第一维表示从源点到 ...
随机推荐
- void (*isr_handle_array[50])(void);求解这个申明怎么理解 啊??
这是函数指针数组.一层一层向里面剥就好啦. 是一个指向 返回值为void 参数也是void的指针数组.先看里面[50]知道是个数组,再向外看是一个函数指针,合起来就是函数指针数组.我写个源码,你就明白 ...
- 转Y-slow23原则(雅虎)
YslowYahoo发布的一款基于FireFox的插件,主要是为了提高网页性能而设计的,下面是它提倡了23条规则,还是很不错的,分享一下: 1.减少HTTP请求次数 合并图片.CSS.JS,改进首次访 ...
- nginx的内页跳转总结
刚进公司的时候老大一直在要求php做内页跳转,当时也不太了解细节所以一直没有说话.后来php问我你会不会做内页跳转,我说会一点就做了几个,从此搞内页跳转搞了两个星期.至于为什么做内页跳转哪就暂时不 ...
- SQL复习三(子查询)
子查询 子查询就是嵌套查询,即select中包含这select,如果一条语句中存在着两个,或者两个以上的select,那么就是子查询语句了. 子查询出现的位置 where后,作为条件的一部分: fro ...
- Tokumx 代替 Mongodb 群集部署
一, 配置环境: 系统: CentOS 7 x64 tokumx1 ip: 192.168.0.155 tokumx2 ip: 192.168.0.156 tokumx3 ip: 192.168.0. ...
- CentOS 5.8 x64 安装TomCat
简单记录一下...虽然安装很简单... 首先下载配置安装 jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u25-do ...
- [DNS]ACL、also-notify、视图服务器及安全设置
1. ACL :访问控制列表放在named.conf的头部,如果acl的内容太多,可以另创建一个文件,将acl放在该文件中,再在主配置文件named.conf用include 将文件加载进来(记得放在 ...
- Ubuntu下搭建C++开发环境
Ubuntu使用eclipse搭建c/c++编译环境----CDT插件 Ubuntu(Linux)使用Eclipse搭建C/C++编译环境 这两天,给自己电脑弄了双系统,除了原来的W ...
- js去除字符串空格
str.replace(/\s+/g,""); str.replace(/\s|\xA0/g,""); empName=empName.replace(/^\s ...
- LPC1768定时器普通定时
//其他通道的基本定时功能都能在这里实现 void Time0Mr0Init(u32 arr,u32 psc) { LPC_SC->PCONP |= (1<<1); ...