#include<bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream &out, string str) {
for(char c : str) out << c;
return out;
}
template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
out << "{";
for(auto it = a.begin(); it != a.end(); it = next(it))
out << (it != a.begin() ? ", " : "") << *it;
return out << "}";
}
void dump() { cerr << "\n"; }
template<class T, class... Ts> void dump(T a, Ts... x) {
cerr << a << ", ";
dump(x...);
}
#ifdef DEBUG
# define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
# define debug(...) false
#endif
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define ST first
#define ND second
template<class T> int size(T && a) { return a.size(); }
using LL = long long;
using PII = pair<int, int>;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(m), b(m), c(m), d(m);
vector<vector<PII>> g(n), r(n);
REP(i, m) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
a[i]--, b[i]--;
g[a[i]].emplace_back(b[i], i);
r[b[i]].emplace_back(a[i], i);
}
vector<bool> on_path(m);
auto dijkstra = [&](vector<vector<PII>> &adj, int source, int banned, bool mark) {
vector<int> dst(n, 1e9), last(n, -1);
priority_queue<PII> Q;
dst[source] = 0;
Q.emplace(0, source);
while(!Q.empty()) {
int d, v;
tie(d, v) = Q.top();
Q.pop();
if(-d != dst[v]) continue;
for(auto &[u, e] : adj[v]) {
if(banned == e) continue;
if(dst[u] > dst[v] + c[e]) {
if(mark) last[u] = e;
dst[u] = dst[v] + c[e];
Q.emplace(-dst[u], u);
}
}
}
REP(i, n) if(last[i] != -1)
on_path[last[i]] = true;
return dst;
};
auto norm_source = dijkstra(g, 0, -1, true);
auto norm_sink = dijkstra(g, n - 1, -1, true);
auto rev_source = dijkstra(r, 0, -1, false);
auto rev_sink = dijkstra(r, n - 1, -1, false);
auto add_edge = [&](int i) {
g[b[i]].emplace_back(a[i], size(a));
r[a[i]].emplace_back(b[i], size(b));
a.emplace_back(b[i]);
b.emplace_back(a[i]);
c.emplace_back(c[i]);
};
auto pop_edge = [&](int i) {
g[b[i]].pop_back();
r[a[i]].pop_back();
a.pop_back();
b.pop_back();
c.pop_back();
};
LL ans = norm_source[n - 1] + norm_sink[0];
REP(i, m) {
if(on_path[i]) {
add_edge(i);
auto source = dijkstra(g, 0, i, false);
auto sink = dijkstra(g, n - 1, i, false);
ans = min(ans, (LL) source[n - 1] + sink[0] + d[i]);
pop_edge(i);
}
else {
int source = min(norm_source[n - 1], norm_source[b[i]] + c[i] + rev_sink[a[i]]);
int sink = min(norm_sink[0], norm_sink[b[i]] + c[i] + rev_source[a[i]]);
ans = min(ans, (LL) source + sink + d[i]);
}
}
if(ans >= 1e9) ans = -1;
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
22 ms |
504 KB |
Output is correct |
4 |
Correct |
25 ms |
380 KB |
Output is correct |
5 |
Correct |
6 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
25 ms |
376 KB |
Output is correct |
11 |
Correct |
29 ms |
380 KB |
Output is correct |
12 |
Correct |
28 ms |
376 KB |
Output is correct |
13 |
Correct |
8 ms |
376 KB |
Output is correct |
14 |
Correct |
15 ms |
376 KB |
Output is correct |
15 |
Correct |
13 ms |
248 KB |
Output is correct |
16 |
Correct |
14 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
149 ms |
2680 KB |
Output is correct |
2 |
Correct |
149 ms |
2680 KB |
Output is correct |
3 |
Correct |
148 ms |
2680 KB |
Output is correct |
4 |
Correct |
20 ms |
504 KB |
Output is correct |
5 |
Correct |
11 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
53 ms |
2612 KB |
Output is correct |
10 |
Correct |
50 ms |
2612 KB |
Output is correct |
11 |
Correct |
107 ms |
2680 KB |
Output is correct |
12 |
Correct |
104 ms |
2672 KB |
Output is correct |
13 |
Correct |
100 ms |
2680 KB |
Output is correct |
14 |
Correct |
85 ms |
2808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
99 ms |
2044 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
109 ms |
2612 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
44 ms |
2612 KB |
Output is correct |
9 |
Correct |
51 ms |
2612 KB |
Output is correct |
10 |
Correct |
82 ms |
2612 KB |
Output is correct |
11 |
Correct |
77 ms |
2608 KB |
Output is correct |
12 |
Correct |
87 ms |
2680 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
89 ms |
2684 KB |
Output is correct |
20 |
Correct |
80 ms |
2680 KB |
Output is correct |
21 |
Correct |
85 ms |
2608 KB |
Output is correct |
22 |
Correct |
87 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
22 ms |
504 KB |
Output is correct |
4 |
Correct |
25 ms |
380 KB |
Output is correct |
5 |
Correct |
6 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
25 ms |
376 KB |
Output is correct |
11 |
Correct |
29 ms |
380 KB |
Output is correct |
12 |
Correct |
28 ms |
376 KB |
Output is correct |
13 |
Correct |
8 ms |
376 KB |
Output is correct |
14 |
Correct |
15 ms |
376 KB |
Output is correct |
15 |
Correct |
13 ms |
248 KB |
Output is correct |
16 |
Correct |
14 ms |
464 KB |
Output is correct |
17 |
Correct |
149 ms |
2680 KB |
Output is correct |
18 |
Correct |
149 ms |
2680 KB |
Output is correct |
19 |
Correct |
148 ms |
2680 KB |
Output is correct |
20 |
Correct |
20 ms |
504 KB |
Output is correct |
21 |
Correct |
11 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
53 ms |
2612 KB |
Output is correct |
26 |
Correct |
50 ms |
2612 KB |
Output is correct |
27 |
Correct |
107 ms |
2680 KB |
Output is correct |
28 |
Correct |
104 ms |
2672 KB |
Output is correct |
29 |
Correct |
100 ms |
2680 KB |
Output is correct |
30 |
Correct |
85 ms |
2808 KB |
Output is correct |
31 |
Correct |
14 ms |
376 KB |
Output is correct |
32 |
Correct |
6 ms |
376 KB |
Output is correct |
33 |
Correct |
99 ms |
2044 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
109 ms |
2612 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
376 KB |
Output is correct |
38 |
Correct |
44 ms |
2612 KB |
Output is correct |
39 |
Correct |
51 ms |
2612 KB |
Output is correct |
40 |
Correct |
82 ms |
2612 KB |
Output is correct |
41 |
Correct |
77 ms |
2608 KB |
Output is correct |
42 |
Correct |
87 ms |
2680 KB |
Output is correct |
43 |
Correct |
5 ms |
376 KB |
Output is correct |
44 |
Correct |
5 ms |
256 KB |
Output is correct |
45 |
Correct |
5 ms |
376 KB |
Output is correct |
46 |
Correct |
5 ms |
376 KB |
Output is correct |
47 |
Correct |
5 ms |
376 KB |
Output is correct |
48 |
Correct |
5 ms |
376 KB |
Output is correct |
49 |
Correct |
89 ms |
2684 KB |
Output is correct |
50 |
Correct |
80 ms |
2680 KB |
Output is correct |
51 |
Correct |
85 ms |
2608 KB |
Output is correct |
52 |
Correct |
87 ms |
2636 KB |
Output is correct |
53 |
Correct |
156 ms |
2680 KB |
Output is correct |
54 |
Correct |
171 ms |
2736 KB |
Output is correct |
55 |
Correct |
159 ms |
2612 KB |
Output is correct |
56 |
Correct |
23 ms |
376 KB |
Output is correct |
57 |
Correct |
22 ms |
376 KB |
Output is correct |
58 |
Correct |
109 ms |
2104 KB |
Output is correct |
59 |
Correct |
122 ms |
2136 KB |
Output is correct |
60 |
Correct |
164 ms |
2044 KB |
Output is correct |
61 |
Correct |
103 ms |
2040 KB |
Output is correct |
62 |
Correct |
117 ms |
2104 KB |
Output is correct |
63 |
Correct |
164 ms |
2168 KB |
Output is correct |
64 |
Correct |
727 ms |
2364 KB |
Output is correct |
65 |
Correct |
766 ms |
2368 KB |
Output is correct |
66 |
Correct |
996 ms |
2344 KB |
Output is correct |
67 |
Correct |
23 ms |
2036 KB |
Output is correct |
68 |
Correct |
56 ms |
2612 KB |
Output is correct |
69 |
Correct |
53 ms |
2680 KB |
Output is correct |
70 |
Correct |
112 ms |
2680 KB |
Output is correct |
71 |
Correct |
120 ms |
2612 KB |
Output is correct |
72 |
Correct |
109 ms |
2612 KB |
Output is correct |
73 |
Correct |
123 ms |
2680 KB |
Output is correct |
74 |
Correct |
106 ms |
2740 KB |
Output is correct |