#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 2e9;
vector<ll> D(int x, vector<vector<array<int, 3> > > &v, vector<bool> &u, int ub = 0, int bl = 0) {
int n = sz(v) - 1;
vector<ll> d(n + 1, -1);
vector<int> prv(n + 1);
vector<int> pre(n + 1);
d[x] = 0;
set<array<int, 2> > s;
s.insert({0, x});
while(sz(s)) {
int y = (*s.begin())[1];
s.erase(s.begin());
for(array<int, 3> z : v[y]) {
if(z[2] == bl) continue;
if(d[z[0]] == -1 || d[z[0]] > d[y] + z[1]) {
if(d[z[0]] != -1) { s.erase(s.find({(int)d[z[0]], z[0]})); }
d[z[0]] = d[y] + z[1];
prv[z[0]] = y;
pre[z[0]] = z[2];
s.insert({(int)d[z[0]], z[0]});
}
}
}
if(ub) {
while(prv[ub]) {
u[pre[ub]] = 1;
ub = prv[ub];
}
}
for(ll &x : d) {
if(x == -1) x = inf;
}
return d;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#ifdef x
freopen("x.txt", "r", stdin);
#endif
int n, m;
cin >> n >> m;
vector<vector<array<int, 3> > > v(n + 1);
vector<vector<array<int, 3> > > u(n + 1);
array<int, 4> x[m + 1];
for(int i = 1; i <= m; ++i) {
for(int j = 0; j < 4; ++j) {
cin >> x[i][j];
}
v[x[i][0]].push_back({x[i][1], x[i][2], i});
u[x[i][1]].push_back({x[i][0], x[i][2], i});
}
vector<bool> rlt(m + 1);
vector<ll> fro = D(1, v, rlt, n);
vector<ll> frn = D(n, v, rlt, 1);
vector<ll> too = D(1, u, rlt);
vector<ll> ton = D(n, u, rlt);
ll fp = fro[n] + frn[1];
for(int i = 1; i <= m; ++i) {
ll tp = 0;
if(rlt[i]) {
v[x[i][1]].push_back({x[i][0], x[i][2], x[i][3]});
tp = x[i][3] + D(1, v, rlt, 0, i)[n] + D(n, v, rlt, 0, i)[1];
v[x[i][1]].pop_back();
} else {
tp = x[i][3]
+ min(fro[n], fro[x[i][1]] + x[i][2] + ton[x[i][0]])
+ min(frn[1], frn[x[i][1]] + x[i][2] + too[x[i][0]]);
}
fp = min(fp, tp);
}
if(fp >= inf) fp = -1;
cout << fp << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
29 ms |
376 KB |
Output is correct |
11 |
Correct |
37 ms |
372 KB |
Output is correct |
12 |
Correct |
34 ms |
388 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
3068 KB |
Output is correct |
2 |
Correct |
22 ms |
3020 KB |
Output is correct |
3 |
Correct |
23 ms |
3020 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 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 |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
26 ms |
2924 KB |
Output is correct |
10 |
Correct |
18 ms |
2992 KB |
Output is correct |
11 |
Correct |
23 ms |
3140 KB |
Output is correct |
12 |
Correct |
28 ms |
2976 KB |
Output is correct |
13 |
Correct |
21 ms |
3100 KB |
Output is correct |
14 |
Correct |
22 ms |
3208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
14 ms |
3016 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
24 ms |
3844 KB |
Output is correct |
6 |
Correct |
0 ms |
312 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
22 ms |
3856 KB |
Output is correct |
9 |
Correct |
17 ms |
3896 KB |
Output is correct |
10 |
Correct |
18 ms |
3900 KB |
Output is correct |
11 |
Correct |
18 ms |
3908 KB |
Output is correct |
12 |
Correct |
18 ms |
3972 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
308 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
18 ms |
3992 KB |
Output is correct |
20 |
Correct |
19 ms |
3912 KB |
Output is correct |
21 |
Correct |
20 ms |
3900 KB |
Output is correct |
22 |
Correct |
18 ms |
3844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
29 ms |
376 KB |
Output is correct |
11 |
Correct |
37 ms |
372 KB |
Output is correct |
12 |
Correct |
34 ms |
388 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
23 ms |
3068 KB |
Output is correct |
18 |
Correct |
22 ms |
3020 KB |
Output is correct |
19 |
Correct |
23 ms |
3020 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 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 |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
26 ms |
2924 KB |
Output is correct |
26 |
Correct |
18 ms |
2992 KB |
Output is correct |
27 |
Correct |
23 ms |
3140 KB |
Output is correct |
28 |
Correct |
28 ms |
2976 KB |
Output is correct |
29 |
Correct |
21 ms |
3100 KB |
Output is correct |
30 |
Correct |
22 ms |
3208 KB |
Output is correct |
31 |
Correct |
2 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
14 ms |
3016 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
24 ms |
3844 KB |
Output is correct |
36 |
Correct |
0 ms |
312 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
22 ms |
3856 KB |
Output is correct |
39 |
Correct |
17 ms |
3896 KB |
Output is correct |
40 |
Correct |
18 ms |
3900 KB |
Output is correct |
41 |
Correct |
18 ms |
3908 KB |
Output is correct |
42 |
Correct |
18 ms |
3972 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
0 ms |
308 KB |
Output is correct |
45 |
Correct |
0 ms |
204 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
18 ms |
3992 KB |
Output is correct |
50 |
Correct |
19 ms |
3912 KB |
Output is correct |
51 |
Correct |
20 ms |
3900 KB |
Output is correct |
52 |
Correct |
18 ms |
3844 KB |
Output is correct |
53 |
Correct |
22 ms |
3900 KB |
Output is correct |
54 |
Correct |
29 ms |
4012 KB |
Output is correct |
55 |
Correct |
26 ms |
3888 KB |
Output is correct |
56 |
Correct |
2 ms |
324 KB |
Output is correct |
57 |
Correct |
2 ms |
332 KB |
Output is correct |
58 |
Correct |
119 ms |
3228 KB |
Output is correct |
59 |
Correct |
138 ms |
3144 KB |
Output is correct |
60 |
Correct |
201 ms |
3132 KB |
Output is correct |
61 |
Correct |
107 ms |
3116 KB |
Output is correct |
62 |
Correct |
131 ms |
3132 KB |
Output is correct |
63 |
Correct |
190 ms |
3168 KB |
Output is correct |
64 |
Correct |
871 ms |
3324 KB |
Output is correct |
65 |
Correct |
901 ms |
3296 KB |
Output is correct |
66 |
Execution timed out |
1081 ms |
3268 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |