POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)
题目链接:
https://cn.vjudge.net/problem/POJ-1724
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
<= 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
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
- /*
- 题意描述
- 输入总钱数k,顶点数n,边数m
- 每一条边s到d长度是l,花费是t
- 可能有重边
- 问在不超过k的前提下,从1到n的最短路径是多少,如果不存在这样的最短路输出-1.
- 解题思路
- 使用用优先队列的广搜,保证每次弹出的结点是距离最短,而且花费相对小的,搜索的时候不用像矩阵中那样标记哪个结点用过了,因为使用
- 的是每个结点的邻接边,标记了可能就不能用这条边了,故直接搜索。还需注意的是一定要是弹出的结点是终点再结束,如果在搜索的时候遇
- 到直接结束,可能这条路径不是最短的,但是从优先队列里第一个弹出的一定是最短的。
- */
- #include<cstdio>
- #include<vector>
- #include<queue>
- #include<cstring>
- const int maxn = ;
- const int INF = 0x3f3f3f3f;
- using namespace std;
- struct Edge{
- int from, to, dist, pay;
- Edge(int s, int d, int l, int t) : from(s), to(d), dist(l), pay(t) { };
- };
- struct HeapNode {
- int u, d, c;
- bool operator < (const HeapNode& a) const {
- if(d == a.d){
- return c > a.c;
- }
- return d > a.d;
- }
- };
- struct BFS {
- int n,m;
- vector<Edge> edges;
- vector<int> G[maxn];
- bool done[maxn];
- void init(int n) {
- this->n = n;
- for(int i = ; i < n; i++)
- G[i].clear();
- edges.clear();
- }
- void AddEdge(int from, int to, int dist, int pay) {
- edges.push_back(Edge(from, to, dist, pay));
- m = edges.size();
- G[from].push_back(m - );
- }
- int bfs(int k, int s) {
- memset(done, , sizeof(done));
- done[s] = ;
- priority_queue<HeapNode> q;
- q.push((HeapNode){s, , });
- while(!q.empty()) {
- HeapNode x = q.top();
- q.pop();
- int u = x.u;
- if(u == n - )
- return x.d;
- for(int i = ; i < G[u].size(); i++) {
- Edge e = edges[G[u][i]];
- if(k >= e.pay + x.c)
- q.push((HeapNode){e.to, x.d+e.dist, e.pay + x.c});
- }
- }
- return -;
- }
- };
- struct BFS solve;
- int main()
- {
- int k, n, m, s, d, l, t;
- while(scanf("%d", &k) != EOF) {
- scanf("%d%d", &n, &m);
- solve.init(n);
- for(int i = ; i <= m; i++) {
- scanf("%d%d%d%d",&s, &d, &l, &t);
- s--;d--;
- solve.AddEdge(s, d, l, t);//先--再使用
- }
- printf("%d\n", solve.bfs(k,));
- }
- return ;
- }
POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)的更多相关文章
- 深搜+剪枝 POJ 1724 ROADS
POJ 1724 ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12766 Accepted: 4722 D ...
- 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 1511 - Invitation Cards 邻接表 Dijkstra堆优化
昨天的题太水了,堆优化跑的不爽,今天换了一个题,1000000个点,1000000条边= = 试一试邻接表 写的过程中遇到了一些问题,由于习惯于把数据结构封装在 struct 里,结果 int [10 ...
- POJ 1724 Roads
题意:有R条路,每条路都有一定的路长和花费,问在总的花费小于一定的值的情况下,从1到N的最短路程 注意:这里两点之间单向边,且可能存在很多条路,所以只能用邻接表存储.思路:用dijks ...
- 用邻接表实现DFS和BFS
#include <stdio.h> #include <stdlib.h> #define MAXVERTEX 10 typedef char VertexType; //顶 ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- poj 1724 ROADS 很水的dfs
题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...
- POJ 1724 ROADS(bfs最短路)
n个点m条边的有向图,每条边有距离跟花费两个参数,求1->n花费在K以内的最短路. 直接优先队列bfs暴力搞就行了,100*10000个状态而已.节点扩充的时候,dp[i][j]表示到达第i点花 ...
随机推荐
- Json中对日期的处理
前言:Json对日期的处理很特别,我们不能简单的转换而得到我们想要的结果,需要进行特殊处理 一.JSon序列化和反序列化对日期的处理 JsonHelper类: using System.IO; usi ...
- iPhone屏幕分辨率和适配规则 图片文字适配
基本概念 - 逻辑分辨率 pt (point),物理分辨率 px (pixel) - 缩放因子 scale 或者 dpr, scale ≈ px / pt - 缩放采样 例如 iPhone 6 Plu ...
- sock5协议转换http协议工具polipo使用笔记(Centos7)
一.安装 Shadowsocks使用socks5协议,而终端很多工具目前只支持http和https等协议,所以我们为终端设置Shadowsocks的思路就是将socks5协议转换成http协议,然后为 ...
- Beta冲刺 (1/7)
Part.1 开篇 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Part.2 成员汇报 组员1(组长)柯奇豪 过去两天完成了哪些任务 熟悉并编写小程序的自定义控件 展示G ...
- Springboot高版本中@ConfigurationProperties注解取消location属性
在spring boot 1.5 版本之前 在@ConfigurationProperties注释中有两个属性:locations:指定配置文件的所在位置prefix:指定配置文件中键名称的前缀 sp ...
- 2nd week
<!DOCTYPE html> <html> <head> <title>用户登录.html</title> <meta http-e ...
- 冲刺博客NO.8
今天做了什么: 多天学习后,实现了短信验证的功能,可以选择国家,可以在Mob的后台管理短信验证 遇到的困难: 注册回调事件,afterEvent的判定(事件完成后调用)
- 使用TheFolderSpy监控文件夹的变化-邮件通知
一.概述 当我们的文档或者代码文件发布在公网.共享文件夹中,其他用户具备访问或修改的权限时,就存在文档被覆盖或删除的分享.另外一个典型的场景,发布在Web服务器上的网页文件,在网站版本不更新的时间,服 ...
- es2017新特性
2017年6月底es2017不期而至; 截止目前es8是ecmascript规范的第九个版本:自es2015开始ECMA协会将每年发布一个版本并将年号作为版本号:算了 直接看下es2017的新特性: ...
- javascript-浏览器消息提醒
如何让用户在浏览器最小化的状态下也能及时的收到消息提醒呢? 这个问题作为webRd是要正面面对的. 大约可分两种场景:一种是类似桌面通知的形式还有一种是类似QQ提醒(在系统任务栏闪烁随后高亮);接下来 ...