#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 = 1e17+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+1, INF);
vector<bool> vis(N, false);
dist[s] = 0;
while (true) {
int u = N;
for (int i = 0; i < N; i++)
if (!vis[i] && dist[i] < dist[u])
u = i;
if (u == N) break;
for (auto [v, i] : g[typ][u]) if (dist[v] > dist[u] + w[i])
dist[v] = dist[u] + w[i], prev[v] = i;
vis[u] = true;
}
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+1, INF);
vector<bool> vis(N, false);
dist[s] = 0;
while (true) {
int u = N;
for (int i = 0; i < N; i++)
if (!vis[i] && dist[i] < dist[u])
u = i;
if (u == N) break;
for (auto [v, i] : g[0][u]) if (i != reve)
dist[v] = min(dist[v], dist[u] + w[i]);
if (b[reve] == u)
dist[a[reve]] = min(dist[a[reve]], dist[u] + w[reve]);
vis[u] = true;
}
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 lol = revw[i];
if (used1[i]) lol += getdist(0, N-1, i);
else lol += min(from1[N-1], from1[b[i]] + toN[a[i]] + w[i]);
if (usedN[i]) lol += getdist(N-1, 0, i);
else lol += min(fromN[0], fromN[b[i]] + to1[a[i]] + w[i]);
ans = min(ans, lol);
}
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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
45 ms |
332 KB |
Output is correct |
11 |
Correct |
45 ms |
332 KB |
Output is correct |
12 |
Correct |
49 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
3 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
4524 KB |
Output is correct |
2 |
Correct |
35 ms |
4512 KB |
Output is correct |
3 |
Correct |
36 ms |
4420 KB |
Output is correct |
4 |
Correct |
3 ms |
460 KB |
Output is correct |
5 |
Correct |
3 ms |
332 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 |
25 ms |
4452 KB |
Output is correct |
10 |
Correct |
26 ms |
4420 KB |
Output is correct |
11 |
Correct |
30 ms |
4472 KB |
Output is correct |
12 |
Correct |
30 ms |
4516 KB |
Output is correct |
13 |
Correct |
27 ms |
4412 KB |
Output is correct |
14 |
Correct |
30 ms |
4752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
20 ms |
3404 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
25 ms |
4420 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
23 ms |
4376 KB |
Output is correct |
9 |
Correct |
23 ms |
4460 KB |
Output is correct |
10 |
Correct |
25 ms |
4420 KB |
Output is correct |
11 |
Correct |
24 ms |
4428 KB |
Output is correct |
12 |
Correct |
24 ms |
4520 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
24 ms |
4684 KB |
Output is correct |
20 |
Correct |
30 ms |
4500 KB |
Output is correct |
21 |
Correct |
24 ms |
4444 KB |
Output is correct |
22 |
Correct |
27 ms |
4404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
45 ms |
332 KB |
Output is correct |
11 |
Correct |
45 ms |
332 KB |
Output is correct |
12 |
Correct |
49 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
3 ms |
332 KB |
Output is correct |
17 |
Correct |
27 ms |
4524 KB |
Output is correct |
18 |
Correct |
35 ms |
4512 KB |
Output is correct |
19 |
Correct |
36 ms |
4420 KB |
Output is correct |
20 |
Correct |
3 ms |
460 KB |
Output is correct |
21 |
Correct |
3 ms |
332 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 |
25 ms |
4452 KB |
Output is correct |
26 |
Correct |
26 ms |
4420 KB |
Output is correct |
27 |
Correct |
30 ms |
4472 KB |
Output is correct |
28 |
Correct |
30 ms |
4516 KB |
Output is correct |
29 |
Correct |
27 ms |
4412 KB |
Output is correct |
30 |
Correct |
30 ms |
4752 KB |
Output is correct |
31 |
Correct |
3 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
20 ms |
3404 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
25 ms |
4420 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
0 ms |
332 KB |
Output is correct |
38 |
Correct |
23 ms |
4376 KB |
Output is correct |
39 |
Correct |
23 ms |
4460 KB |
Output is correct |
40 |
Correct |
25 ms |
4420 KB |
Output is correct |
41 |
Correct |
24 ms |
4428 KB |
Output is correct |
42 |
Correct |
24 ms |
4520 KB |
Output is correct |
43 |
Correct |
0 ms |
332 KB |
Output is correct |
44 |
Correct |
0 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 |
0 ms |
332 KB |
Output is correct |
48 |
Correct |
0 ms |
332 KB |
Output is correct |
49 |
Correct |
24 ms |
4684 KB |
Output is correct |
50 |
Correct |
30 ms |
4500 KB |
Output is correct |
51 |
Correct |
24 ms |
4444 KB |
Output is correct |
52 |
Correct |
27 ms |
4404 KB |
Output is correct |
53 |
Correct |
27 ms |
4300 KB |
Output is correct |
54 |
Correct |
30 ms |
4356 KB |
Output is correct |
55 |
Correct |
29 ms |
4440 KB |
Output is correct |
56 |
Correct |
3 ms |
332 KB |
Output is correct |
57 |
Correct |
2 ms |
332 KB |
Output is correct |
58 |
Correct |
122 ms |
3416 KB |
Output is correct |
59 |
Correct |
134 ms |
4188 KB |
Output is correct |
60 |
Correct |
122 ms |
4196 KB |
Output is correct |
61 |
Correct |
103 ms |
4256 KB |
Output is correct |
62 |
Correct |
126 ms |
4224 KB |
Output is correct |
63 |
Correct |
126 ms |
4156 KB |
Output is correct |
64 |
Correct |
129 ms |
4512 KB |
Output is correct |
65 |
Correct |
120 ms |
4568 KB |
Output is correct |
66 |
Correct |
102 ms |
4420 KB |
Output is correct |
67 |
Correct |
19 ms |
3960 KB |
Output is correct |
68 |
Correct |
29 ms |
5516 KB |
Output is correct |
69 |
Correct |
26 ms |
5556 KB |
Output is correct |
70 |
Correct |
30 ms |
5556 KB |
Output is correct |
71 |
Correct |
29 ms |
5504 KB |
Output is correct |
72 |
Correct |
28 ms |
5656 KB |
Output is correct |
73 |
Correct |
28 ms |
5580 KB |
Output is correct |
74 |
Correct |
29 ms |
5572 KB |
Output is correct |