poj_1724 ROADS(有费用限制的最短路)
来源:互联网 发布:淘宝违法判刑大全 编辑:程序博客网 时间:2024/05/12 17:12
ROADS
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14250 Accepted: 5184
Description
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 Nthat he can afford with the amount of money he has.
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 Nthat he can afford with the amount of money he has.
Input
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 :
Notice that different roads may have the same source and destination cities.
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
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.
If such path does not exist, only number -1 should be written to the output.
Sample Input
5671 2 2 32 4 3 33 4 2 41 3 4 14 6 2 13 5 2 05 4 3 2
Sample Output
11
BFS加优先队列,优先队列保存某个到顶点u的方案,包括距离与费用,以距离作优先级。
从顶点1开始BFS,只要费用不超过k,就将当前方案加入优先队列。
当从优先队列出队的是顶点n的方案,该方案就是答案,优先队列的性质可以保证它是最短的。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 10010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct Edge{ int from, to, dist, cost; Edge(int u, int v, int d, int c) : from(u), to(v), dist(d), cost(c) {}};struct HeapNode{ int d, u, c; HeapNode(int dd, int uu, int cc) : d(dd), u(uu), c(cc) {} bool operator < (const HeapNode& rhs) const { return d > rhs.d; }};struct Dijkstra{ int n, m, k; vector<Edge> edges; vector<int> G[maxn]; void init(int n, int k) { this->n = n; this->k = k; for(int i = 1; i <= n; i++) G[i].clear(); edges.clear(); } void AddEdge(int from, int to, int dist, int cost) { edges.push_back(Edge(from, to, dist, cost)); m = edges.size(); G[from].push_back(m-1); } int dijkstra(int s) { priority_queue<HeapNode> Q; Q.push(HeapNode(0, s, 0)); while(!Q.empty()) { HeapNode x = Q.top(); Q.pop(); int u = x.u; if(u == n) return x.d; for(int i = 0; i < G[u].size(); i++) { Edge& e = edges[G[u][i]]; if(x.c + e.cost <= k) Q.push(HeapNode(x.d+e.dist, e.to, x.c+e.cost)); } } return -1; }};Dijkstra dij;int k, n, m;int main(){ while(~scanf("%d%d%d", &k, &n, &m)) { dij.init(n, k); int u, v, d, c; for(int i = 0; i < m; i++) { scanf("%d%d%d%d", &u, &v, &d, &c); dij.AddEdge(u, v, d, c); } printf("%d\n", dij.dijkstra(1)); } return 0;}
0 0
- poj_1724 ROADS(有费用限制的最短路)
- 【poj】【费用限制最短路】ROADs
- POJ 1724 - ROADS 有限制的最短路,A*
- POJ 1724 ROADS (有限制的最短路)
- POJ1724 ROADS(加限制条件的最短路,深搜)
- POJ1062(有限制的最短路)
- [POJ 1724] ROADS (有约束条件的最短路)
- POJ 1724ROADS(费用最短路:优先队列+BFS)
- poj 1724 spfa(有费用上限的最短路(双重标准最短路))
- poj 1724 ROADS 加限制的最短路
- Roads带限制条件的最短路算法
- hdu 4179(有限制的最短路)
- HDU3768限制最短路(有必经过的点)
- hdu4396 spfa有限制的最短路
- poj 1062(有限制的最短路)
- poj 1724(有限制的最短路)
- hdu4885 有 限制的最短路
- 限制费用的最短路 例题:poj 1724
- 从零开始学_JavaScript_系列(33)——dom.append()与$.append()
- bootstrap——组件(八、进度条)
- MySQL 2002问题解决方案 - Can't connect to local MySQL server through socket...
- 性能调优攻略-来自酷壳陈皓
- Session ID/session token 及和cookie区别
- poj_1724 ROADS(有费用限制的最短路)
- Linux 系统启动过程
- C++(3)_异质链表
- mysql-binlog常用操作
- 多线程环境下单例模式
- Volatile从入门到放弃
- PAT基础编程 5-21 求特殊方程的正整数解 (15分)
- 分布式内存数据库---redis配置文件常用配置介绍
- scala起步