#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lowbit(x) x&-x
#define ckmin(a, b) a = min(a, b)
#define ckmax(a, b) a = max(a, b)
const int maxn = 4e5 + 5;
const ll INF = 2e9 + 7;
struct edge{
int a, b, c, d;
edge(){}
edge(int _a, int _b, int _c, int _d) : a(_a), b(_b), c(_c), d(_d){}
};
int n, m;
vector<int> adj[maxn], radj[maxn];
vector<edge> e;
int invalid[50004], crit[50004];
int main(void){
fastio;
cin>>n>>m;
for(int i = 0; i < m; i++){
int a, b, c, d;
cin>>a>>b>>c>>d;
a--, b--;
adj[a].pb(i);
radj[b].pb(i);
e.pb(edge(a, b, c, d));
}
vector<ll> dist(n, INF), dist2(n, INF), rdist(n, INF), rdist2(n, INF);
vector<int> from(n), from2(n);
dist[0] = 0, dist2[n - 1] = 0, rdist[n - 1] = 0, rdist2[0] = 0;
auto dijkstra = [&](vector<ll>& dist, vector<int>& from) -> void{
vector<int> vis(n);
for(int i = 0; i < n; i++){
ll mn = INF, id = 0;
for(int j = 0; j < n; j++) if(!vis[j] && dist[j] < mn) mn = dist[j], id = j;
vis[id] = 1;
for(auto x : adj[id]){
if(invalid[x]) continue;
auto [a, b, c, d] = e[x];
if(dist[b] > dist[a] + c) from[b] = a;
dist[b] = min(dist[b], dist[a] + c);
}
}
};
auto rdijkstra = [&](vector<ll>& dist) -> void{
vector<int> vis(n);
for(int i = 0; i < n; i++){
ll mn = INF, id = 0;
for(int j = 0; j < n; j++) if(!vis[j] && dist[j] < mn) mn = dist[j], id = j;
vis[id] = 1;
for(auto x : radj[id]){
auto [a, b, c, d] = e[x];
dist[a] = min(dist[a], dist[b] + c);
}
}
};
dijkstra(dist, from);
dijkstra(dist2, from2);
rdijkstra(rdist);
rdijkstra(rdist2);
auto find_crit = [&](int st, int ed, vector<ll> &dist, vector<int> &from){
if(dist[ed] >= INF) return;
int pos = ed;
while(pos != st){
int org = pos;
for(auto x : radj[pos]){
auto [a, b, c, d] = e[x];
if(dist[pos] == dist[a] + c && from[pos] == a){
crit[x] = 1;
pos = a;
break;
}
}
}
};
find_crit(0, n - 1, dist, from);
find_crit(n - 1, 0, dist2, from2);
ll ans = dist[n - 1] + dist2[0];
int cnt = 0;
for(int i = 0; i < m; i++){
auto [a, b, c, d] = e[i];
if(crit[i]){
cnt++;
if(cnt > n * 2) break;
// if(cnt > n * 10) break;
invalid[i] = 1;
e.pb(edge(b, a, c, d));
adj[b].pb(m);
vector<ll> ndist(n, INF), ndist2(n, INF);
ndist[0] = 0, ndist2[n - 1] = 0;
dijkstra(ndist, from), dijkstra(ndist2, from);
ans = min(ans, ndist[n - 1] + ndist2[0] + d);
e.pop_back();
adj[b].pop_back();
invalid[i] = 0;
continue;
}
//invert : becomes b->a
ll d1 = min(dist[n - 1], dist[b] + rdist[a] + c);
ll d2 = min(dist2[0], dist2[b] + rdist2[a] + c);
ans = min(ans, d1 + d2 + d);
}
cout<<(ans < INF ? ans : -1)<<"\n";
}
Compilation message
ho_t4.cpp: In lambda function:
ho_t4.cpp:74:8: warning: unused variable 'org' [-Wunused-variable]
74 | int org = pos;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19288 KB |
Output is correct |
2 |
Correct |
5 ms |
19292 KB |
Output is correct |
3 |
Correct |
6 ms |
19508 KB |
Output is correct |
4 |
Correct |
7 ms |
19288 KB |
Output is correct |
5 |
Correct |
4 ms |
19292 KB |
Output is correct |
6 |
Correct |
4 ms |
19292 KB |
Output is correct |
7 |
Correct |
4 ms |
19292 KB |
Output is correct |
8 |
Correct |
4 ms |
19292 KB |
Output is correct |
9 |
Correct |
4 ms |
19288 KB |
Output is correct |
10 |
Correct |
41 ms |
19504 KB |
Output is correct |
11 |
Correct |
54 ms |
19292 KB |
Output is correct |
12 |
Correct |
50 ms |
19496 KB |
Output is correct |
13 |
Correct |
5 ms |
19288 KB |
Output is correct |
14 |
Correct |
6 ms |
19292 KB |
Output is correct |
15 |
Correct |
6 ms |
19432 KB |
Output is correct |
16 |
Correct |
6 ms |
19292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
20944 KB |
Output is correct |
2 |
Correct |
22 ms |
20796 KB |
Output is correct |
3 |
Correct |
24 ms |
20948 KB |
Output is correct |
4 |
Correct |
6 ms |
19544 KB |
Output is correct |
5 |
Correct |
8 ms |
19292 KB |
Output is correct |
6 |
Correct |
5 ms |
19292 KB |
Output is correct |
7 |
Correct |
4 ms |
19488 KB |
Output is correct |
8 |
Correct |
4 ms |
19292 KB |
Output is correct |
9 |
Correct |
19 ms |
20804 KB |
Output is correct |
10 |
Correct |
18 ms |
20812 KB |
Output is correct |
11 |
Correct |
23 ms |
20820 KB |
Output is correct |
12 |
Correct |
22 ms |
20944 KB |
Output is correct |
13 |
Correct |
22 ms |
20824 KB |
Output is correct |
14 |
Correct |
22 ms |
20944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
19292 KB |
Output is correct |
2 |
Correct |
5 ms |
19292 KB |
Output is correct |
3 |
Correct |
17 ms |
20792 KB |
Output is correct |
4 |
Correct |
4 ms |
19292 KB |
Output is correct |
5 |
Correct |
18 ms |
20948 KB |
Output is correct |
6 |
Correct |
4 ms |
19292 KB |
Output is correct |
7 |
Correct |
5 ms |
19292 KB |
Output is correct |
8 |
Correct |
17 ms |
20808 KB |
Output is correct |
9 |
Correct |
16 ms |
20880 KB |
Output is correct |
10 |
Correct |
20 ms |
20884 KB |
Output is correct |
11 |
Correct |
19 ms |
20948 KB |
Output is correct |
12 |
Correct |
19 ms |
20948 KB |
Output is correct |
13 |
Correct |
4 ms |
19292 KB |
Output is correct |
14 |
Correct |
5 ms |
19288 KB |
Output is correct |
15 |
Correct |
4 ms |
19544 KB |
Output is correct |
16 |
Correct |
4 ms |
19292 KB |
Output is correct |
17 |
Correct |
4 ms |
19292 KB |
Output is correct |
18 |
Correct |
4 ms |
19292 KB |
Output is correct |
19 |
Correct |
18 ms |
20772 KB |
Output is correct |
20 |
Correct |
19 ms |
21716 KB |
Output is correct |
21 |
Correct |
28 ms |
21740 KB |
Output is correct |
22 |
Correct |
19 ms |
21716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19288 KB |
Output is correct |
2 |
Correct |
5 ms |
19292 KB |
Output is correct |
3 |
Correct |
6 ms |
19508 KB |
Output is correct |
4 |
Correct |
7 ms |
19288 KB |
Output is correct |
5 |
Correct |
4 ms |
19292 KB |
Output is correct |
6 |
Correct |
4 ms |
19292 KB |
Output is correct |
7 |
Correct |
4 ms |
19292 KB |
Output is correct |
8 |
Correct |
4 ms |
19292 KB |
Output is correct |
9 |
Correct |
4 ms |
19288 KB |
Output is correct |
10 |
Correct |
41 ms |
19504 KB |
Output is correct |
11 |
Correct |
54 ms |
19292 KB |
Output is correct |
12 |
Correct |
50 ms |
19496 KB |
Output is correct |
13 |
Correct |
5 ms |
19288 KB |
Output is correct |
14 |
Correct |
6 ms |
19292 KB |
Output is correct |
15 |
Correct |
6 ms |
19432 KB |
Output is correct |
16 |
Correct |
6 ms |
19292 KB |
Output is correct |
17 |
Correct |
21 ms |
20944 KB |
Output is correct |
18 |
Correct |
22 ms |
20796 KB |
Output is correct |
19 |
Correct |
24 ms |
20948 KB |
Output is correct |
20 |
Correct |
6 ms |
19544 KB |
Output is correct |
21 |
Correct |
8 ms |
19292 KB |
Output is correct |
22 |
Correct |
5 ms |
19292 KB |
Output is correct |
23 |
Correct |
4 ms |
19488 KB |
Output is correct |
24 |
Correct |
4 ms |
19292 KB |
Output is correct |
25 |
Correct |
19 ms |
20804 KB |
Output is correct |
26 |
Correct |
18 ms |
20812 KB |
Output is correct |
27 |
Correct |
23 ms |
20820 KB |
Output is correct |
28 |
Correct |
22 ms |
20944 KB |
Output is correct |
29 |
Correct |
22 ms |
20824 KB |
Output is correct |
30 |
Correct |
22 ms |
20944 KB |
Output is correct |
31 |
Correct |
6 ms |
19292 KB |
Output is correct |
32 |
Correct |
5 ms |
19292 KB |
Output is correct |
33 |
Correct |
17 ms |
20792 KB |
Output is correct |
34 |
Correct |
4 ms |
19292 KB |
Output is correct |
35 |
Correct |
18 ms |
20948 KB |
Output is correct |
36 |
Correct |
4 ms |
19292 KB |
Output is correct |
37 |
Correct |
5 ms |
19292 KB |
Output is correct |
38 |
Correct |
17 ms |
20808 KB |
Output is correct |
39 |
Correct |
16 ms |
20880 KB |
Output is correct |
40 |
Correct |
20 ms |
20884 KB |
Output is correct |
41 |
Correct |
19 ms |
20948 KB |
Output is correct |
42 |
Correct |
19 ms |
20948 KB |
Output is correct |
43 |
Correct |
4 ms |
19292 KB |
Output is correct |
44 |
Correct |
5 ms |
19288 KB |
Output is correct |
45 |
Correct |
4 ms |
19544 KB |
Output is correct |
46 |
Correct |
4 ms |
19292 KB |
Output is correct |
47 |
Correct |
4 ms |
19292 KB |
Output is correct |
48 |
Correct |
4 ms |
19292 KB |
Output is correct |
49 |
Correct |
18 ms |
20772 KB |
Output is correct |
50 |
Correct |
19 ms |
21716 KB |
Output is correct |
51 |
Correct |
28 ms |
21740 KB |
Output is correct |
52 |
Correct |
19 ms |
21716 KB |
Output is correct |
53 |
Correct |
28 ms |
21908 KB |
Output is correct |
54 |
Correct |
35 ms |
21716 KB |
Output is correct |
55 |
Correct |
35 ms |
21716 KB |
Output is correct |
56 |
Correct |
8 ms |
19292 KB |
Output is correct |
57 |
Correct |
7 ms |
19428 KB |
Output is correct |
58 |
Correct |
142 ms |
21716 KB |
Output is correct |
59 |
Correct |
140 ms |
21632 KB |
Output is correct |
60 |
Correct |
178 ms |
21652 KB |
Output is correct |
61 |
Correct |
114 ms |
21712 KB |
Output is correct |
62 |
Correct |
135 ms |
21736 KB |
Output is correct |
63 |
Correct |
181 ms |
21716 KB |
Output is correct |
64 |
Correct |
160 ms |
21640 KB |
Output is correct |
65 |
Correct |
180 ms |
21588 KB |
Output is correct |
66 |
Correct |
237 ms |
21584 KB |
Output is correct |
67 |
Correct |
15 ms |
21716 KB |
Output is correct |
68 |
Correct |
18 ms |
21908 KB |
Output is correct |
69 |
Correct |
22 ms |
22092 KB |
Output is correct |
70 |
Correct |
27 ms |
21996 KB |
Output is correct |
71 |
Correct |
23 ms |
21972 KB |
Output is correct |
72 |
Correct |
23 ms |
21972 KB |
Output is correct |
73 |
Correct |
28 ms |
21968 KB |
Output is correct |
74 |
Correct |
28 ms |
21956 KB |
Output is correct |