#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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
416 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
4 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 |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
51 ms |
380 KB |
Output is correct |
11 |
Correct |
49 ms |
332 KB |
Output is correct |
12 |
Correct |
52 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 |
4 ms |
400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
3524 KB |
Output is correct |
2 |
Correct |
35 ms |
3464 KB |
Output is correct |
3 |
Correct |
28 ms |
3576 KB |
Output is correct |
4 |
Correct |
4 ms |
332 KB |
Output is correct |
5 |
Correct |
4 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 |
3604 KB |
Output is correct |
10 |
Correct |
26 ms |
3408 KB |
Output is correct |
11 |
Correct |
33 ms |
3508 KB |
Output is correct |
12 |
Correct |
29 ms |
3500 KB |
Output is correct |
13 |
Correct |
28 ms |
3396 KB |
Output is correct |
14 |
Correct |
29 ms |
3652 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 |
1956 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
24 ms |
3356 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
24 ms |
3276 KB |
Output is correct |
9 |
Correct |
23 ms |
3176 KB |
Output is correct |
10 |
Correct |
24 ms |
3260 KB |
Output is correct |
11 |
Correct |
24 ms |
3192 KB |
Output is correct |
12 |
Correct |
25 ms |
3208 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
24 ms |
3248 KB |
Output is correct |
20 |
Correct |
33 ms |
3212 KB |
Output is correct |
21 |
Correct |
25 ms |
3184 KB |
Output is correct |
22 |
Correct |
24 ms |
3148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
416 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
4 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 |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
51 ms |
380 KB |
Output is correct |
11 |
Correct |
49 ms |
332 KB |
Output is correct |
12 |
Correct |
52 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 |
4 ms |
400 KB |
Output is correct |
17 |
Correct |
27 ms |
3524 KB |
Output is correct |
18 |
Correct |
35 ms |
3464 KB |
Output is correct |
19 |
Correct |
28 ms |
3576 KB |
Output is correct |
20 |
Correct |
4 ms |
332 KB |
Output is correct |
21 |
Correct |
4 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 |
3604 KB |
Output is correct |
26 |
Correct |
26 ms |
3408 KB |
Output is correct |
27 |
Correct |
33 ms |
3508 KB |
Output is correct |
28 |
Correct |
29 ms |
3500 KB |
Output is correct |
29 |
Correct |
28 ms |
3396 KB |
Output is correct |
30 |
Correct |
29 ms |
3652 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 |
1956 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
24 ms |
3356 KB |
Output is correct |
36 |
Correct |
0 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
24 ms |
3276 KB |
Output is correct |
39 |
Correct |
23 ms |
3176 KB |
Output is correct |
40 |
Correct |
24 ms |
3260 KB |
Output is correct |
41 |
Correct |
24 ms |
3192 KB |
Output is correct |
42 |
Correct |
25 ms |
3208 KB |
Output is correct |
43 |
Correct |
0 ms |
332 KB |
Output is correct |
44 |
Correct |
0 ms |
332 KB |
Output is correct |
45 |
Correct |
0 ms |
332 KB |
Output is correct |
46 |
Correct |
0 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
0 ms |
332 KB |
Output is correct |
49 |
Correct |
24 ms |
3248 KB |
Output is correct |
50 |
Correct |
33 ms |
3212 KB |
Output is correct |
51 |
Correct |
25 ms |
3184 KB |
Output is correct |
52 |
Correct |
24 ms |
3148 KB |
Output is correct |
53 |
Correct |
29 ms |
3288 KB |
Output is correct |
54 |
Correct |
29 ms |
3308 KB |
Output is correct |
55 |
Correct |
29 ms |
3292 KB |
Output is correct |
56 |
Correct |
3 ms |
380 KB |
Output is correct |
57 |
Correct |
2 ms |
392 KB |
Output is correct |
58 |
Correct |
124 ms |
2508 KB |
Output is correct |
59 |
Correct |
125 ms |
2584 KB |
Output is correct |
60 |
Correct |
127 ms |
2596 KB |
Output is correct |
61 |
Correct |
120 ms |
2508 KB |
Output is correct |
62 |
Correct |
126 ms |
2520 KB |
Output is correct |
63 |
Correct |
126 ms |
2604 KB |
Output is correct |
64 |
Correct |
141 ms |
2684 KB |
Output is correct |
65 |
Correct |
120 ms |
2784 KB |
Output is correct |
66 |
Correct |
108 ms |
2760 KB |
Output is correct |
67 |
Correct |
18 ms |
2444 KB |
Output is correct |
68 |
Correct |
28 ms |
3468 KB |
Output is correct |
69 |
Correct |
26 ms |
3420 KB |
Output is correct |
70 |
Correct |
29 ms |
3504 KB |
Output is correct |
71 |
Correct |
30 ms |
3424 KB |
Output is correct |
72 |
Correct |
27 ms |
3508 KB |
Output is correct |
73 |
Correct |
29 ms |
3508 KB |
Output is correct |
74 |
Correct |
30 ms |
3524 KB |
Output is correct |