ROADS
Time Limit: 1000MS | | Memory Limit: 65536K | Total Submissions: 9524 | | Accepted: 3523 |
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.
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 :
- 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.
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 Source
有条件的最短路问题,求一个在消耗金钱不大于cost的情况下从1到N的最短路。
优先队列+Dijkstra 47Ms
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 10100 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
const int MOD = (int)1e9 + 7;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
///#pragma comment(linker, "/STACK:102400000,102400000")
struct state
{
int n , d , c;
bool operator < (const state a)const
{
if(a.d == d)return a.c < c;
return a.d < d;
}
};
struct edge
{
int u , v , w , c ,next;
}e[MAXN];
int head[110] , d[110];
int n , m , cost , tot;
void addedge(int u , int v , int w , int c)
{
e[tot].u = u;
e[tot].v = v;
e[tot].w = w;
e[tot].c = c;
e[tot].next = head[u];
head[u]= tot++;
}
void dijkstra()
{
priority_queue <state> q;
state S;
S.n = 1 , S.d = 0 , S.c = 0;
int res = INF;
clr(d , INF);
while(!q.empty())q.pop();
q.push(S);
while(!q.empty())
{
state x , y;
int u , v , d , w , c;
x = q.top() ; q.pop();
u = x.n , d = x.d ;
if(u == n)
{
res = x.d;
break;
}
for(int k = head[u] ; k != -1 ; k = e[k].next)
{
v = e[k].v , w = e[k].w , c = e[k].c;
if(x.c + c <= cost)
{
y.n = v , y.d = d + w , y.c = x.c + c;
q.push(y);
}
}
}
if(res == INF)printf("-1\n");
else printf("%d\n" , res);
}
int main()
{
// freopen("out.text" , "w" , stdout);
scanf("%d%d%d" , &cost , &n , &m);
clr(head , -1);
tot = 0;
while(m -- )
{
int u , v , w , c;
scanf("%d%d%d%d" , &u , &v , &w , &c);
addedge(u , v , w , c);
// cout << "*******" << endl;
}
dijkstra();
return 0;
}
|