题意:有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的更多相关文章

  1. 深搜+剪枝 POJ 1724 ROADS

    POJ 1724 ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12766   Accepted: 4722 D ...

  2. poj 1724 ROADS 很水的dfs

    题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...

  3. poj 1724 ROADS 解题报告

    题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每 ...

  4. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  5. POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)

    题目链接: https://cn.vjudge.net/problem/POJ-1724 N cities named with numbers 1 ... N are connected with ...

  6. POJ 1724 ROADS【最短路/搜索/DP】

    一道写法多样的题,很具有启发性. 具体参考:http://www.cnblogs.com/scau20110726/archive/2013/04/28/3050178.html http://blo ...

  7. DFS(剪枝) POJ 1724 ROADS

    题目传送门 题意:问从1到n的最短路径,同时满足花费总值小于等于k 分析:深搜+剪枝,如果之前走过该点或者此时的路劲长度大于最小值就不进行搜索. /************************** ...

  8. POJ 1724 ROADS(二维SPFA)

    题目链接 用STL实现超时了,用普通队列500+,看到spfa,反应太迟钝了. #include <cstring> #include <cstdio> #include &l ...

  9. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

随机推荐

  1. Microsoft.Xna.Framework.TitleContainer.OpenStream()

    /// <summary> /// This method opens a file using System.IO classes and the /// TitleLocation p ...

  2. 两个Activity之间的交互startActivityForResult的使用

    代码如下: package com.zzw.teststartintentforrequest; import android.app.Activity; import android.content ...

  3. 【面试虐菜】—— JAVA面试题(3)

    1 throws与throw的区别 解析:throws和throw是异常处理时两个常见的关键字,初级程序员常常容易正确理解throw和throws的作用和区别,说明已经能比较深入理解异常处理.Thro ...

  4. Ubuntu下编程环境GNU安装

    ubuntu下C编程   环境搭建 其实,linux下写C也是很容易的.IDE的话用 eclipse 集成 CDT 模块就行了.当然这属于重量级的了,就如同VC++之于windows一样.那有没有像T ...

  5. ARP

    视频教程 http://baidu.ku6.com/watch/08644463979695746698.html?page=videoMultiNeed arp代理  跨越路由 免费arp  检查i ...

  6. NGUI3.5系列教程之 一些小功能的实现

    (一)可拖动窗体的实现: 1:添加一个Sprite为鼠标点击区域,改名为:DragSprite 2:给DragSprite添加Collider 3:给DragSprite添加Drag Object , ...

  7. 关于qt5在win7下发布 & 打包

    QT5 发布时,莫过于依赖动态链接库(dll) , 但是,QT5的动态链接库貌似都有2套 ,例如 Qt5Core (针对realese) , Qt5Cored (针对debug) ,凡事末尾带d的都是 ...

  8. verilog逻辑复制

    本文转自:http://www.cnblogs.com/linjie-swust/archive/2012/03/27/FPGA_verilog.html 在FPGA设计中经常使用到逻辑复制,逻辑复制 ...

  9. SQL对like 操作中的特殊字符处理方法

    SQL对like 操作中的特殊字符处理方法:    SQL Server查询过程中,单引号 ' 是特殊字符,所以在查询的时候要转换成双单引号 '' .    在like操作还有以下特殊字符:下划线_, ...

  10. LintCode-Median II

    Numbers keep coming, return the median of numbers at every time a new number added. Example For numb ...