#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const int N = 2e2 + 10;
const int M = 5e4 + 10;
const ll Inf = 1e15;
ll n, m;
ll u[M], v[M], c[M], d[M], ans[M], Gd[M];
ll G[N][N], W[N][N], mk[N], dis[N], dis2[N], par[N];
void Build(){
memset(G, 31, sizeof G);
memset(mk, 0, sizeof mk);
memset(dis, 31, sizeof dis);
memset(dis2, 31, sizeof dis2);
memset(W, 0, sizeof W);
memset(par, 0, sizeof par);
for(int i = 0; i < m; i++){
G[u[i]][v[i]] = min(G[u[i]][v[i]], c[i]);
if(G[u[i]][v[i]] == c[i]) W[u[i]][v[i]] = i;
}
}
ll Solve(int idx){
swap(u[idx], v[idx]);
Build();
dis[1] = 0;
for(int i = 0; i < n; i++){
ll mn = Inf, id = -1;
for(int j = 1; j <= n; j++)
if(!mk[j] && dis[j] < mn){
mn = dis[j];
id = j;
}
if(id == -1) break;
mk[id] = 1;
for(int j = 1; j <= n; j++)
if(dis[j] > dis[id] + G[id][j]){
dis[j] = dis[id] + G[id][j];
}
}
swap(u[idx], v[idx]);
return dis[n];
}
void Main(){
memset(Gd, 0, sizeof Gd);
Build();
dis[1] = 0;
for(int i = 0; i < n; i++){
ll mn = Inf, id = -1;
for(int j = 1; j <= n; j++)
if(!mk[j] && dis[j] < mn){
mn = dis[j];
id = j;
}
if(id == -1) break;
mk[id] = 1;
Gd[par[id]] = 1;
for(int j = 1; j <= n; j++)
if(dis[j] > dis[id] + G[id][j]){
dis[j] = dis[id] + G[id][j];
par[j] = W[id][j];
}
}
//Build();
memset(mk, 0, sizeof mk);
dis2[n] = 0;
for(int i = 0; i < n; i++){
ll mn = Inf, id = -1;
for(int j = 1; j <= n; j++)
if(!mk[j] && dis2[j] < mn){
mn = dis2[j];
id = j;
}
if(id == -1) break;
mk[id] = 1;
Gd[par[id]] = 1;
for(int j = 1; j <= n; j++)
if(dis2[j] > dis2[id] + G[j][id]){
dis2[j] = dis2[id] + G[j][id];
par[j] = W[j][id];
}
}
for(int i = 0; i < m; i++){
if(!Gd[i]){
ans[i] += min(dis[n], dis[v[i]] + c[i] + dis2[u[i]]);
}
}
for(int i = 0; i < m; i++){
if(Gd[i]){
ans[i] += Solve(i);
//cerr << "?? " << i << '\n';
}
}
return ;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++) cin >> u[i] >> v[i] >> c[i] >> d[i];
ll Ans = Solve(m);
Main();
//for(int i = 0; i < m; i++) cout << ans[i] << ' ';
//cout << '\n';
for(int i = 0; i < m; i++){
if(u[i] == 1 || u[i] == n) u[i] = n + 1 - u[i];
if(v[i] == 1 || v[i] == n) v[i] = n + 1 - v[i];
}
Ans += Solve(m);
Main();
for(int i = 0; i < m; i++) Ans = min(Ans, d[i] + ans[i]);
//for(int i = 0; i < m; i++) cout << ans[i] << ' ';
//cout << '\n';
cout << (Ans < Inf ? Ans : -1) << '\n';
return 0;
}
/*
4 10
1 2 4 4
1 2 4 4
1 3 2 1
1 3 2 1
4 3 1 2
4 3 1 2
4 1 6 1
4 1 6 1
2 4 2 5
2 4 2 5
4 5
1 2 4 4
1 3 2 1
4 3 1 2
4 1 6 1
2 4 2 5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
1656 KB |
Output is correct |
2 |
Correct |
8 ms |
1528 KB |
Output is correct |
3 |
Correct |
116 ms |
1528 KB |
Output is correct |
4 |
Correct |
118 ms |
1528 KB |
Output is correct |
5 |
Correct |
9 ms |
1528 KB |
Output is correct |
6 |
Correct |
11 ms |
1528 KB |
Output is correct |
7 |
Correct |
7 ms |
1400 KB |
Output is correct |
8 |
Correct |
7 ms |
1400 KB |
Output is correct |
9 |
Correct |
6 ms |
1528 KB |
Output is correct |
10 |
Correct |
76 ms |
1528 KB |
Output is correct |
11 |
Correct |
78 ms |
1528 KB |
Output is correct |
12 |
Correct |
75 ms |
1400 KB |
Output is correct |
13 |
Correct |
41 ms |
1528 KB |
Output is correct |
14 |
Correct |
81 ms |
1528 KB |
Output is correct |
15 |
Correct |
86 ms |
1656 KB |
Output is correct |
16 |
Correct |
79 ms |
1528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
344 ms |
3448 KB |
Output is correct |
2 |
Correct |
351 ms |
3448 KB |
Output is correct |
3 |
Correct |
348 ms |
3320 KB |
Output is correct |
4 |
Correct |
124 ms |
1528 KB |
Output is correct |
5 |
Correct |
96 ms |
1656 KB |
Output is correct |
6 |
Correct |
12 ms |
1400 KB |
Output is correct |
7 |
Correct |
7 ms |
1400 KB |
Output is correct |
8 |
Correct |
7 ms |
1528 KB |
Output is correct |
9 |
Correct |
190 ms |
3320 KB |
Output is correct |
10 |
Correct |
192 ms |
3576 KB |
Output is correct |
11 |
Correct |
278 ms |
3320 KB |
Output is correct |
12 |
Correct |
279 ms |
3324 KB |
Output is correct |
13 |
Correct |
292 ms |
3448 KB |
Output is correct |
14 |
Correct |
278 ms |
3448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
1528 KB |
Output is correct |
2 |
Correct |
20 ms |
1400 KB |
Output is correct |
3 |
Correct |
418 ms |
3052 KB |
Output is correct |
4 |
Correct |
13 ms |
1400 KB |
Output is correct |
5 |
Correct |
517 ms |
3448 KB |
Output is correct |
6 |
Correct |
5 ms |
1400 KB |
Output is correct |
7 |
Correct |
5 ms |
1400 KB |
Output is correct |
8 |
Correct |
250 ms |
3448 KB |
Output is correct |
9 |
Correct |
263 ms |
3576 KB |
Output is correct |
10 |
Correct |
373 ms |
3448 KB |
Output is correct |
11 |
Correct |
353 ms |
3448 KB |
Output is correct |
12 |
Correct |
349 ms |
3448 KB |
Output is correct |
13 |
Correct |
5 ms |
1400 KB |
Output is correct |
14 |
Correct |
5 ms |
1400 KB |
Output is correct |
15 |
Correct |
6 ms |
1528 KB |
Output is correct |
16 |
Correct |
6 ms |
1656 KB |
Output is correct |
17 |
Correct |
5 ms |
1404 KB |
Output is correct |
18 |
Correct |
6 ms |
1400 KB |
Output is correct |
19 |
Correct |
357 ms |
3320 KB |
Output is correct |
20 |
Correct |
358 ms |
3580 KB |
Output is correct |
21 |
Correct |
351 ms |
3320 KB |
Output is correct |
22 |
Correct |
367 ms |
3320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
1656 KB |
Output is correct |
2 |
Correct |
8 ms |
1528 KB |
Output is correct |
3 |
Correct |
116 ms |
1528 KB |
Output is correct |
4 |
Correct |
118 ms |
1528 KB |
Output is correct |
5 |
Correct |
9 ms |
1528 KB |
Output is correct |
6 |
Correct |
11 ms |
1528 KB |
Output is correct |
7 |
Correct |
7 ms |
1400 KB |
Output is correct |
8 |
Correct |
7 ms |
1400 KB |
Output is correct |
9 |
Correct |
6 ms |
1528 KB |
Output is correct |
10 |
Correct |
76 ms |
1528 KB |
Output is correct |
11 |
Correct |
78 ms |
1528 KB |
Output is correct |
12 |
Correct |
75 ms |
1400 KB |
Output is correct |
13 |
Correct |
41 ms |
1528 KB |
Output is correct |
14 |
Correct |
81 ms |
1528 KB |
Output is correct |
15 |
Correct |
86 ms |
1656 KB |
Output is correct |
16 |
Correct |
79 ms |
1528 KB |
Output is correct |
17 |
Correct |
344 ms |
3448 KB |
Output is correct |
18 |
Correct |
351 ms |
3448 KB |
Output is correct |
19 |
Correct |
348 ms |
3320 KB |
Output is correct |
20 |
Correct |
124 ms |
1528 KB |
Output is correct |
21 |
Correct |
96 ms |
1656 KB |
Output is correct |
22 |
Correct |
12 ms |
1400 KB |
Output is correct |
23 |
Correct |
7 ms |
1400 KB |
Output is correct |
24 |
Correct |
7 ms |
1528 KB |
Output is correct |
25 |
Correct |
190 ms |
3320 KB |
Output is correct |
26 |
Correct |
192 ms |
3576 KB |
Output is correct |
27 |
Correct |
278 ms |
3320 KB |
Output is correct |
28 |
Correct |
279 ms |
3324 KB |
Output is correct |
29 |
Correct |
292 ms |
3448 KB |
Output is correct |
30 |
Correct |
278 ms |
3448 KB |
Output is correct |
31 |
Correct |
99 ms |
1528 KB |
Output is correct |
32 |
Correct |
20 ms |
1400 KB |
Output is correct |
33 |
Correct |
418 ms |
3052 KB |
Output is correct |
34 |
Correct |
13 ms |
1400 KB |
Output is correct |
35 |
Correct |
517 ms |
3448 KB |
Output is correct |
36 |
Correct |
5 ms |
1400 KB |
Output is correct |
37 |
Correct |
5 ms |
1400 KB |
Output is correct |
38 |
Correct |
250 ms |
3448 KB |
Output is correct |
39 |
Correct |
263 ms |
3576 KB |
Output is correct |
40 |
Correct |
373 ms |
3448 KB |
Output is correct |
41 |
Correct |
353 ms |
3448 KB |
Output is correct |
42 |
Correct |
349 ms |
3448 KB |
Output is correct |
43 |
Correct |
5 ms |
1400 KB |
Output is correct |
44 |
Correct |
5 ms |
1400 KB |
Output is correct |
45 |
Correct |
6 ms |
1528 KB |
Output is correct |
46 |
Correct |
6 ms |
1656 KB |
Output is correct |
47 |
Correct |
5 ms |
1404 KB |
Output is correct |
48 |
Correct |
6 ms |
1400 KB |
Output is correct |
49 |
Correct |
357 ms |
3320 KB |
Output is correct |
50 |
Correct |
358 ms |
3580 KB |
Output is correct |
51 |
Correct |
351 ms |
3320 KB |
Output is correct |
52 |
Correct |
367 ms |
3320 KB |
Output is correct |
53 |
Correct |
419 ms |
3448 KB |
Output is correct |
54 |
Correct |
435 ms |
3552 KB |
Output is correct |
55 |
Correct |
425 ms |
3320 KB |
Output is correct |
56 |
Correct |
120 ms |
1656 KB |
Output is correct |
57 |
Correct |
122 ms |
1528 KB |
Output is correct |
58 |
Correct |
222 ms |
3064 KB |
Output is correct |
59 |
Correct |
227 ms |
3064 KB |
Output is correct |
60 |
Correct |
217 ms |
3064 KB |
Output is correct |
61 |
Correct |
232 ms |
2936 KB |
Output is correct |
62 |
Correct |
222 ms |
3064 KB |
Output is correct |
63 |
Correct |
214 ms |
2936 KB |
Output is correct |
64 |
Correct |
237 ms |
2936 KB |
Output is correct |
65 |
Correct |
227 ms |
2936 KB |
Output is correct |
66 |
Correct |
221 ms |
3064 KB |
Output is correct |
67 |
Correct |
23 ms |
2936 KB |
Output is correct |
68 |
Correct |
235 ms |
3324 KB |
Output is correct |
69 |
Correct |
238 ms |
3448 KB |
Output is correct |
70 |
Correct |
340 ms |
3320 KB |
Output is correct |
71 |
Correct |
341 ms |
3576 KB |
Output is correct |
72 |
Correct |
337 ms |
3448 KB |
Output is correct |
73 |
Correct |
342 ms |
3448 KB |
Output is correct |
74 |
Correct |
338 ms |
3320 KB |
Output is correct |