/*
author : abushbandit
contest : ---
*/
#pragma GCC optimize("Ofast,unroll-loops")
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int INF = 1e15,N = 500;
int dis[N + 1];
int n;
int f[N + 1][N + 1];
multiset<pair<int,int>> g[N + 1];
int dist(int x,int tar){
set<pair<int,int>> q;
for(int i = 1;i <= n;i++) dis[i] = INF;
q.insert({0, x});
dis[x] = 0;
while(!q.empty()){
auto [val,u] = *q.begin();
q.erase(q.begin());
if(val > dis[u]) continue;
for(auto [v, w] : g[u]){
if(dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
q.insert({dis[v], v});
}
}
}
return dis[tar];
}
void solve() {
int m;
cin >> n >> m;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
f[i][j] = INF;
f[i][i] = 0;
}
}
int cnt = 0;
int a[m],b[m],c[m],d[m];
for(int i = 0;i < m;i++){
cin >> a[i] >> b[i] >> c[i] >> d[i];
cnt += c[i] == 0;
f[a[i]][b[i]] = min(c[i],f[a[i]][b[i]]);
g[a[i]].insert({b[i],c[i]});
}
for(int k = 1; k <= n; ++k){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
}
}
}
int ans = f[1][n] + f[n][1];
for(int i = 0;i < m;i++){
int cur = min(f[1][n], f[1][b[i]] + f[a[i]][n] + c[i]) + min(f[n][1], f[n][b[i]] + f[a[i]][1] + c[i]) + d[i];
if(cur > ans){
continue;
}
cur = d[i];
g[a[i]].erase(g[a[i]].find({b[i],c[i]}));
g[b[i]].insert({a[i],c[i]});
cur += dist(1,n) + dist(n,1);
ans = min(ans,cur);
g[a[i]].insert({b[i],c[i]});
g[b[i]].erase(g[b[i]].find({a[i],c[i]}));
}
if(ans >= INF){
ans = -1;
}
cout << ans << "\n";
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int t = 1;
//~ cin >> t;
while(t--){
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1624 KB |
Output is correct |
2 |
Correct |
7 ms |
1116 KB |
Output is correct |
3 |
Correct |
7 ms |
1372 KB |
Output is correct |
4 |
Correct |
8 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
7 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
10 ms |
1368 KB |
Output is correct |
11 |
Correct |
17 ms |
1268 KB |
Output is correct |
12 |
Correct |
8 ms |
1372 KB |
Output is correct |
13 |
Correct |
7 ms |
1372 KB |
Output is correct |
14 |
Correct |
7 ms |
1396 KB |
Output is correct |
15 |
Correct |
7 ms |
1372 KB |
Output is correct |
16 |
Correct |
8 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
6748 KB |
Output is correct |
2 |
Correct |
29 ms |
6736 KB |
Output is correct |
3 |
Correct |
26 ms |
7000 KB |
Output is correct |
4 |
Correct |
8 ms |
1372 KB |
Output is correct |
5 |
Correct |
7 ms |
1372 KB |
Output is correct |
6 |
Correct |
7 ms |
1112 KB |
Output is correct |
7 |
Correct |
7 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
46 ms |
7004 KB |
Output is correct |
10 |
Correct |
40 ms |
7000 KB |
Output is correct |
11 |
Correct |
31 ms |
7120 KB |
Output is correct |
12 |
Correct |
29 ms |
7004 KB |
Output is correct |
13 |
Correct |
28 ms |
6996 KB |
Output is correct |
14 |
Correct |
27 ms |
7000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1368 KB |
Output is correct |
2 |
Correct |
7 ms |
1116 KB |
Output is correct |
3 |
Correct |
22 ms |
5464 KB |
Output is correct |
4 |
Correct |
7 ms |
1116 KB |
Output is correct |
5 |
Correct |
28 ms |
6820 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
54 ms |
6884 KB |
Output is correct |
9 |
Correct |
64 ms |
6744 KB |
Output is correct |
10 |
Correct |
28 ms |
6748 KB |
Output is correct |
11 |
Correct |
29 ms |
6748 KB |
Output is correct |
12 |
Correct |
29 ms |
6888 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 |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
29 ms |
6744 KB |
Output is correct |
20 |
Correct |
31 ms |
6748 KB |
Output is correct |
21 |
Correct |
35 ms |
6736 KB |
Output is correct |
22 |
Correct |
30 ms |
6736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1624 KB |
Output is correct |
2 |
Correct |
7 ms |
1116 KB |
Output is correct |
3 |
Correct |
7 ms |
1372 KB |
Output is correct |
4 |
Correct |
8 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
7 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
10 ms |
1368 KB |
Output is correct |
11 |
Correct |
17 ms |
1268 KB |
Output is correct |
12 |
Correct |
8 ms |
1372 KB |
Output is correct |
13 |
Correct |
7 ms |
1372 KB |
Output is correct |
14 |
Correct |
7 ms |
1396 KB |
Output is correct |
15 |
Correct |
7 ms |
1372 KB |
Output is correct |
16 |
Correct |
8 ms |
1372 KB |
Output is correct |
17 |
Correct |
25 ms |
6748 KB |
Output is correct |
18 |
Correct |
29 ms |
6736 KB |
Output is correct |
19 |
Correct |
26 ms |
7000 KB |
Output is correct |
20 |
Correct |
8 ms |
1372 KB |
Output is correct |
21 |
Correct |
7 ms |
1372 KB |
Output is correct |
22 |
Correct |
7 ms |
1112 KB |
Output is correct |
23 |
Correct |
7 ms |
1116 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
46 ms |
7004 KB |
Output is correct |
26 |
Correct |
40 ms |
7000 KB |
Output is correct |
27 |
Correct |
31 ms |
7120 KB |
Output is correct |
28 |
Correct |
29 ms |
7004 KB |
Output is correct |
29 |
Correct |
28 ms |
6996 KB |
Output is correct |
30 |
Correct |
27 ms |
7000 KB |
Output is correct |
31 |
Correct |
7 ms |
1368 KB |
Output is correct |
32 |
Correct |
7 ms |
1116 KB |
Output is correct |
33 |
Correct |
22 ms |
5464 KB |
Output is correct |
34 |
Correct |
7 ms |
1116 KB |
Output is correct |
35 |
Correct |
28 ms |
6820 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
54 ms |
6884 KB |
Output is correct |
39 |
Correct |
64 ms |
6744 KB |
Output is correct |
40 |
Correct |
28 ms |
6748 KB |
Output is correct |
41 |
Correct |
29 ms |
6748 KB |
Output is correct |
42 |
Correct |
29 ms |
6888 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 |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
29 ms |
6744 KB |
Output is correct |
50 |
Correct |
31 ms |
6748 KB |
Output is correct |
51 |
Correct |
35 ms |
6736 KB |
Output is correct |
52 |
Correct |
30 ms |
6736 KB |
Output is correct |
53 |
Correct |
29 ms |
6744 KB |
Output is correct |
54 |
Correct |
27 ms |
6736 KB |
Output is correct |
55 |
Correct |
28 ms |
7084 KB |
Output is correct |
56 |
Correct |
7 ms |
1368 KB |
Output is correct |
57 |
Correct |
7 ms |
1372 KB |
Output is correct |
58 |
Correct |
33 ms |
5880 KB |
Output is correct |
59 |
Correct |
32 ms |
5760 KB |
Output is correct |
60 |
Correct |
44 ms |
5724 KB |
Output is correct |
61 |
Correct |
57 ms |
5720 KB |
Output is correct |
62 |
Correct |
167 ms |
5880 KB |
Output is correct |
63 |
Correct |
389 ms |
5724 KB |
Output is correct |
64 |
Correct |
113 ms |
6232 KB |
Output is correct |
65 |
Correct |
427 ms |
6460 KB |
Output is correct |
66 |
Correct |
914 ms |
6432 KB |
Output is correct |
67 |
Correct |
42 ms |
4956 KB |
Output is correct |
68 |
Correct |
50 ms |
7004 KB |
Output is correct |
69 |
Correct |
41 ms |
7108 KB |
Output is correct |
70 |
Correct |
30 ms |
7004 KB |
Output is correct |
71 |
Correct |
33 ms |
7004 KB |
Output is correct |
72 |
Correct |
31 ms |
7000 KB |
Output is correct |
73 |
Correct |
29 ms |
7024 KB |
Output is correct |
74 |
Correct |
32 ms |
7004 KB |
Output is correct |