-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1724_2.cc
69 lines (56 loc) · 1.51 KB
/
1724_2.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <queue>
#include <climits>
using namespace std;
struct City {
int n, len, cost;
City() {}
City(int nn, int ll, int cc) : n(nn), len(ll), cost(cc) {}
};
bool operator < (const City &c1, const City &c2) {
if(c1.len != c2.len) return c2.len < c1.len;
else return c2.cost < c1.cost;
}
pair<int, int> citymap[101][101];
pair<int, int> minval[101];
bool used[101][10001];
int main() {
int K, N, R;
cin >> K >> N >> R;
for(int i = 1; i <= N; ++i)
for(int j = 1; j <= N; ++j)
citymap[i][j] = make_pair(INT_MAX, INT_MAX);
while(R--) {
int s, d, l, t;
cin >> s >> d >> l >> t;
citymap[s][d] = make_pair(l, t);
}
for(int i = 1; i <= N; ++i) {
minval[i] = make_pair(INT_MAX, INT_MAX);
}
priority_queue<City> q;
City c(1, 0, 0);
q.push(c);
minval[1] = make_pair(0, 0);
while(!q.empty()) {
City city = q.top();
q.pop();
if(city.n == N) {
cout << city.len << endl;
return 0;
}
used[city.n][city.cost] = true;
for(int i = 1; i <= N; ++i) {
if(citymap[city.n][i].first == INT_MAX) continue;
int len = city.len + citymap[city.n][i].first;
int cost = city.cost + citymap[city.n][i].second;
if(cost > K) continue;
if(!used[i][cost]) {
City c(i, len, cost);
q.push(c);
}
}
}
cout << -1 << endl;
return 0;
}