POJ 1724 ROADS(優先佇列+spfa)
就是給你n個點,m條邊。然後找到錢最少的情況下,走過了多少路。一個點有可能多次走過,搜的時候保證到達每個點的花費值最小。
ROADS
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10122 | Accepted: 3750 |
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 N that 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 N that 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
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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
#define LL __int64
//#define LL long long
#define INF 0x3fffffff
#define PI 3.1415926535898
using namespace std;
const int maxn = 10005;
int head[maxn];
int n, m, ans;
int total;
int num[maxn];
struct node1
{
int d, l, t;
int next;
} f[maxn];
struct node
{
int n, d, c;
bool operator < (const struct node a)const
{
if(a.d == d) return a.c < c;
return a.d < d;
}
};
void add(int s, int d, int l, int t)
{
f[ans].d = d;
f[ans].t = t;
f[ans].l = l;
f[ans].next = head[s];
head[s] = ans++;
}
void bfs()
{
priority_queue<node>que;
struct node temp, xtemp;
int cnt = INF;
temp.d = 0;
temp.n = 1;
temp.c = 0;
while(!que.empty())
que.pop();
que.push(temp);
while(!que.empty())
{
temp = que.top();
que.pop();
if(temp.n == n)
{
cnt = temp.d;
break;
}
int p = head[temp.n];
while(p != -1)
{
if(temp.c+f[p].t <= total)
{
xtemp.n = f[p].d;
xtemp.c = temp.c+f[p].t;
xtemp.d = temp.d+f[p].l;
que.push(xtemp);
}
p = f[p].next;
}
}
if(cnt == INF)
cout<<"-1"<<endl;
else
cout<<cnt<<endl;
}
int main()
{
while(cin >>total>>n>>m)
{
int s, d, l, t;
memset(head, -1, sizeof(head));
ans = 0;
for(int i = 0; i < m; i++)
{
cin >>s>>d>>l>>t;
add(s, d, l, t);
}
bfs();
}
return 0;
}
相關文章
- POJ 3253 Fence Repair 優先佇列AI佇列
- POJ2431 Expedition (優先佇列)佇列
- POJ 2051(最小堆/優先佇列)佇列
- POJ 3253Fence Repair(哈夫曼&優先佇列)AI佇列
- Sunscreen POJ - 3614(防曬油) 貪心-優先佇列佇列
- PHP優先佇列PHP佇列
- 堆--優先佇列佇列
- 優先佇列 (轉)佇列
- 淺談優先佇列佇列
- STL 優先佇列 用法佇列
- 堆與優先佇列佇列
- 堆和優先佇列佇列
- BZOJ 1724 [Usaco2006 Nov]Fence Repair 切割木板:貪心 優先佇列【合併果子】AI佇列
- 優先佇列的學習記錄--例題:Expedition(POJ2431)佇列
- POJ 3253-Fence Repair(哈夫曼樹-最小值優先佇列)AI佇列
- 優先佇列和堆排序佇列排序
- 堆排序與優先佇列排序佇列
- Java優先佇列(PriorityQueue)示例Java佇列
- 01揹包優先佇列優化佇列優化
- 棧,佇列,優先順序佇列簡單介面使用佇列
- Redis實現任務佇列、優先順序佇列Redis佇列
- NO GAME NO LIFE(優先佇列/最小堆)GAM佇列
- 優先佇列的比較器佇列
- 封裝優先順序佇列封裝佇列
- 二叉堆優先佇列佇列
- 堆——神奇的優先佇列(上)佇列
- 優先佇列的效能測試佇列
- hdu5040 優先佇列+bfs佇列
- POJ 1442-Black Box(動態區間第K小-優先佇列)佇列
- 佇列 優先順序佇列 python 程式碼實現佇列Python
- 演算法面試(三) 優先佇列演算法面試佇列
- STL優先佇列最小堆最大堆佇列
- STL醜數(set+優先佇列)佇列
- 【圖論】拓撲排序+優先佇列圖論排序佇列
- 1007(優先佇列)佇列
- 三、資料結構演算法-棧、佇列、優先佇列、雙端佇列資料結構演算法佇列
- .NET 6 優先佇列 PriorityQueue 實現分析佇列
- Java優先順序佇列DelayedWorkQueue原理分析Java佇列