#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define ld long double
#define ar array
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7;
const int INF = 1e15+5;
const int MXN = 205, MXM = 50005;
vector<ar<int,2>> g[2][MXN];
int a[MXM], b[MXM], w[MXM], revw[MXM], N, M;
void dijkstra(int s, int t, int typ, vector<int> &dist, vector<bool> &used) {
vector<int> prev(N, -1);
used.resize(M, false); dist.resize(N, INF);
priority_queue<ar<int,3>, vector<ar<int,3>>, greater<ar<int,3>>> pq;
pq.push({0, s, -1});
while (!pq.empty()) {
auto [d, u, e] = pq.top(); pq.pop();
if (dist[u] <= d) continue;
dist[u] = d; prev[u] = e;
for (auto [v, i] : g[typ][u]) if (dist[v] > d+w[i])
pq.push({d+w[i], v, i});
}
if (typ) return;
for (int cur = t; prev[cur] != -1; cur = a[prev[cur]])
used[prev[cur]] = true;
}
int getdist(int s, int t, int reve) {
vector<int> dist(N, INF);
priority_queue<ar<int,2>, vector<ar<int,2>>, greater<ar<int,2>>> pq;
pq.push({0, s});
while (!pq.empty()) {
auto [d, u] = pq.top(); pq.pop();
if (dist[u] <= d) continue;
dist[u] = d;
for (auto [v, i] : g[0][u]) if (dist[v] > d+w[i] && i != reve)
pq.push({d+w[i], v});
if (u == b[reve] && dist[a[reve]] > d+w[reve])
pq.push({d+w[reve], a[reve]});
}
return dist[t];
}
void solve() {
cin >> N >> M;
for (int i = 0; i < M; i++) {
cin >> a[i] >> b[i] >> w[i] >> revw[i];
g[0][--a[i]].push_back({--b[i], i});
g[1][b[i]].push_back({a[i], i});
}
vector<int> to1, toN, from1, fromN;
vector<bool> used1, usedN, tmp;
dijkstra(0, N-1, 0, from1, used1);
dijkstra(N-1, 0, 0, fromN, usedN);
dijkstra(0, N-1, 1, to1, tmp);
dijkstra(N-1, 0, 1, toN, tmp);
int ans = from1[N-1] + fromN[0];
for (int i = 0; i < M; i++) {
int tmp = revw[i];
if (used1[i]) tmp += getdist(0, N-1, i);
else tmp += min(from1[N-1], from1[b[i]] + toN[a[i]] + w[i]);
if (usedN[i]) tmp += getdist(N-1, 0, i);
else tmp += min(fromN[0], fromN[b[i]] + to1[a[i]] + w[i]);
ans = min(ans, tmp);
}
if (ans >= INF/2) cout << -1 << '\n';
else cout << ans << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1; //cin >> t;
while (t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
476 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
472 KB |
Output is correct |
10 |
Correct |
28 ms |
460 KB |
Output is correct |
11 |
Correct |
26 ms |
468 KB |
Output is correct |
12 |
Correct |
26 ms |
484 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
476 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
6832 KB |
Output is correct |
2 |
Correct |
80 ms |
6852 KB |
Output is correct |
3 |
Correct |
86 ms |
7044 KB |
Output is correct |
4 |
Correct |
3 ms |
580 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
31 ms |
6148 KB |
Output is correct |
10 |
Correct |
31 ms |
6200 KB |
Output is correct |
11 |
Correct |
82 ms |
6744 KB |
Output is correct |
12 |
Correct |
72 ms |
6984 KB |
Output is correct |
13 |
Correct |
73 ms |
6832 KB |
Output is correct |
14 |
Correct |
83 ms |
7172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
51 ms |
4672 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
87 ms |
6044 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
32 ms |
5668 KB |
Output is correct |
9 |
Correct |
31 ms |
5648 KB |
Output is correct |
10 |
Correct |
90 ms |
5964 KB |
Output is correct |
11 |
Correct |
66 ms |
6024 KB |
Output is correct |
12 |
Correct |
65 ms |
6116 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
89 ms |
6216 KB |
Output is correct |
20 |
Correct |
76 ms |
6012 KB |
Output is correct |
21 |
Correct |
64 ms |
5964 KB |
Output is correct |
22 |
Correct |
71 ms |
6096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
476 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
472 KB |
Output is correct |
10 |
Correct |
28 ms |
460 KB |
Output is correct |
11 |
Correct |
26 ms |
468 KB |
Output is correct |
12 |
Correct |
26 ms |
484 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
476 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
78 ms |
6832 KB |
Output is correct |
18 |
Correct |
80 ms |
6852 KB |
Output is correct |
19 |
Correct |
86 ms |
7044 KB |
Output is correct |
20 |
Correct |
3 ms |
580 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
31 ms |
6148 KB |
Output is correct |
26 |
Correct |
31 ms |
6200 KB |
Output is correct |
27 |
Correct |
82 ms |
6744 KB |
Output is correct |
28 |
Correct |
72 ms |
6984 KB |
Output is correct |
29 |
Correct |
73 ms |
6832 KB |
Output is correct |
30 |
Correct |
83 ms |
7172 KB |
Output is correct |
31 |
Correct |
3 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
51 ms |
4672 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
87 ms |
6044 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
32 ms |
5668 KB |
Output is correct |
39 |
Correct |
31 ms |
5648 KB |
Output is correct |
40 |
Correct |
90 ms |
5964 KB |
Output is correct |
41 |
Correct |
66 ms |
6024 KB |
Output is correct |
42 |
Correct |
65 ms |
6116 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
89 ms |
6216 KB |
Output is correct |
50 |
Correct |
76 ms |
6012 KB |
Output is correct |
51 |
Correct |
64 ms |
5964 KB |
Output is correct |
52 |
Correct |
71 ms |
6096 KB |
Output is correct |
53 |
Correct |
76 ms |
6692 KB |
Output is correct |
54 |
Correct |
100 ms |
6720 KB |
Output is correct |
55 |
Correct |
92 ms |
6704 KB |
Output is correct |
56 |
Correct |
2 ms |
476 KB |
Output is correct |
57 |
Correct |
2 ms |
460 KB |
Output is correct |
58 |
Execution timed out |
1097 ms |
5020 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |