1.题目描述:
ROADS
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15020 | Accepted: 5427 |
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
Source
2.题意概述:
图中每条路都有 路长 和 “过路费” 两个参数,现在只有 K 块钱,要你求起点到终点的最短路,也就是说在 K 花费内的最短路。
3.解题思路:
深搜bfs+优先队列,保证堆顶都是价值最高,这样得到的第一个解就是最优解
4.AC代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#define INF 0x3f3f3f3f
#define maxn 10010
#define N 111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
struct node
{
int to, val, cost, next;
} E[maxn];
struct que
{
int to, val, cost;
que(int a, int b, int c)
{
to = a;
val = b;
cost = c;
}
friend bool operator< (que a, que b)
{
if (a.val == b.val)
return a.cost > b.cost;
return a.val > b.val;
}
};
int cnt;
int head[N];
void init()
{
memset(head, -1, sizeof(head));
cnt = 0;
}
void addedge(int u, int v, int w, int c)
{
E[cnt].to = v;
E[cnt].val = w;
E[cnt].cost = c;
E[cnt].next = head[u];
head[u] = cnt++;
}
int bfs(int sta, int n, int k)
{
priority_queue<que> q;
q.push(que(sta, 0, 0));
while (!q.empty())
{
que pre = q.top();
q.pop();
if (pre.to == n)
return pre.val;
for (int i = head[pre.to]; i != -1; i = E[i].next)
{
if (pre.cost + E[i].cost <= k)
q.push(que(E[i].to, pre.val + E[i].val, pre.cost + E[i].cost));
}
}
return -1;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int k, n, r;
while (~scanf("%d%d%d", &k, &n, &r))
{
init();
while (r--)
{
int s, d, l, t;
scanf("%d%d%d%d", &s, &d, &l, &t);
addedge(s, d, l, t);
}
int ans = bfs(1, n, k);
printf("%d\n", ans);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}