#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll INF = 1e18;
struct Edge {
int to, c;
ll p;
};
map<int, vector<Edge>> graph[100001];
ll dp[100001];
map<int, ll> dp2[100001], psum[100001];
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v, c;
ll p;
cin >> u >> v >> c >> p;
graph[u][c].push_back({v, c, p});
graph[v][c].push_back({u, c, p});
psum[u][c] += p;
psum[v][c] += p;
}
memset(dp, 0x3f, sizeof dp);
dp[1] = 0;
priority_queue<tuple<ll, int, int>> pq;
pq.push({0, 1, 0});
while (pq.size()) {
ll cost;
int node, c;
tie(cost, node, c) = pq.top();
pq.pop();
if (c) {
if (dp2[node][c] != -cost) continue;
for (Edge i : graph[node][c]) {
// We can't flip i in this case
ll case1 = psum[node][c] - i.p;
if (case1 - cost < dp[i.to]) {
dp[i.to] = case1 - cost;
pq.push({-dp[i.to], i.to, 0});
}
}
} else {
if (dp[node] != -cost) continue;
for (auto &i : graph[node]) {
for (Edge j : i.second) {
// Case 1: We don't flip j
ll case1 = psum[node][j.c] - j.p - cost;
if (case1 < dp[j.to]) {
dp[j.to] = case1;
pq.push({-dp[j.to], j.to, 0});
}
// Case 2: We flip j but not another edge of the same colour
ll case2 = j.p - cost;
if (case2 < dp[j.to]) {
dp[j.to] = case2;
pq.push({-dp[j.to], j.to, 0});
}
// Case 3: We flip j and another edge of the same colour
ll case3 = -cost;
if (!dp2[j.to].count(j.c) || case3 < dp2[j.to][j.c]) {
dp2[j.to][j.c] = case3;
pq.push({-dp2[j.to][j.c], j.to, j.c});
}
}
}
}
}
cout << (dp[n] > INF ? -1 : dp[n]);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15192 KB |
Output is correct |
2 |
Correct |
3 ms |
15196 KB |
Output is correct |
3 |
Correct |
4 ms |
15196 KB |
Output is correct |
4 |
Correct |
3 ms |
15196 KB |
Output is correct |
5 |
Correct |
3 ms |
15192 KB |
Output is correct |
6 |
Correct |
3 ms |
15196 KB |
Output is correct |
7 |
Correct |
4 ms |
15452 KB |
Output is correct |
8 |
Correct |
3 ms |
15196 KB |
Output is correct |
9 |
Correct |
6 ms |
15960 KB |
Output is correct |
10 |
Correct |
6 ms |
15704 KB |
Output is correct |
11 |
Correct |
5 ms |
15452 KB |
Output is correct |
12 |
Correct |
4 ms |
15452 KB |
Output is correct |
13 |
Correct |
4 ms |
15452 KB |
Output is correct |
14 |
Correct |
4 ms |
15452 KB |
Output is correct |
15 |
Correct |
5 ms |
15452 KB |
Output is correct |
16 |
Correct |
4 ms |
15452 KB |
Output is correct |
17 |
Correct |
4 ms |
15452 KB |
Output is correct |
18 |
Correct |
3 ms |
15452 KB |
Output is correct |
19 |
Correct |
4 ms |
15452 KB |
Output is correct |
20 |
Correct |
4 ms |
15452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
33732 KB |
Output is correct |
2 |
Correct |
47 ms |
24788 KB |
Output is correct |
3 |
Correct |
101 ms |
29896 KB |
Output is correct |
4 |
Correct |
67 ms |
27332 KB |
Output is correct |
5 |
Correct |
737 ms |
79164 KB |
Output is correct |
6 |
Correct |
520 ms |
68164 KB |
Output is correct |
7 |
Correct |
260 ms |
52700 KB |
Output is correct |
8 |
Correct |
274 ms |
46828 KB |
Output is correct |
9 |
Correct |
285 ms |
46984 KB |
Output is correct |
10 |
Correct |
219 ms |
45756 KB |
Output is correct |
11 |
Correct |
78 ms |
31060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15192 KB |
Output is correct |
2 |
Correct |
3 ms |
15196 KB |
Output is correct |
3 |
Correct |
4 ms |
15196 KB |
Output is correct |
4 |
Correct |
3 ms |
15196 KB |
Output is correct |
5 |
Correct |
3 ms |
15192 KB |
Output is correct |
6 |
Correct |
3 ms |
15196 KB |
Output is correct |
7 |
Correct |
4 ms |
15452 KB |
Output is correct |
8 |
Correct |
3 ms |
15196 KB |
Output is correct |
9 |
Correct |
6 ms |
15960 KB |
Output is correct |
10 |
Correct |
6 ms |
15704 KB |
Output is correct |
11 |
Correct |
5 ms |
15452 KB |
Output is correct |
12 |
Correct |
4 ms |
15452 KB |
Output is correct |
13 |
Correct |
4 ms |
15452 KB |
Output is correct |
14 |
Correct |
4 ms |
15452 KB |
Output is correct |
15 |
Correct |
5 ms |
15452 KB |
Output is correct |
16 |
Correct |
4 ms |
15452 KB |
Output is correct |
17 |
Correct |
4 ms |
15452 KB |
Output is correct |
18 |
Correct |
3 ms |
15452 KB |
Output is correct |
19 |
Correct |
4 ms |
15452 KB |
Output is correct |
20 |
Correct |
4 ms |
15452 KB |
Output is correct |
21 |
Correct |
119 ms |
33732 KB |
Output is correct |
22 |
Correct |
47 ms |
24788 KB |
Output is correct |
23 |
Correct |
101 ms |
29896 KB |
Output is correct |
24 |
Correct |
67 ms |
27332 KB |
Output is correct |
25 |
Correct |
737 ms |
79164 KB |
Output is correct |
26 |
Correct |
520 ms |
68164 KB |
Output is correct |
27 |
Correct |
260 ms |
52700 KB |
Output is correct |
28 |
Correct |
274 ms |
46828 KB |
Output is correct |
29 |
Correct |
285 ms |
46984 KB |
Output is correct |
30 |
Correct |
219 ms |
45756 KB |
Output is correct |
31 |
Correct |
78 ms |
31060 KB |
Output is correct |
32 |
Correct |
78 ms |
25552 KB |
Output is correct |
33 |
Correct |
83 ms |
27080 KB |
Output is correct |
34 |
Correct |
306 ms |
47024 KB |
Output is correct |
35 |
Correct |
201 ms |
38844 KB |
Output is correct |
36 |
Correct |
233 ms |
44384 KB |
Output is correct |
37 |
Correct |
272 ms |
46248 KB |
Output is correct |
38 |
Correct |
244 ms |
52812 KB |
Output is correct |
39 |
Correct |
83 ms |
27808 KB |
Output is correct |
40 |
Correct |
288 ms |
46864 KB |
Output is correct |
41 |
Correct |
306 ms |
47184 KB |
Output is correct |
42 |
Correct |
398 ms |
56000 KB |
Output is correct |
43 |
Correct |
126 ms |
35124 KB |
Output is correct |
44 |
Correct |
244 ms |
38468 KB |
Output is correct |
45 |
Correct |
218 ms |
43676 KB |
Output is correct |
46 |
Correct |
217 ms |
44332 KB |
Output is correct |
47 |
Correct |
273 ms |
45976 KB |
Output is correct |
48 |
Correct |
837 ms |
84656 KB |
Output is correct |