-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_10217.cpp
More file actions
99 lines (86 loc) · 2.22 KB
/
BOJ_10217.cpp
File metadata and controls
99 lines (86 loc) · 2.22 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define INF 987654321
using namespace std;
struct Edge {
int e, time, cost;
Edge(int e, int cost, int time) {
this->e = e;
this->cost = cost;
this->time = time;
}
};
struct Q {
int node, cost, time;
Q(int node, int cost, int time) {
this->node = node;
this->cost = cost;
this->time = time;
}
bool operator<(const Q &second) const { return time > second.time; }
};
int t, n, m, k, a, b, c, d;
vector<vector<int>> dp;
vector<vector<Edge *>> edges;
bool compare(Edge *a, Edge *b) { return a->time < b->time; }
int main(void) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> t;
while (t--) {
cin >> n >> m >> k;
dp.assign(n + 1, vector<int>(m + 1, INF));
edges.resize(n + 1);
for (int i = 0; i < k; i++) {
cin >> a >> b >> c >> d;
edges[a].push_back(new Edge(b, c, d));
}
for (int i = 0; i <= m; i++) {
dp[1][i] = 0;
}
for (int i = 1; i <= n; i++) {
sort(edges[i].begin(), edges[i].end(), compare);
}
priority_queue<Q> pq;
pq.push({1, 0, 0});
while (!pq.empty()) {
int start_time = pq.top().time;
int start_cost = pq.top().cost;
int start_node = pq.top().node;
if (start_node == n)
break;
pq.pop();
if (dp[start_node][start_cost] < start_time)
continue;
for (int i = 0; i < edges[start_node].size(); i++) {
int next_time = start_time + edges[start_node][i]->time;
int next_cost = start_cost + edges[start_node][i]->cost;
int next_node = edges[start_node][i]->e;
if (next_cost > m)
continue;
if (dp[next_node][next_cost] > next_time) {
for (int j = next_cost; j <= m; j++) {
if (dp[next_node][j] <= next_time) {
break;
}
dp[next_node][j] = next_time;
}
dp[next_node][next_cost] = next_time;
pq.push({next_node, next_cost, next_time});
}
}
}
int min = INF;
for (int i = 0; i <= m; i++) {
if (dp[n][i] < min)
min = dp[n][i];
}
if (min == INF)
cout << "Poor KCM\n";
else
cout << min << "\n";
}
return 0;
}