// put it before the include
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//author : FatihCihan
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
#define int long long
const int inf = 1e18 + 7;
void solve(){
int n,m;
cin >> n >> m;
vector < vector < pair < int , int > > > graph(n+1) , igraph(n+1);
array < int , 4 > edge[m];
bool is_in_path[m];
memset(is_in_path , 0 , sizeof(is_in_path));
for(int i = 0;i<m;i++){
cin >> edge[i][0] >> edge[i][1] >> edge[i][2] >> edge[i][3];
graph[edge[i][0]].push_back({edge[i][1] , i});
igraph[edge[i][1]].push_back({edge[i][0] , i});
}
auto eval = [&](int st , vector < vector < pair < int , int > > > G , int forbidden){
vector < int > vis(n+1 , inf);
vector < pair < int , int > > pq(n+1 , {inf , inf});
pq[st] = {0,-1};
// cout << "forbidden : " << forbidden << endl;
while(true){
int tmp = -1;
pair < int , int > val = {inf,inf};
for(int i = 1;i<=n;i++){
if(vis[i] == inf and val > pq[i]){
tmp = i;
val = pq[i];
}
}
if(tmp == -1 or val.first == inf)break;
// cout << "bfs : " << tmp << " " << val.first << " " << val.second << endl;
// cout << "G : ";for(int i = 0;i<sz(G[tmp]);i++)cout << G[tmp][i].first << " ";cout << endl;
vis[tmp] = val.first;
if(val.second != -1 and forbidden == -1){
is_in_path[val.second] = 1;
}
for(int i = 0;i<sz(G[tmp]);i++){
if(G[tmp][i].second != forbidden){
array < int , 4 > ed = edge[G[tmp][i].second];
pq[G[tmp][i].first] = min(pq[G[tmp][i].first] , {val.first + ed[2] , G[tmp][i].second});
}
}
}
// cout << st << " : ";for(int i = 1;i<=n;i++)cout << vis[i] << " ";cout << endl;
return vis;
};
vector < int > from1 = eval(1 , graph , -1);
vector < int > fromn = eval(n , graph , -1);
vector < int > to1 = eval(1 , igraph , -1);
vector < int > ton = eval(n , igraph , -1);
int ans = from1[n] + fromn[1];
int sayac = 0;
for(int i = 0;i<m;i++){
// cout << "edge : " << edge[i][0] << " " << edge[i][1] << " " << edge[i][2] << " " << edge[i][3] << endl;
int r1 , r2;
if(is_in_path[i]){
// cout << "sayac : " << sayac << " " << i << endl;
sayac++;
// cout << "special : " << edge[i][0] << " " << edge[i][1] << " " << edge[i][2] << " " << edge[i][3] << endl;
vector < int > tfrom1 = eval(1 , graph , i);
// cout << "tfrom1 : ";for(int i = 1;i<=n;i++)cout << tfrom1[i] << " ";cout << endl;
vector < int > tfromn = eval(n , graph , i);
// cout << "tfromn : ";for(int i = 1;i<=n;i++)cout << tfromn[i] << " ";cout << endl;
vector < int > tto1 = eval(1 , igraph , i);
// cout << "tto1 : ";for(int i = 1;i<=n;i++)cout << tto1[i] << " ";cout << endl;
vector < int > tton = eval(n , igraph , i);
// cout << "tton : ";for(int i = 1;i<=n;i++)cout << tton[i] << " ";cout << endl;
r1 = min(tfrom1[n] , tfrom1[edge[i][1]] + edge[i][2] + tton[edge[i][0]]);//gidis
r2 = min(tfromn[1] , tfromn[edge[i][1]] + edge[i][2] + tto1[edge[i][0]]);//donus
// cout << "r : " << r1 << " " << r2 << endl;
}
else{
// cout << "ordinary : " << edge[i][0] << " " << edge[i][1] << " " << edge[i][2] << " " << edge[i][3] << endl;
r1 = min(from1[n] , from1[edge[i][1]] + edge[i][2] + ton[edge[i][0]]);//gidis
r2 = min(fromn[1] , fromn[edge[i][1]] + edge[i][2] + to1[edge[i][0]]);//donus
// cout << "r : " << r1 << " " << r2 << endl;
// cout << "donus : " << fromn[edge[i][1]] << " + " << edge[i][2] << " + " << to1[edge[i][0]] << endl;
}
// cout << "######" << endl;
ans = min(ans , edge[i][3] + r1 + r2);
}
cout << (ans >= inf ? -1 : ans) << endl;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase = 1;//cin >> testcase;
while(testcase--)solve();
cerr << 1000.0 * clock() / CLOCKS_PER_SEC << " ms" << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
464 KB |
Output is correct |
2 |
Correct |
5 ms |
348 KB |
Output is correct |
3 |
Correct |
156 ms |
604 KB |
Output is correct |
4 |
Correct |
152 ms |
344 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
12 ms |
512 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
376 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
70 ms |
468 KB |
Output is correct |
11 |
Correct |
94 ms |
344 KB |
Output is correct |
12 |
Correct |
86 ms |
344 KB |
Output is correct |
13 |
Correct |
49 ms |
600 KB |
Output is correct |
14 |
Correct |
100 ms |
836 KB |
Output is correct |
15 |
Correct |
101 ms |
348 KB |
Output is correct |
16 |
Correct |
118 ms |
580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
651 ms |
6512 KB |
Output is correct |
2 |
Correct |
718 ms |
6284 KB |
Output is correct |
3 |
Correct |
700 ms |
6620 KB |
Output is correct |
4 |
Correct |
138 ms |
600 KB |
Output is correct |
5 |
Correct |
101 ms |
572 KB |
Output is correct |
6 |
Correct |
22 ms |
344 KB |
Output is correct |
7 |
Correct |
2 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
357 ms |
7032 KB |
Output is correct |
10 |
Correct |
354 ms |
6768 KB |
Output is correct |
11 |
Correct |
575 ms |
6476 KB |
Output is correct |
12 |
Correct |
518 ms |
6840 KB |
Output is correct |
13 |
Correct |
558 ms |
6956 KB |
Output is correct |
14 |
Correct |
502 ms |
6688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
139 ms |
576 KB |
Output is correct |
2 |
Correct |
24 ms |
348 KB |
Output is correct |
3 |
Correct |
469 ms |
4800 KB |
Output is correct |
4 |
Correct |
15 ms |
348 KB |
Output is correct |
5 |
Correct |
527 ms |
6116 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
272 ms |
6308 KB |
Output is correct |
9 |
Correct |
270 ms |
6256 KB |
Output is correct |
10 |
Correct |
469 ms |
6204 KB |
Output is correct |
11 |
Correct |
494 ms |
6144 KB |
Output is correct |
12 |
Correct |
495 ms |
6868 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
468 ms |
6972 KB |
Output is correct |
20 |
Correct |
461 ms |
6780 KB |
Output is correct |
21 |
Correct |
452 ms |
6112 KB |
Output is correct |
22 |
Correct |
455 ms |
6260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
464 KB |
Output is correct |
2 |
Correct |
5 ms |
348 KB |
Output is correct |
3 |
Correct |
156 ms |
604 KB |
Output is correct |
4 |
Correct |
152 ms |
344 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
12 ms |
512 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
376 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
70 ms |
468 KB |
Output is correct |
11 |
Correct |
94 ms |
344 KB |
Output is correct |
12 |
Correct |
86 ms |
344 KB |
Output is correct |
13 |
Correct |
49 ms |
600 KB |
Output is correct |
14 |
Correct |
100 ms |
836 KB |
Output is correct |
15 |
Correct |
101 ms |
348 KB |
Output is correct |
16 |
Correct |
118 ms |
580 KB |
Output is correct |
17 |
Correct |
651 ms |
6512 KB |
Output is correct |
18 |
Correct |
718 ms |
6284 KB |
Output is correct |
19 |
Correct |
700 ms |
6620 KB |
Output is correct |
20 |
Correct |
138 ms |
600 KB |
Output is correct |
21 |
Correct |
101 ms |
572 KB |
Output is correct |
22 |
Correct |
22 ms |
344 KB |
Output is correct |
23 |
Correct |
2 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
357 ms |
7032 KB |
Output is correct |
26 |
Correct |
354 ms |
6768 KB |
Output is correct |
27 |
Correct |
575 ms |
6476 KB |
Output is correct |
28 |
Correct |
518 ms |
6840 KB |
Output is correct |
29 |
Correct |
558 ms |
6956 KB |
Output is correct |
30 |
Correct |
502 ms |
6688 KB |
Output is correct |
31 |
Correct |
139 ms |
576 KB |
Output is correct |
32 |
Correct |
24 ms |
348 KB |
Output is correct |
33 |
Correct |
469 ms |
4800 KB |
Output is correct |
34 |
Correct |
15 ms |
348 KB |
Output is correct |
35 |
Correct |
527 ms |
6116 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
272 ms |
6308 KB |
Output is correct |
39 |
Correct |
270 ms |
6256 KB |
Output is correct |
40 |
Correct |
469 ms |
6204 KB |
Output is correct |
41 |
Correct |
494 ms |
6144 KB |
Output is correct |
42 |
Correct |
495 ms |
6868 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
468 ms |
6972 KB |
Output is correct |
50 |
Correct |
461 ms |
6780 KB |
Output is correct |
51 |
Correct |
452 ms |
6112 KB |
Output is correct |
52 |
Correct |
455 ms |
6260 KB |
Output is correct |
53 |
Correct |
706 ms |
6356 KB |
Output is correct |
54 |
Correct |
707 ms |
6396 KB |
Output is correct |
55 |
Correct |
666 ms |
6396 KB |
Output is correct |
56 |
Correct |
146 ms |
576 KB |
Output is correct |
57 |
Correct |
132 ms |
348 KB |
Output is correct |
58 |
Correct |
299 ms |
5160 KB |
Output is correct |
59 |
Correct |
342 ms |
5248 KB |
Output is correct |
60 |
Correct |
462 ms |
5580 KB |
Output is correct |
61 |
Correct |
282 ms |
5456 KB |
Output is correct |
62 |
Correct |
329 ms |
5168 KB |
Output is correct |
63 |
Correct |
449 ms |
5008 KB |
Output is correct |
64 |
Correct |
290 ms |
5100 KB |
Output is correct |
65 |
Correct |
354 ms |
5136 KB |
Output is correct |
66 |
Correct |
391 ms |
5080 KB |
Output is correct |
67 |
Correct |
12 ms |
4548 KB |
Output is correct |
68 |
Correct |
335 ms |
7148 KB |
Output is correct |
69 |
Correct |
362 ms |
6548 KB |
Output is correct |
70 |
Correct |
556 ms |
6404 KB |
Output is correct |
71 |
Correct |
613 ms |
6884 KB |
Output is correct |
72 |
Correct |
580 ms |
6360 KB |
Output is correct |
73 |
Correct |
551 ms |
7088 KB |
Output is correct |
74 |
Correct |
512 ms |
6424 KB |
Output is correct |