#include <bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
*/
using namespace std;
// using namespace __gnu_pbds;
using namespace chrono;
// mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
/*
template <class T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
*/
//***************** CONSTANTS *****************
const int MXN = 200;
const int MXM = 50000;
const int64_t INF = 1'500'000'000'000'000;
//***************** GLOBAL VARIABLES *****************
vector<array<int, 2>> g[2][MXN];
int U[MXM], V[MXM], w[MXM], rev[MXM];
int N, M;
//***************** AUXILIARY STRUCTS *****************
void dijkstra(int S, int T, int idx, int64_t dist[], bool used[]){
int prev[N]; memset(prev, -1, sizeof prev);
vector<bool> vis(N, false);
for(int i = 0; i <= N; i++) dist[i] = INF;
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[idx][u])
if(dist[v] > dist[u] + w[i])
dist[v] = dist[u] + w[i], prev[v] = i;
vis[u] = 1;
}
if(idx) return;
for(int cur = T; prev[cur] != -1; cur = U[prev[cur]])
used[prev[cur]] = 1;
}
int64_t dijkstra(int S, int T, int idx){
vector<bool> vis(N, 0);
int64_t dist[N + 1];
for(int i = 0; i <= N; i++)
dist[i] = INF;
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 != idx){
dist[v] = min(dist[v], dist[u] + w[i]);
}
if(V[idx] == u)
dist[U[idx]] = min(dist[U[idx]], dist[u] + w[idx]);
vis[u] = 1;
}
return dist[T];
}
//***************** MAIN BODY *****************
void solve(){
cin >> N >> M;
for(int i = 0; i < M; i++){
cin >> U[i] >> V[i] >> w[i] >> rev[i];
--U[i], --V[i];
g[0][U[i]].push_back({V[i], i});
g[1][V[i]].push_back({U[i], i});
}
bool used[3][M];
memset(used, 0, sizeof used);
int64_t from[2][N + 1], to[2][N + 1];
dijkstra(0, N - 1, 0, from[0], used[0]);
dijkstra(N - 1, 0, 0, from[1], used[1]);
dijkstra(0, N - 1, 1, to[0], used[2]);
dijkstra(N - 1, 0, 1, to[1], used[2]);
int64_t ans = from[0][N - 1] + from[1][0];
for(int i = 0; i < M; i++){
int64_t temp = rev[i];
if(used[0][i])
temp += dijkstra(0, N - 1, i);
else temp += min(from[0][N - 1], from[0][V[i]] + to[1][U[i]] + w[i]);
if(used[1][i])
temp += dijkstra(N - 1, 0, i);
else temp += min(from[1][0], from[1][V[i]] + to[0][U[i]] + w[i]);
ans = min(ans, temp);
}
cout << (ans >= INF ? -1 : ans) << '\n';
}
//***************** *****************
int32_t main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
#ifdef LOCAL
auto begin = high_resolution_clock::now();
#endif
int tc = 1;
// cin >> tc;
for (int t = 0; t < tc; t++)
solve();
#ifdef LOCAL
auto end = high_resolution_clock::now();
cout << fixed << setprecision(4);
cout << "Execution Time: " << duration_cast<duration<double>>(end - begin).count() << "seconds" << endl;
#endif
return 0;
}
/*
If code gives a WA, check for the following :
1. I/O format
2. Are you clearing all global variables in between tests if multitests are a thing
3. Can you definitively prove the logic
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
396 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
4 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 |
1 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 |
50 ms |
380 KB |
Output is correct |
11 |
Correct |
58 ms |
332 KB |
Output is correct |
12 |
Correct |
58 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
4 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
3 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
2508 KB |
Output is correct |
2 |
Correct |
27 ms |
2544 KB |
Output is correct |
3 |
Correct |
27 ms |
2516 KB |
Output is correct |
4 |
Correct |
4 ms |
332 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 |
24 ms |
2588 KB |
Output is correct |
10 |
Correct |
32 ms |
2500 KB |
Output is correct |
11 |
Correct |
28 ms |
2584 KB |
Output is correct |
12 |
Correct |
27 ms |
2484 KB |
Output is correct |
13 |
Correct |
27 ms |
2460 KB |
Output is correct |
14 |
Correct |
27 ms |
2744 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 |
19 ms |
1956 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
24 ms |
2472 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
23 ms |
2488 KB |
Output is correct |
9 |
Correct |
22 ms |
2500 KB |
Output is correct |
10 |
Correct |
24 ms |
2508 KB |
Output is correct |
11 |
Correct |
25 ms |
2504 KB |
Output is correct |
12 |
Correct |
24 ms |
2600 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 |
0 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 |
23 ms |
2576 KB |
Output is correct |
20 |
Correct |
33 ms |
2564 KB |
Output is correct |
21 |
Correct |
25 ms |
2556 KB |
Output is correct |
22 |
Correct |
23 ms |
2436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
396 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
4 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 |
1 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 |
50 ms |
380 KB |
Output is correct |
11 |
Correct |
58 ms |
332 KB |
Output is correct |
12 |
Correct |
58 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
4 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 |
28 ms |
2508 KB |
Output is correct |
18 |
Correct |
27 ms |
2544 KB |
Output is correct |
19 |
Correct |
27 ms |
2516 KB |
Output is correct |
20 |
Correct |
4 ms |
332 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 |
24 ms |
2588 KB |
Output is correct |
26 |
Correct |
32 ms |
2500 KB |
Output is correct |
27 |
Correct |
28 ms |
2584 KB |
Output is correct |
28 |
Correct |
27 ms |
2484 KB |
Output is correct |
29 |
Correct |
27 ms |
2460 KB |
Output is correct |
30 |
Correct |
27 ms |
2744 KB |
Output is correct |
31 |
Correct |
3 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
19 ms |
1956 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
24 ms |
2472 KB |
Output is correct |
36 |
Correct |
0 ms |
332 KB |
Output is correct |
37 |
Correct |
0 ms |
332 KB |
Output is correct |
38 |
Correct |
23 ms |
2488 KB |
Output is correct |
39 |
Correct |
22 ms |
2500 KB |
Output is correct |
40 |
Correct |
24 ms |
2508 KB |
Output is correct |
41 |
Correct |
25 ms |
2504 KB |
Output is correct |
42 |
Correct |
24 ms |
2600 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 |
0 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 |
23 ms |
2576 KB |
Output is correct |
50 |
Correct |
33 ms |
2564 KB |
Output is correct |
51 |
Correct |
25 ms |
2556 KB |
Output is correct |
52 |
Correct |
23 ms |
2436 KB |
Output is correct |
53 |
Correct |
25 ms |
2448 KB |
Output is correct |
54 |
Correct |
28 ms |
2584 KB |
Output is correct |
55 |
Correct |
28 ms |
2556 KB |
Output is correct |
56 |
Correct |
3 ms |
332 KB |
Output is correct |
57 |
Correct |
2 ms |
332 KB |
Output is correct |
58 |
Correct |
128 ms |
1996 KB |
Output is correct |
59 |
Correct |
124 ms |
1980 KB |
Output is correct |
60 |
Correct |
134 ms |
1996 KB |
Output is correct |
61 |
Correct |
110 ms |
2000 KB |
Output is correct |
62 |
Correct |
126 ms |
1964 KB |
Output is correct |
63 |
Correct |
128 ms |
2020 KB |
Output is correct |
64 |
Correct |
154 ms |
2124 KB |
Output is correct |
65 |
Correct |
121 ms |
2208 KB |
Output is correct |
66 |
Correct |
104 ms |
2124 KB |
Output is correct |
67 |
Correct |
18 ms |
1892 KB |
Output is correct |
68 |
Correct |
25 ms |
2516 KB |
Output is correct |
69 |
Correct |
25 ms |
2432 KB |
Output is correct |
70 |
Correct |
36 ms |
2460 KB |
Output is correct |
71 |
Correct |
27 ms |
2496 KB |
Output is correct |
72 |
Correct |
35 ms |
2544 KB |
Output is correct |
73 |
Correct |
28 ms |
2612 KB |
Output is correct |
74 |
Correct |
38 ms |
2556 KB |
Output is correct |