/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 2000+100, M = 1e5+10, K = 52, MX = 30;
int n, m;
vector<array<ll, 4>> g[N], r[N];
vector<array<ll, 4>> E;
ll d1[N], d2[N], d3[N], d4[N];
vector<bool> in;
void solve(){
cin >> n >> m;
in.resize(m);
for(int i = 0; i < m; ++i){
int u, v, c, d; cin >> u >> v >> c >> d;
g[u].pb({v, c, d, i});
r[v].pb({u, c, d, i});
E.pb({u, v, c, d});
}
for(int i = 1; i <= n; ++i) d1[i] = d2[i] = d3[i] = d4[i] = 1e15;
vector<bool> used(n+1);
priority_queue<array<ll, 3>> Q;
Q.push({0, 1, -1});
d1[1] = 0;
while(!Q.empty()){
int v = Q.top()[1]; int e = Q.top()[2]; Q.pop();
if(used[v]) continue;
if(e >= 0)
in[e] = 1;
used[v] = 1;
for(auto U: g[v]){
int u = U[0];
if(d1[u] > d1[v] + U[1]){
d1[u] = d1[v] + U[1];
Q.push({-d1[u], u, U[3]});
}
}
}
fill(all(used), false);
Q.push({0, 1, -1});
d3[1] = 0;
while(!Q.empty()){
int v = Q.top()[1]; int e = Q.top()[2]; Q.pop();
if(used[v]) continue;
if(e >= 0)
in[e] = 1;
used[v] = 1;
for(auto U: r[v]){
int u = U[0];
if(d3[u] > d3[v] + U[1]){
d3[u] = d3[v] + U[1];
Q.push({-d3[u], u, U[3]});
}
}
}
fill(all(used), false);
Q.push({0, n, -1});
d2[n] = 0;
while(!Q.empty()){
int v = Q.top()[1]; int e = Q.top()[2]; Q.pop();
if(used[v]) continue;
if(e >= 0)
in[e] = 1;
used[v] = 1;
for(auto U: g[v]){
int u = U[0];
if(d2[u] > d2[v] + U[1]){
d2[u] = d2[v] + U[1];
Q.push({-d2[u], u, U[3]});
}
}
}
fill(all(used), false);
Q.push({0, n, -1});
d4[n] = 0;
while(!Q.empty()){
int v = Q.top()[1]; int e = Q.top()[2]; Q.pop();
if(used[v]) continue;
if(e >= 0)
in[e] = 1;
used[v] = 1;
for(auto U: r[v]){
int u = U[0];
if(d4[u] > d4[v] + U[1]){
d4[u] = d4[v] + U[1];
Q.push({-d4[u], u, U[3]});
}
}
}
ll ans = d1[n] + d2[1];
// cout << d1[n] << ' ' << d2[1] << '\n';
for(int i = 0; i < m; ++i){
int u = E[i][0], v = E[i][1];
ll w = E[i][2], d = E[i][3];
if(in[i]){
fill(all(used), false);
vector<ll> D(n+1, 1e15), D1(n+1, 1e15);
D[1] = 0;
Q.push({0, 1, -1});
while(!Q.empty()){
int x = Q.top()[1]; Q.pop();
if(used[x]) continue;
used[x] = 1;
for(auto U: g[x]){
if(U[3] == i) continue;
int y = U[0];
if(D[y] > D[x] + U[1]){
D[y] = D[x] + U[1];
Q.push({-D[y], y, -1});
}
}
if(x == v){
if(D[u] > D[v] + w){
D[u] = D[v] + w;
Q.push({-D[u], u, -1});
}
}
}
fill(all(used), false);
D1[n] = 0;
Q.push({0, n, -1});
while(!Q.empty()){
int x = Q.top()[1]; Q.pop();
if(used[x]) continue;
used[x] = 1;
for(auto U: g[x]){
if(U[3] == i) continue;
int y = U[0];
if(D1[y] > D1[x] + U[1]){
D1[y] = D1[x] + U[1];
Q.push({-D1[y], y, -1});
}
}
if(x == v){
if(D1[u] > D1[v] + w){
D1[u] = D1[v] + w;
Q.push({-D1[u], u, -1});
}
}
}
ans = min(ans, D[n] + D1[1] + d);
// for(int i = 1; i <= n; ++i) cout << D[i] << ' ';
// en;
}else{
ans = min({
ans,
d1[v] + d4[u] + d + w + min(d2[1], d2[v] + d3[u] + w),
min(d1[n], d1[v] + d4[u] + w) + d2[v] + d3[u] + d + w
});
}
}
// for(int i = 1; i <= n; ++i){
// cout << d1[i] << ' ' << d2[i] << '\n';
// }
cout << (ans > 1e10 ? -1 : ans);
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
// en;
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:178:15: warning: unused variable 'aa' [-Wunused-variable]
178 | int tt = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
24 ms |
752 KB |
Output is correct |
4 |
Correct |
25 ms |
856 KB |
Output is correct |
5 |
Correct |
2 ms |
856 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
564 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
18 ms |
700 KB |
Output is correct |
11 |
Correct |
22 ms |
756 KB |
Output is correct |
12 |
Correct |
21 ms |
604 KB |
Output is correct |
13 |
Correct |
5 ms |
604 KB |
Output is correct |
14 |
Correct |
12 ms |
752 KB |
Output is correct |
15 |
Correct |
10 ms |
604 KB |
Output is correct |
16 |
Correct |
13 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
7884 KB |
Output is correct |
2 |
Correct |
163 ms |
7880 KB |
Output is correct |
3 |
Correct |
180 ms |
8328 KB |
Output is correct |
4 |
Correct |
21 ms |
860 KB |
Output is correct |
5 |
Correct |
8 ms |
732 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
55 ms |
8064 KB |
Output is correct |
10 |
Correct |
56 ms |
7928 KB |
Output is correct |
11 |
Correct |
112 ms |
7880 KB |
Output is correct |
12 |
Correct |
112 ms |
8292 KB |
Output is correct |
13 |
Correct |
116 ms |
8132 KB |
Output is correct |
14 |
Correct |
99 ms |
8684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
111 ms |
6928 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
126 ms |
7628 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
49 ms |
7628 KB |
Output is correct |
9 |
Correct |
48 ms |
8012 KB |
Output is correct |
10 |
Correct |
83 ms |
7884 KB |
Output is correct |
11 |
Correct |
81 ms |
7628 KB |
Output is correct |
12 |
Correct |
84 ms |
7884 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
87 ms |
8392 KB |
Output is correct |
20 |
Correct |
81 ms |
7880 KB |
Output is correct |
21 |
Correct |
108 ms |
7628 KB |
Output is correct |
22 |
Correct |
89 ms |
7880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
24 ms |
752 KB |
Output is correct |
4 |
Correct |
25 ms |
856 KB |
Output is correct |
5 |
Correct |
2 ms |
856 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
564 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
18 ms |
700 KB |
Output is correct |
11 |
Correct |
22 ms |
756 KB |
Output is correct |
12 |
Correct |
21 ms |
604 KB |
Output is correct |
13 |
Correct |
5 ms |
604 KB |
Output is correct |
14 |
Correct |
12 ms |
752 KB |
Output is correct |
15 |
Correct |
10 ms |
604 KB |
Output is correct |
16 |
Correct |
13 ms |
604 KB |
Output is correct |
17 |
Correct |
163 ms |
7884 KB |
Output is correct |
18 |
Correct |
163 ms |
7880 KB |
Output is correct |
19 |
Correct |
180 ms |
8328 KB |
Output is correct |
20 |
Correct |
21 ms |
860 KB |
Output is correct |
21 |
Correct |
8 ms |
732 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
55 ms |
8064 KB |
Output is correct |
26 |
Correct |
56 ms |
7928 KB |
Output is correct |
27 |
Correct |
112 ms |
7880 KB |
Output is correct |
28 |
Correct |
112 ms |
8292 KB |
Output is correct |
29 |
Correct |
116 ms |
8132 KB |
Output is correct |
30 |
Correct |
99 ms |
8684 KB |
Output is correct |
31 |
Correct |
14 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
111 ms |
6928 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
126 ms |
7628 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
49 ms |
7628 KB |
Output is correct |
39 |
Correct |
48 ms |
8012 KB |
Output is correct |
40 |
Correct |
83 ms |
7884 KB |
Output is correct |
41 |
Correct |
81 ms |
7628 KB |
Output is correct |
42 |
Correct |
84 ms |
7884 KB |
Output is correct |
43 |
Correct |
1 ms |
600 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
600 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
87 ms |
8392 KB |
Output is correct |
50 |
Correct |
81 ms |
7880 KB |
Output is correct |
51 |
Correct |
108 ms |
7628 KB |
Output is correct |
52 |
Correct |
89 ms |
7880 KB |
Output is correct |
53 |
Correct |
179 ms |
7628 KB |
Output is correct |
54 |
Correct |
194 ms |
7820 KB |
Output is correct |
55 |
Correct |
175 ms |
7628 KB |
Output is correct |
56 |
Correct |
25 ms |
752 KB |
Output is correct |
57 |
Correct |
24 ms |
604 KB |
Output is correct |
58 |
Correct |
87 ms |
6960 KB |
Output is correct |
59 |
Correct |
94 ms |
6864 KB |
Output is correct |
60 |
Correct |
132 ms |
6864 KB |
Output is correct |
61 |
Correct |
75 ms |
6860 KB |
Output is correct |
62 |
Correct |
97 ms |
6824 KB |
Output is correct |
63 |
Correct |
130 ms |
6864 KB |
Output is correct |
64 |
Correct |
637 ms |
7136 KB |
Output is correct |
65 |
Correct |
669 ms |
7308 KB |
Output is correct |
66 |
Correct |
860 ms |
7356 KB |
Output is correct |
67 |
Correct |
13 ms |
5568 KB |
Output is correct |
68 |
Correct |
66 ms |
7880 KB |
Output is correct |
69 |
Correct |
54 ms |
8128 KB |
Output is correct |
70 |
Correct |
119 ms |
7884 KB |
Output is correct |
71 |
Correct |
117 ms |
8128 KB |
Output is correct |
72 |
Correct |
122 ms |
7884 KB |
Output is correct |
73 |
Correct |
120 ms |
8248 KB |
Output is correct |
74 |
Correct |
116 ms |
7876 KB |
Output is correct |