openjudge ROADS
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
- 题目大意:
- n个点,m条边,每条边有长度和费用,需要求出1到n的在费用不大于k的情况下的最短路径,如果没有输出-1
- 这道题dijsktra算法即可,并且不能记录dis数组,直接在第一次到达n时即为ans;
- 如果记录dis可能会出现花费不足以到达终点但是总长度较短,导致输出-1。
#include<cstdio>
#include<queue>
using namespace std;
struct X
{
int v,q,j,n,f;
bool operator<(const X &a)const
{
if(q==a.q) return j>a.j;
return q>a.q;
}//重载运算符
}x[];
priority_queue<X>qq;//调用系统堆
int main()
{
int k,n,m;
scanf("%d%d%d",&k,&n,&m);
for(int i=;i<=m;i++)
{
int u;
scanf("%d%d%d%d",&u,&x[i].v,&x[i].q,&x[i].j);
x[i].n=x[u].f;
x[u].f=i;
}
qq.push((X){,,,,});//载入初始节点
while(!qq.empty())
{
X t=qq.top();qq.pop();
if(t.v==n)
{
printf("%d",t.q);//到达n即为最优解
return ;
}
for(int i=x[t.v].f;i;i=x[i].n)
if(t.j+x[i].j<=k) qq.push((X){x[i].v,x[i].q+t.q,x[i].j+t.j,,});
}
printf("-1");
return ;
}
openjudge ROADS的更多相关文章
- #图# #dijkstra# ----- OpenJudge 726:ROADS
OpenJudge 726:ROADS 总时间限制: 1000ms内存限制: 65536kB 描述 N cities named with numbers 1 ... N are connected ...
- OpenJudge/Poj 1251 丛林中的路/Jungle Roads
1.链接地址: http://bailian.openjudge.cn/practice/1251/ http://poj.org/problem?id=1251 2.题目: 总时间限制: 1000m ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- Jungle Roads[HDU1301]
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- POJ1947 Rebuilding Roads[树形背包]
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11495 Accepted: 5276 ...
- 【OpenJudge 8463】Stupid cat & Doge
http://noi.openjudge.cn/ch0204/8463/ 挺恶心的一道简单分治. 一开始准备非递归. 大if判断,后来发现代码量过长,决定大打表判断后继情况,后来发现序号不对称. 最后 ...
- 【OpenJudge 191】【POJ 1189】钉子和小球
http://noi.openjudge.cn/ch0405/191/ http://poj.org/problem?id=1189 一开始忘了\(2^{50}\)没超long long差点写高精度Q ...
- 【OpenJudge 1665】完美覆盖
http://noi.openjudge.cn/ch0405/1665/?lang=zh_CN 状压水题,手动转移 #include<cstdio> #include<cstring ...
- 【OpenJudge 1793】矩形覆盖
http://noi.openjudge.cn/ch0405/1793/ 好虐的一道题啊. 看数据范围,一眼状压,然后调了好长时间QwQ 很容易想到覆盖的点数作为状态,我用状态i表示至少覆盖状态i表示 ...
随机推荐
- linux的七种文件类型
d 目录 - 普通文件 l 符号链接 s 套接字文件 b 块设备文件 二进制文件 c 字符设备文件 p 命名管道文件
- LVM逻辑卷管理
一.LVM简介 LVM(Logic Volume Manager)逻辑卷管理,简单理解就是将一块或多块硬盘的分区在逻辑上集合,当一块大硬盘来使用. 其特点是: 1.可以实现在线动态扩展,也可以缩减 2 ...
- px和em区别-在font-size的 css 的使用
px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的. em是相对长度单位.相对于当前对象内文本的字体尺寸,多理解父级设定font-size的尺寸.如当前对行内文本的字体尺寸未 ...
- python+php+redis+shell实现几台redis的同步数据
之所以使用python,是因为python多线程非常简单. 之所以使用shell,是因为写了个服务,可以方便的重启python写的那个脚本. 总体思路:利用redis的发布订阅,php作为生产者,py ...
- C++模板编程里的主版本模板类、全特化、偏特化(C++ Type Traits)
1. 主版本模板类 首先我们来看一段初学者都能看懂,应用了模板的程序: 1 #include <iostream> 2 using namespace std; 3 4 template ...
- C#:安装Windows服务,动态指定服务名及描述
Installer.cs>> public Installer() { InitializeComponent(); /* 服务未注册前,System.Configuration.Conf ...
- Django缓存使用方法
Django缓存分为Session和Cookie:Session为放在服务器端的缓存:Cookie为放在客户端(浏览器)的缓存. Session一般用来保存登录会话:Cookie一般用来保存一些个性化 ...
- threadid=1: thread exiting with uncaught.exception ......解决方法
threadid=1: thread exiting with uncaught exception (group=0x40015560)E/AndroidRuntime(285): FATAL E ...
- Intellij IDEA的激活
选server激活,填http://www.iteblog.com/idea/key.php即可 现在改为http://idea.iteblog.com/key.php
- Linux scp复制文件,不需要输入密码的技巧
当两台LINUX主机之间要互传文件时可使用SCP命令来实现,建立信任关系之后可不输入密码. 把你的本地主机用户的ssh公匙文件复制到远程主机用户的~/.ssh/authorized_keys文件中 假 ...