#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 200 + 2;
const int M = 5e4 + 2;
const ll linf = 1e18;
ll dist[N][N], di[N];
set<pair<int, int>> g[N];
int u[M], v[M], c[M], d[M], is[M], prv[N], n, m;
void Dijkstra(int x) {
for (int i = 0; i < n; i++) di[i] = linf;
vector<int> was(n);
priority_queue<pair<ll, int>> pq;
di[x] = 0;
pq.push({0, x});
while (!pq.empty()) {
ll v = -pq.top().first;
int s = pq.top().second;
pq.pop();
if (was[s] == 1) continue;
was[s] = 1;
for (auto zz : g[s]) {
int u = zz.first; int i = zz.second;
if (di[u] > v + c[i]) {
di[u] = v + c[i];
pq.push({-di[u], u});
prv[u] = i;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) dist[i][j] = linf;
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i] >> c[i] >> d[i];
u[i] -= 1; v[i] -= 1;
smin(dist[u[i]][v[i]],(ll) c[i]);
g[u[i]].insert({v[i], i});
}
for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) smin(dist[i][j], dist[i][k] + dist[k][j]);
for (int i = 0; i < n; i++) prv[i] = -1;
Dijkstra(0);
for (int i = 0; i < n; i++) if (prv[i] != -1) is[prv[i]] = 1;
for (int i = 0; i < n; i++) prv[i] = -1;
Dijkstra(n - 1);
for (int i = 0; i < n; i++) if (prv[i] != -1) is[prv[i]] = 1;
ll ans = linf;
if (dist[0][n - 1] != linf && dist[n - 1][0] != linf) ans = dist[0][n - 1] + dist[n - 1][0];
for (int i = 0; i < m; i++) {
if (is[i] == 0) {
ll d1 = min(dist[0][n - 1], dist[0][v[i]] + c[i] + dist[u[i]][n - 1]);
ll d2 = min(dist[n - 1][0], dist[n - 1][v[i]] + c[i] + dist[u[i]][0]);
if (d1 < linf && d2 < linf) smin(ans, d1 + d2 + d[i]);
continue;
}
g[u[i]].erase({v[i], i});
g[v[i]].insert({u[i], i});
ll ret = d[i];
Dijkstra(0);
if (di[n - 1] == linf) {
g[v[i]].erase({u[i], i});
g[u[i]].insert({v[i], i});
continue;
}
ret += di[n - 1];
Dijkstra(n - 1);
if (di[0] == linf) {
g[v[i]].erase({u[i], i});
g[u[i]].insert({v[i], i});
continue;
}
ret += di[0];
smin(ans, ret);
g[v[i]].erase({u[i], i});
g[u[i]].insert({v[i], i});
}
if (ans == linf) ans = -1;
cout << ans << en;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
724 KB |
Output is correct |
2 |
Correct |
9 ms |
596 KB |
Output is correct |
3 |
Correct |
27 ms |
748 KB |
Output is correct |
4 |
Correct |
30 ms |
740 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
9 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
28 ms |
744 KB |
Output is correct |
11 |
Correct |
33 ms |
724 KB |
Output is correct |
12 |
Correct |
33 ms |
848 KB |
Output is correct |
13 |
Correct |
11 ms |
724 KB |
Output is correct |
14 |
Correct |
20 ms |
724 KB |
Output is correct |
15 |
Correct |
12 ms |
736 KB |
Output is correct |
16 |
Correct |
18 ms |
652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
513 ms |
3992 KB |
Output is correct |
2 |
Correct |
542 ms |
4020 KB |
Output is correct |
3 |
Correct |
526 ms |
4040 KB |
Output is correct |
4 |
Correct |
32 ms |
724 KB |
Output is correct |
5 |
Correct |
16 ms |
724 KB |
Output is correct |
6 |
Correct |
9 ms |
596 KB |
Output is correct |
7 |
Correct |
9 ms |
596 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
80 ms |
3916 KB |
Output is correct |
10 |
Correct |
73 ms |
3900 KB |
Output is correct |
11 |
Correct |
149 ms |
4044 KB |
Output is correct |
12 |
Correct |
334 ms |
3992 KB |
Output is correct |
13 |
Correct |
115 ms |
3916 KB |
Output is correct |
14 |
Correct |
88 ms |
3972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
748 KB |
Output is correct |
2 |
Correct |
9 ms |
696 KB |
Output is correct |
3 |
Correct |
639 ms |
3232 KB |
Output is correct |
4 |
Correct |
9 ms |
596 KB |
Output is correct |
5 |
Correct |
716 ms |
3848 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
136 ms |
3952 KB |
Output is correct |
9 |
Correct |
183 ms |
3952 KB |
Output is correct |
10 |
Correct |
475 ms |
4128 KB |
Output is correct |
11 |
Correct |
169 ms |
3916 KB |
Output is correct |
12 |
Correct |
217 ms |
3888 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
548 ms |
3964 KB |
Output is correct |
20 |
Correct |
172 ms |
3932 KB |
Output is correct |
21 |
Correct |
207 ms |
3960 KB |
Output is correct |
22 |
Correct |
203 ms |
3964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
724 KB |
Output is correct |
2 |
Correct |
9 ms |
596 KB |
Output is correct |
3 |
Correct |
27 ms |
748 KB |
Output is correct |
4 |
Correct |
30 ms |
740 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
9 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
28 ms |
744 KB |
Output is correct |
11 |
Correct |
33 ms |
724 KB |
Output is correct |
12 |
Correct |
33 ms |
848 KB |
Output is correct |
13 |
Correct |
11 ms |
724 KB |
Output is correct |
14 |
Correct |
20 ms |
724 KB |
Output is correct |
15 |
Correct |
12 ms |
736 KB |
Output is correct |
16 |
Correct |
18 ms |
652 KB |
Output is correct |
17 |
Correct |
513 ms |
3992 KB |
Output is correct |
18 |
Correct |
542 ms |
4020 KB |
Output is correct |
19 |
Correct |
526 ms |
4040 KB |
Output is correct |
20 |
Correct |
32 ms |
724 KB |
Output is correct |
21 |
Correct |
16 ms |
724 KB |
Output is correct |
22 |
Correct |
9 ms |
596 KB |
Output is correct |
23 |
Correct |
9 ms |
596 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
80 ms |
3916 KB |
Output is correct |
26 |
Correct |
73 ms |
3900 KB |
Output is correct |
27 |
Correct |
149 ms |
4044 KB |
Output is correct |
28 |
Correct |
334 ms |
3992 KB |
Output is correct |
29 |
Correct |
115 ms |
3916 KB |
Output is correct |
30 |
Correct |
88 ms |
3972 KB |
Output is correct |
31 |
Correct |
23 ms |
748 KB |
Output is correct |
32 |
Correct |
9 ms |
696 KB |
Output is correct |
33 |
Correct |
639 ms |
3232 KB |
Output is correct |
34 |
Correct |
9 ms |
596 KB |
Output is correct |
35 |
Correct |
716 ms |
3848 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
136 ms |
3952 KB |
Output is correct |
39 |
Correct |
183 ms |
3952 KB |
Output is correct |
40 |
Correct |
475 ms |
4128 KB |
Output is correct |
41 |
Correct |
169 ms |
3916 KB |
Output is correct |
42 |
Correct |
217 ms |
3888 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
548 ms |
3964 KB |
Output is correct |
50 |
Correct |
172 ms |
3932 KB |
Output is correct |
51 |
Correct |
207 ms |
3960 KB |
Output is correct |
52 |
Correct |
203 ms |
3964 KB |
Output is correct |
53 |
Correct |
683 ms |
3992 KB |
Output is correct |
54 |
Correct |
723 ms |
3996 KB |
Output is correct |
55 |
Correct |
715 ms |
3992 KB |
Output is correct |
56 |
Correct |
28 ms |
844 KB |
Output is correct |
57 |
Correct |
26 ms |
740 KB |
Output is correct |
58 |
Correct |
405 ms |
3352 KB |
Output is correct |
59 |
Correct |
473 ms |
3344 KB |
Output is correct |
60 |
Correct |
637 ms |
3432 KB |
Output is correct |
61 |
Correct |
399 ms |
3276 KB |
Output is correct |
62 |
Correct |
468 ms |
3344 KB |
Output is correct |
63 |
Correct |
648 ms |
3344 KB |
Output is correct |
64 |
Correct |
967 ms |
3924 KB |
Output is correct |
65 |
Execution timed out |
1077 ms |
4036 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |