POJ 1724 Roads
题意:有R条路,每条路都有一定的路长和花费,问在总的花费小于一定的值的情况下,从1到N的最短路程
注意:这里两点之间单向边,且可能存在很多条路,所以只能用邻接表存储。
思路:用dijkstra,但是每次判断一个元素是否能进入队列,并不是源点到它的距离被更新了才入队,
而是只要满足从源点到该点总的路费小于给定的值,都可入队。
每次从优先级队列中取出路程最小(如果路程相同,取花费最小)的点。
当N点被取出来时,直接结束
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
using namespace std;
const int maxn=0x3f3f3f3f; int money,n,r,a,b,l,toll,t;
int ans,tot; struct Road{
int next,from,to; }road[]; int head[];
int rlength[]; //每条路的路长
int cost[]; //每条路的花费 struct State{
//u到1点的总的花费(cost)、路长(dis)
int u;
int cost,dis;
void init(int uu,int costt,int diss){
u=uu;
cost=costt;
dis=diss;
} bool operator < (const State tmp) const
{
if(dis==tmp.dis)
return cost>tmp.cost;
else
return dis>tmp.dis;
//如果dis<tmp.dis,则为从小到大排,但这样的话优先级队列取得是最大的,它默认的就是最大堆,取排在最后的
}
}; void add(int a,int b,int ll,int tt){
road[tot].next=head[a];
road[tot].from=a;
road[tot].to=b;
rlength[tot]=ll;
cost[tot]=tt;
head[a]=tot++;
} void dijastra(){
int idx;
State temp,minNode,node;
priority_queue<State> q;
while(!q.empty()) q.pop();
//dis[1]=0;
//vis[1]=1;
temp.init(,,);
q.push(temp);
while(!q.empty()){
minNode=q.top();
q.pop();
idx=minNode.u;
if(idx==n){
ans=minNode.dis;
break;
}
for(int k=head[idx];k!=-;k=road[k].next){
int v=road[k].to;
if(minNode.cost+cost[k]<=money){
node.init(v,minNode.cost+cost[k],minNode.dis+rlength[k]);
q.push(node);
}
}
}
} int main()
{
scanf("%d",&t);
for(int i=;i<=t;i++){
ans=maxn;
tot=;
memset(head,-,sizeof(head)); scanf("%d",&money);
scanf("%d",&n);
scanf("%d",&r); for(int i=;i<=r;i++){
scanf("%d%d%d%d",&a,&b,&l,&toll);
add(a,b,l,toll);
}
dijastra();
if(ans==maxn)
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
}
POJ 1724 Roads的更多相关文章
- 深搜+剪枝 POJ 1724 ROADS
POJ 1724 ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12766 Accepted: 4722 D ...
- poj 1724 ROADS 很水的dfs
题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...
- poj 1724 ROADS 解题报告
题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每 ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)
题目链接: https://cn.vjudge.net/problem/POJ-1724 N cities named with numbers 1 ... N are connected with ...
- POJ 1724 ROADS【最短路/搜索/DP】
一道写法多样的题,很具有启发性. 具体参考:http://www.cnblogs.com/scau20110726/archive/2013/04/28/3050178.html http://blo ...
- DFS(剪枝) POJ 1724 ROADS
题目传送门 题意:问从1到n的最短路径,同时满足花费总值小于等于k 分析:深搜+剪枝,如果之前走过该点或者此时的路劲长度大于最小值就不进行搜索. /************************** ...
- POJ 1724 ROADS(二维SPFA)
题目链接 用STL实现超时了,用普通队列500+,看到spfa,反应太迟钝了. #include <cstring> #include <cstdio> #include &l ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
随机推荐
- jquery获取html元素的绝对位置和相对位置
jquery获取html元素的绝对位置坐标和相对父元素的位置坐标方法:绝对位置坐标:$("#elem").offset().top$("#elem").offs ...
- <a href="onclick="javascript:goSearch(this)" class="click" name="Java">Java</a>为什么a标签的父节点获取不到
<script> function goSearch(event) { //var select = $('#keyInput').val($(event).attr("name ...
- 一款仿PBA官网首页jQuery焦点图的切换特效
一款仿PBA官网首页jQuery焦点图的切换特效,非常的简单大方, 在对浏览器兼容性的方面做了不少的功夫.IE6也勉强能过去. 还是一款全屏的焦点图切换特效.大气而清新.很适合简介大方的网站. 下图还 ...
- php中echo、print、print_r、printf的返回值
1.echo 无返回值,是一个语言结构.在输出多个参数时不可以使用小括号; 2.print返回值为1:如:$x = 0; echo print $x."<br/>";/ ...
- 用MySQL log调试程序
打开my.ini文件 在[mysqld]的下面加上log = c:/mysql_query.log.txt重启mysql 以后你用可以用editplus查看你运行的sql了,不用在程序里一句句的用lo ...
- ref 和out的用法以及区别
在项目其实很少用ref和out,但是我们常用的工具resharep在帮我们重构的时候难免会给我们重构成带有ref或者是out的方法. 本人也是用的少所以难免忘记,留下简略笔记,以供后来自我参考: 为何 ...
- openerp经典收藏 深入理解对象(转载)
深入理解对象(转载) 原文地址:http://shine-it.net/index.php/topic,2159.0.htmlhttp://blog.sina.com.cn/s/blog_57ded9 ...
- Python学习笔记1——人人都爱列表
一些BIF函数在列表中的应用: Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64) ...
- linux 压缩文件 及压缩选项详解
本文介绍linux下的压缩程序tar.gzip.gunzip.bzip2.bunzip2.compress.uncompress. zip. unzip.rar.unrar等程式,以及如何使用它们对. ...
- Linux流量监控工具 - iftop (最全面的iftop教程)
在类Unix系统中可以使用top查看系统资源.进程.内存占用等信息.查看网络状态可以使用netstat.nmap等工具.若要查看实时的网络流量,监控TCP/IP连接等,则可以使用iftop. 一.if ...