#include<bits/stdc++.h>
#define ll long long
#define int long long
const int nmax = 200 + 5, N = 3e5 + 5;
const ll oo = 1e18;
const int lg = 7, M = 4e3;
const int base = 2e5, mod = 998244353;
#define pii pair<ll, ll>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';cout << endl
using namespace std;
multiset<pii> adj[nmax];
struct node{
int u, v, c, d;
}a[N];
ll f[nmax][nmax];
int n, m;
priority_queue<pii, vector<pii>, greater<pii>> q;
ll dp[nmax];
ll dist(int x){
for(int i = 1; i <= n; ++i) dp[i] = oo;
q.push({0, x});
dp[x] =0;
while(q.size()){
pii tmp = q.top();q.pop();
int u = tmp.se;
ll val = tmp.fi;
if(val != dp[u]) continue;
for(auto [v, w] : adj[u]){
if(dp[v] > dp[u] + w){
dp[v] = dp[u] + w;
q.push({dp[v], v});
}
}
}
if(x == 1) return dp[n];
return dp[1];
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> n >> m;
memset(f, 0x3f, sizeof f);
for(int i = 1; i <= n; ++i) f[i][i] =0;
for(int i = 1; i <= m; ++i){
int u, v, c, d;
cin >> u >> v >> c >> d;
a[i] = {u, v, c, d};
adj[u].insert({v, c});
f[u][v] = min(f[u][v], 1ll*c);
}
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]);
}
}
}
// for(int i = 1; i <= n; ++i){
// for(int j = 1; j <= n; ++j){
// cout << f[i][j] << ' ';
// }
// cout << endl;
//
// }
ll ans = f[1][n] + f[n][1];
for(int i = 1; i <= m; ++i){
int u = a[i].v, v = a[i].u;
ll cur =min(f[1][n], f[1][u] + f[v][n] + a[i].c)+ min(f[n][1], f[n][u] + f[v][1] + a[i].c) + a[i].d;
if(cur >= ans) continue;
swap(u, v);
adj[u].erase(adj[u].find({v, a[i].c}));
adj[v].insert({u, a[i].c});
ans = min(ans, dist(1) + dist(n) + a[i].d);
swap(u, v);
adj[u].erase(adj[u].find({v, a[i].c}));
adj[v].insert({u, a[i].c});
}
if(ans >= oo / 2) cout << -1;
else cout << ans;
}
/*
4 5
3 1 3 7
3 2 0 4
1 2 5 1
4 2 4 9
3 4 3 9
*/
Compilation message
ho_t4.cpp:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
44 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2648 KB |
Output is correct |
2 |
Correct |
7 ms |
2652 KB |
Output is correct |
3 |
Correct |
7 ms |
2796 KB |
Output is correct |
4 |
Correct |
13 ms |
2792 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
10 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
10 ms |
2652 KB |
Output is correct |
11 |
Correct |
17 ms |
2780 KB |
Output is correct |
12 |
Correct |
8 ms |
2652 KB |
Output is correct |
13 |
Correct |
7 ms |
2652 KB |
Output is correct |
14 |
Correct |
7 ms |
2652 KB |
Output is correct |
15 |
Correct |
7 ms |
2652 KB |
Output is correct |
16 |
Correct |
7 ms |
2788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
6788 KB |
Output is correct |
2 |
Correct |
28 ms |
6748 KB |
Output is correct |
3 |
Correct |
25 ms |
6940 KB |
Output is correct |
4 |
Correct |
13 ms |
2648 KB |
Output is correct |
5 |
Correct |
7 ms |
2788 KB |
Output is correct |
6 |
Correct |
10 ms |
2648 KB |
Output is correct |
7 |
Correct |
7 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
46 ms |
6916 KB |
Output is correct |
10 |
Correct |
37 ms |
6736 KB |
Output is correct |
11 |
Correct |
28 ms |
6796 KB |
Output is correct |
12 |
Correct |
29 ms |
6748 KB |
Output is correct |
13 |
Correct |
26 ms |
6748 KB |
Output is correct |
14 |
Correct |
28 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2776 KB |
Output is correct |
2 |
Correct |
7 ms |
2724 KB |
Output is correct |
3 |
Correct |
22 ms |
5640 KB |
Output is correct |
4 |
Correct |
7 ms |
2652 KB |
Output is correct |
5 |
Correct |
26 ms |
6712 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2652 KB |
Output is correct |
8 |
Correct |
53 ms |
6752 KB |
Output is correct |
9 |
Correct |
65 ms |
6748 KB |
Output is correct |
10 |
Correct |
28 ms |
6480 KB |
Output is correct |
11 |
Correct |
27 ms |
6736 KB |
Output is correct |
12 |
Correct |
27 ms |
6736 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
0 ms |
2652 KB |
Output is correct |
18 |
Correct |
0 ms |
2652 KB |
Output is correct |
19 |
Correct |
30 ms |
6704 KB |
Output is correct |
20 |
Correct |
35 ms |
6720 KB |
Output is correct |
21 |
Correct |
29 ms |
6748 KB |
Output is correct |
22 |
Correct |
28 ms |
6616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2648 KB |
Output is correct |
2 |
Correct |
7 ms |
2652 KB |
Output is correct |
3 |
Correct |
7 ms |
2796 KB |
Output is correct |
4 |
Correct |
13 ms |
2792 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
10 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
10 ms |
2652 KB |
Output is correct |
11 |
Correct |
17 ms |
2780 KB |
Output is correct |
12 |
Correct |
8 ms |
2652 KB |
Output is correct |
13 |
Correct |
7 ms |
2652 KB |
Output is correct |
14 |
Correct |
7 ms |
2652 KB |
Output is correct |
15 |
Correct |
7 ms |
2652 KB |
Output is correct |
16 |
Correct |
7 ms |
2788 KB |
Output is correct |
17 |
Correct |
25 ms |
6788 KB |
Output is correct |
18 |
Correct |
28 ms |
6748 KB |
Output is correct |
19 |
Correct |
25 ms |
6940 KB |
Output is correct |
20 |
Correct |
13 ms |
2648 KB |
Output is correct |
21 |
Correct |
7 ms |
2788 KB |
Output is correct |
22 |
Correct |
10 ms |
2648 KB |
Output is correct |
23 |
Correct |
7 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
46 ms |
6916 KB |
Output is correct |
26 |
Correct |
37 ms |
6736 KB |
Output is correct |
27 |
Correct |
28 ms |
6796 KB |
Output is correct |
28 |
Correct |
29 ms |
6748 KB |
Output is correct |
29 |
Correct |
26 ms |
6748 KB |
Output is correct |
30 |
Correct |
28 ms |
6748 KB |
Output is correct |
31 |
Correct |
7 ms |
2776 KB |
Output is correct |
32 |
Correct |
7 ms |
2724 KB |
Output is correct |
33 |
Correct |
22 ms |
5640 KB |
Output is correct |
34 |
Correct |
7 ms |
2652 KB |
Output is correct |
35 |
Correct |
26 ms |
6712 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
0 ms |
2652 KB |
Output is correct |
38 |
Correct |
53 ms |
6752 KB |
Output is correct |
39 |
Correct |
65 ms |
6748 KB |
Output is correct |
40 |
Correct |
28 ms |
6480 KB |
Output is correct |
41 |
Correct |
27 ms |
6736 KB |
Output is correct |
42 |
Correct |
27 ms |
6736 KB |
Output is correct |
43 |
Correct |
1 ms |
2652 KB |
Output is correct |
44 |
Correct |
1 ms |
2652 KB |
Output is correct |
45 |
Correct |
1 ms |
2652 KB |
Output is correct |
46 |
Correct |
1 ms |
2652 KB |
Output is correct |
47 |
Correct |
0 ms |
2652 KB |
Output is correct |
48 |
Correct |
0 ms |
2652 KB |
Output is correct |
49 |
Correct |
30 ms |
6704 KB |
Output is correct |
50 |
Correct |
35 ms |
6720 KB |
Output is correct |
51 |
Correct |
29 ms |
6748 KB |
Output is correct |
52 |
Correct |
28 ms |
6616 KB |
Output is correct |
53 |
Correct |
29 ms |
6700 KB |
Output is correct |
54 |
Correct |
27 ms |
6668 KB |
Output is correct |
55 |
Correct |
27 ms |
6696 KB |
Output is correct |
56 |
Correct |
7 ms |
2652 KB |
Output is correct |
57 |
Correct |
7 ms |
2772 KB |
Output is correct |
58 |
Correct |
35 ms |
6024 KB |
Output is correct |
59 |
Correct |
31 ms |
5976 KB |
Output is correct |
60 |
Correct |
34 ms |
5968 KB |
Output is correct |
61 |
Correct |
39 ms |
5932 KB |
Output is correct |
62 |
Correct |
167 ms |
5928 KB |
Output is correct |
63 |
Correct |
349 ms |
6032 KB |
Output is correct |
64 |
Correct |
93 ms |
6224 KB |
Output is correct |
65 |
Correct |
205 ms |
6268 KB |
Output is correct |
66 |
Correct |
402 ms |
6228 KB |
Output is correct |
67 |
Correct |
36 ms |
5864 KB |
Output is correct |
68 |
Correct |
48 ms |
6748 KB |
Output is correct |
69 |
Correct |
44 ms |
6992 KB |
Output is correct |
70 |
Correct |
30 ms |
6928 KB |
Output is correct |
71 |
Correct |
32 ms |
6736 KB |
Output is correct |
72 |
Correct |
38 ms |
6744 KB |
Output is correct |
73 |
Correct |
29 ms |
6748 KB |
Output is correct |
74 |
Correct |
31 ms |
6856 KB |
Output is correct |