#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll inf = 1e17;
vector<ll> dijkstra(vector<vector<array<int, 3>>> v, int source){
int n = v.size();
vector<ll> dis(n, inf);
vector<bool> bl(n, 0);
priority_queue<pair<ll, int>> pq;
dis[source] = 0;
pq.push({0, source});
while(!pq.empty()){
int nd = pq.top().sc; pq.pop();
if(bl[nd]) continue;
bl[nd] = 1;
for(auto [w, c, x]: v[nd]) if(dis[nd]+w < dis[x]){
dis[x] = dis[nd]+w;
pq.push({-dis[x], x});
}
}
return dis;
}
int e[50000][4];
vector<ll> d1, d2, d3, d4;
vector<vector<array<int, 3>>> v, vr;
vector<bool> s1, s2;
vector<bool> bl1, bl2;
int ddd1[200], ddd2[200];
void dfs_1(int nd, int d){
if(bl1[nd]){
return;
}
bl1[nd] = 1;
ddd1[nd] = d;
for(auto [w, c, x]: v[nd]) dfs_1(x, d+1);
}
void dfs_2(int nd, int d){
if(bl2[nd]){
return;
}
bl2[nd] = 1;
ddd2[nd] = d;
for(auto [w, c, x]: v[nd]) dfs_2(x, d+1);
}
void dfs1(int nd){
for(auto [w, c, x]: vr[nd]) if((d1[x] < d1[nd] || (d1[x] == d1[nd] && ddd1[x] < ddd1[nd])) && d1[x]+w == d1[nd]){
s1[c] = 1;
dfs1(x);
break;
}
}
void dfs2(int nd){
for(auto [w, c, x]: vr[nd]) if((d3[x] < d3[nd] || (d3[x] == d3[nd] && ddd2[x] < ddd2[nd])) && d3[x]+w == d3[nd]){
s2[c] = 1;
dfs2(x);
break;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
v.resize(n);
vr.resize(n);
for(int i = 0; i < m; i++){
cin >> e[i][0] >> e[i][1] >> e[i][2] >> e[i][3];
e[i][0]--, e[i][1]--;
v[e[i][0]].pb({e[i][2], i, e[i][1]});
vr[e[i][1]].pb({e[i][2], i, e[i][0]});
}
s1.assign(m, 0), s2.assign(m, 0);
d1 = dijkstra(v, 0), d2 = dijkstra(vr, n-1), d3 = dijkstra(v, n-1), d4 = dijkstra(vr, 0);
bl1.assign(n, 0);
bl2.assign(n, 0);
dfs_1(0, 0);
dfs_2(n-1, 0);
dfs1(n-1);
dfs2(0);
ll ans = inf;
ans = min(ans, d1[n-1]+d3[0]);
for(int i = 0; i < m; i++){
int a = e[i][0], b = e[i][1];
ll w = e[i][2], c = e[i][3];
if(!s2[i] && !s1[i]){
ans = min(ans, c+d1[b]+w+d2[a]+d3[0]);
ans = min(ans, c+d1[n-1]+d3[b]+w+d4[a]);
ans = min(ans, c+d1[b]+w+d2[a]+d3[b]+w+d4[a]);
continue;
}
vector<vector<array<int, 3>>> v0(n);
for(int j = 0; j < m; j++){
if(j == i) v0[e[j][1]].pb({e[j][2], e[j][3], e[j][0]});
else v0[e[j][0]].pb({e[j][2], e[j][3], e[j][1]});
}
if(!s2[i]){
auto dd1 = dijkstra(v0, 0);
ans = min(ans, dd1[n-1]+d3[0]+e[i][3]);
ans = min(ans, dd1[n-1]+d3[b]+w+d4[a]+e[i][3]);
}
else if(!s1[i]){
auto dd2 = dijkstra(v0, n-1);
ans = min(ans, d1[n-1]+dd2[0]+e[i][3]);
ans = min(ans, d1[b]+w+d2[a]+dd2[0]+e[i][3]);
}
else{
auto dd1 = dijkstra(v0, 0), dd2 = dijkstra(v0, n-1);
ans = min(ans, dd1[n-1]+dd2[0]+e[i][3]);
}
}
if(ans >= inf) ans = -1;
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
34 ms |
460 KB |
Output is correct |
11 |
Correct |
34 ms |
460 KB |
Output is correct |
12 |
Correct |
32 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
5196 KB |
Output is correct |
2 |
Correct |
29 ms |
5224 KB |
Output is correct |
3 |
Correct |
31 ms |
5196 KB |
Output is correct |
4 |
Correct |
3 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 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 |
20 ms |
5296 KB |
Output is correct |
10 |
Correct |
20 ms |
5288 KB |
Output is correct |
11 |
Correct |
35 ms |
5324 KB |
Output is correct |
12 |
Correct |
28 ms |
5280 KB |
Output is correct |
13 |
Correct |
29 ms |
5276 KB |
Output is correct |
14 |
Correct |
36 ms |
5452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
30 ms |
4044 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
52 ms |
5188 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
18 ms |
5316 KB |
Output is correct |
9 |
Correct |
17 ms |
5332 KB |
Output is correct |
10 |
Correct |
36 ms |
5168 KB |
Output is correct |
11 |
Correct |
40 ms |
5152 KB |
Output is correct |
12 |
Correct |
34 ms |
5324 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 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 |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
89 ms |
5308 KB |
Output is correct |
20 |
Correct |
50 ms |
5252 KB |
Output is correct |
21 |
Correct |
30 ms |
5188 KB |
Output is correct |
22 |
Correct |
61 ms |
5196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
34 ms |
460 KB |
Output is correct |
11 |
Correct |
34 ms |
460 KB |
Output is correct |
12 |
Correct |
32 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
27 ms |
5196 KB |
Output is correct |
18 |
Correct |
29 ms |
5224 KB |
Output is correct |
19 |
Correct |
31 ms |
5196 KB |
Output is correct |
20 |
Correct |
3 ms |
460 KB |
Output is correct |
21 |
Correct |
2 ms |
460 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 |
20 ms |
5296 KB |
Output is correct |
26 |
Correct |
20 ms |
5288 KB |
Output is correct |
27 |
Correct |
35 ms |
5324 KB |
Output is correct |
28 |
Correct |
28 ms |
5280 KB |
Output is correct |
29 |
Correct |
29 ms |
5276 KB |
Output is correct |
30 |
Correct |
36 ms |
5452 KB |
Output is correct |
31 |
Correct |
5 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
30 ms |
4044 KB |
Output is correct |
34 |
Correct |
0 ms |
332 KB |
Output is correct |
35 |
Correct |
52 ms |
5188 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
18 ms |
5316 KB |
Output is correct |
39 |
Correct |
17 ms |
5332 KB |
Output is correct |
40 |
Correct |
36 ms |
5168 KB |
Output is correct |
41 |
Correct |
40 ms |
5152 KB |
Output is correct |
42 |
Correct |
34 ms |
5324 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
0 ms |
204 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 |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
89 ms |
5308 KB |
Output is correct |
50 |
Correct |
50 ms |
5252 KB |
Output is correct |
51 |
Correct |
30 ms |
5188 KB |
Output is correct |
52 |
Correct |
61 ms |
5196 KB |
Output is correct |
53 |
Correct |
25 ms |
5164 KB |
Output is correct |
54 |
Correct |
34 ms |
5196 KB |
Output is correct |
55 |
Correct |
42 ms |
5176 KB |
Output is correct |
56 |
Correct |
2 ms |
460 KB |
Output is correct |
57 |
Correct |
2 ms |
460 KB |
Output is correct |
58 |
Correct |
254 ms |
4152 KB |
Output is correct |
59 |
Correct |
292 ms |
4172 KB |
Output is correct |
60 |
Correct |
364 ms |
4132 KB |
Output is correct |
61 |
Correct |
257 ms |
4132 KB |
Output is correct |
62 |
Correct |
331 ms |
4172 KB |
Output is correct |
63 |
Correct |
367 ms |
4172 KB |
Output is correct |
64 |
Correct |
730 ms |
4312 KB |
Output is correct |
65 |
Correct |
669 ms |
4264 KB |
Output is correct |
66 |
Correct |
675 ms |
4228 KB |
Output is correct |
67 |
Correct |
17 ms |
4792 KB |
Output is correct |
68 |
Correct |
26 ms |
6476 KB |
Output is correct |
69 |
Correct |
22 ms |
6520 KB |
Output is correct |
70 |
Correct |
32 ms |
6364 KB |
Output is correct |
71 |
Correct |
29 ms |
6404 KB |
Output is correct |
72 |
Correct |
29 ms |
6340 KB |
Output is correct |
73 |
Correct |
34 ms |
6420 KB |
Output is correct |
74 |
Correct |
32 ms |
6476 KB |
Output is correct |