#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#define ll long long
#define ld long double
#define eps 1e-8
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFLL 0x003f3f3f3f3f3f3f
// change if necessary
#define MAXN 210
using namespace std;
int n, m;
vector<array<int, 3>> adj[MAXN];
vector<array<int, 3>> radj[MAXN];
ll adjm[MAXN][MAXN];
ll radjm[MAXN][MAXN];
ll ans = INFLL;
ll dist_1[MAXN];
ll rdist_1[MAXN];
ll dist_n[MAXN];
ll rdist_n[MAXN];
bool vis[MAXN];
void solve(int rem) {
for (int i = 0; i < n; i++) {
dist_1[i] = INFLL;
dist_n[i] = INFLL;
rdist_1[i] = INFLL;
rdist_n[i] = INFLL;
}
memset(vis, 0, sizeof vis);
int e = 0;
dist_1[0] = 0;
while (e != -1) {
ll d = dist_1[e];
vis[e] = true;
if (e != rem) {
for (int u = 0; u < n; u++) {
if (!vis[u] && d + adjm[e][u] < dist_1[u]) {
dist_1[u] = d + adjm[e][u];
}
}
}
e = -1;
ll min_d = INFLL;
for (int i = 0; i < n; i++) {
if (!vis[i] && dist_1[i] < min_d) {
e = i;
min_d = dist_1[i];
}
}
}
memset(vis, 0, sizeof vis);
e = n - 1;
rdist_n[n - 1] = 0;
while (e != -1) {
ll d = rdist_n[e];
vis[e] = true;
for (int u = 0; u < n; u++) {
if (!vis[u] && d + radjm[e][u] < rdist_n[u] && u != rem) {
rdist_n[u] = d + radjm[e][u];
}
}
e = -1;
ll min_d = INFLL;
for (int i = 0; i < n; i++) {
if (!vis[i] && rdist_n[i] < min_d) {
e = i;
min_d = rdist_n[i];
}
}
}
memset(vis, 0, sizeof vis);
e = n - 1;
dist_n[n - 1] = 0;
while (e != -1) {
int d = dist_n[e];
vis[e] = true;
if (e != rem) {
for (int u = 0; u < n; u++) {
if (!vis[u] && d + adjm[e][u] < dist_n[u]) {
dist_n[u] = d + adjm[e][u];
}
}
}
e = -1;
ll min_d = INFLL;
for (int i = 0; i < n; i++) {
if (!vis[i] && dist_n[i] < min_d) {
e = i;
min_d = dist_n[i];
}
}
}
memset(vis, 0, sizeof vis);
e = 0;
rdist_1[0] = 0;
while (e != -1) {
ll d = rdist_1[e];
vis[e] = true;
for (int u = 0; u < n; u++) {
if (!vis[u] && d + radjm[e][u] < rdist_1[u] && u != rem) {
rdist_1[u] = d + radjm[e][u];
}
}
e = -1;
ll min_d = INFLL;
for (int i = 0; i < n; i++) {
if (!vis[i] && rdist_1[i] < min_d) {
e = i;
min_d = rdist_1[i];
}
}
}
ans = min(ans, dist_1[n - 1] + dist_n[0]);
if (rem == n || adj[rem].empty()) return;
// path without flipping
pair<ll, ll> max_to_n = {dist_1[n - 1], dist_1[n - 1]};
pair<ll, ll> max_to_1 = {dist_n[0], dist_n[0]};
pair<ll, ll> r_max_to_1 = {rdist_1[rem], rdist_1[rem]};
pair<ll, ll> r_max_to_n = {rdist_n[rem], rdist_n[rem]};
for (const auto &e : adj[rem]) {
ll to_n = dist_1[rem] + e[1] + rdist_n[e[0]];
ll to_1 = dist_n[rem] + e[1] + rdist_1[e[0]];
if (to_n <= max_to_n.first) {
max_to_n.second = max_to_n.first;
max_to_n.first = to_n;
} else if (to_n < max_to_n.second) {
max_to_n.second = to_n;
}
if (to_1 <= max_to_1.first) {
max_to_1.second = max_to_1.first;
max_to_1.first = to_1;
} else if (to_1 < max_to_1.second) {
max_to_1.second = to_1;
}
// no flip best prev path
ll r_to_n = e[1] + rdist_n[e[0]];
ll r_to_1 = e[1] + rdist_1[e[0]];
if (r_to_n <= r_max_to_n.first) {
r_max_to_n.second = r_max_to_n.first;
r_max_to_n.first = r_to_n;
} else if (r_to_n < r_max_to_n.second) {
r_max_to_n.second = r_to_n;
}
if (r_to_1 <= r_max_to_1.first) {
r_max_to_1.second = r_max_to_1.first;
r_max_to_1.first = r_to_1;
} else if (r_to_1 < r_max_to_1.second) {
r_max_to_1.second = r_to_1;
}
}
// consider flipping this
for (const auto &e : adj[rem]) {
ll to_n = dist_1[rem] + e[1] + rdist_n[e[0]];
ll r_to_n = e[1] + rdist_n[e[0]];
if (to_n == max_to_n.first) {
to_n = max_to_n.second;
} else {
to_n = max_to_n.first;
}
if (r_to_n == r_max_to_n.first) {
r_to_n = r_max_to_n.second;
} else {
r_to_n = r_max_to_n.first;
}
to_n = min(to_n, dist_1[e[0]] + e[1] + r_to_n);
ll to_1 = dist_n[rem] + e[1] + rdist_1[e[0]];
ll r_to_1 = e[1] + rdist_1[e[0]];
if (to_1 == max_to_1.first) {
to_1 = max_to_1.second;
} else {
to_1 = max_to_1.first;
}
if (r_to_1 == r_max_to_1.first) {
r_to_1 = r_max_to_1.second;
} else {
r_to_1 = r_max_to_1.first;
}
to_1 = min(to_1, dist_n[e[0]] + e[1] + r_to_1);
ans = min(ans, to_n + to_1 + e[2]);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
adjm[i][j] = INFLL;
radjm[i][j] = INFLL;
}
}
for (int i = 0; i < m; i++) {
int u, v, c, d; cin >> u >> v >> c >> d;
u--; v--;
adj[u].push_back({v, c, d});
radj[v].push_back({u, c, d});
adjm[u][v] = min(adjm[u][v], (ll)c);
radjm[v][u] = min(radjm[v][u], (ll)c);
}
for (int i = 0; i <= n; i++) {
solve(i);
}
if (ans == INFLL) {
cout << "-1\n";
} else {
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
1024 KB |
Output is correct |
2 |
Correct |
15 ms |
1024 KB |
Output is correct |
3 |
Correct |
176 ms |
1144 KB |
Output is correct |
4 |
Correct |
180 ms |
1024 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
32 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
179 ms |
1108 KB |
Output is correct |
11 |
Correct |
181 ms |
1024 KB |
Output is correct |
12 |
Correct |
178 ms |
1144 KB |
Output is correct |
13 |
Correct |
77 ms |
1024 KB |
Output is correct |
14 |
Correct |
129 ms |
1024 KB |
Output is correct |
15 |
Correct |
127 ms |
1152 KB |
Output is correct |
16 |
Correct |
131 ms |
1144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
206 ms |
2944 KB |
Output is correct |
2 |
Correct |
207 ms |
3016 KB |
Output is correct |
3 |
Correct |
207 ms |
3064 KB |
Output is correct |
4 |
Correct |
176 ms |
1152 KB |
Output is correct |
5 |
Correct |
163 ms |
1024 KB |
Output is correct |
6 |
Correct |
57 ms |
1024 KB |
Output is correct |
7 |
Correct |
6 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
105 ms |
2936 KB |
Output is correct |
10 |
Correct |
106 ms |
2816 KB |
Output is correct |
11 |
Correct |
164 ms |
3064 KB |
Output is correct |
12 |
Correct |
159 ms |
2936 KB |
Output is correct |
13 |
Correct |
157 ms |
2944 KB |
Output is correct |
14 |
Correct |
158 ms |
3200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
1144 KB |
Output is correct |
2 |
Correct |
55 ms |
1024 KB |
Output is correct |
3 |
Correct |
97 ms |
2304 KB |
Output is correct |
4 |
Correct |
36 ms |
1024 KB |
Output is correct |
5 |
Correct |
101 ms |
2816 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
79 ms |
2944 KB |
Output is correct |
9 |
Correct |
76 ms |
2944 KB |
Output is correct |
10 |
Correct |
105 ms |
3064 KB |
Output is correct |
11 |
Correct |
92 ms |
2936 KB |
Output is correct |
12 |
Correct |
87 ms |
3072 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
99 ms |
3064 KB |
Output is correct |
20 |
Correct |
97 ms |
2944 KB |
Output is correct |
21 |
Correct |
88 ms |
2848 KB |
Output is correct |
22 |
Correct |
94 ms |
2936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
1024 KB |
Output is correct |
2 |
Correct |
15 ms |
1024 KB |
Output is correct |
3 |
Correct |
176 ms |
1144 KB |
Output is correct |
4 |
Correct |
180 ms |
1024 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
32 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
179 ms |
1108 KB |
Output is correct |
11 |
Correct |
181 ms |
1024 KB |
Output is correct |
12 |
Correct |
178 ms |
1144 KB |
Output is correct |
13 |
Correct |
77 ms |
1024 KB |
Output is correct |
14 |
Correct |
129 ms |
1024 KB |
Output is correct |
15 |
Correct |
127 ms |
1152 KB |
Output is correct |
16 |
Correct |
131 ms |
1144 KB |
Output is correct |
17 |
Correct |
206 ms |
2944 KB |
Output is correct |
18 |
Correct |
207 ms |
3016 KB |
Output is correct |
19 |
Correct |
207 ms |
3064 KB |
Output is correct |
20 |
Correct |
176 ms |
1152 KB |
Output is correct |
21 |
Correct |
163 ms |
1024 KB |
Output is correct |
22 |
Correct |
57 ms |
1024 KB |
Output is correct |
23 |
Correct |
6 ms |
1024 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
105 ms |
2936 KB |
Output is correct |
26 |
Correct |
106 ms |
2816 KB |
Output is correct |
27 |
Correct |
164 ms |
3064 KB |
Output is correct |
28 |
Correct |
159 ms |
2936 KB |
Output is correct |
29 |
Correct |
157 ms |
2944 KB |
Output is correct |
30 |
Correct |
158 ms |
3200 KB |
Output is correct |
31 |
Correct |
107 ms |
1144 KB |
Output is correct |
32 |
Correct |
55 ms |
1024 KB |
Output is correct |
33 |
Correct |
97 ms |
2304 KB |
Output is correct |
34 |
Correct |
36 ms |
1024 KB |
Output is correct |
35 |
Correct |
101 ms |
2816 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
0 ms |
384 KB |
Output is correct |
38 |
Correct |
79 ms |
2944 KB |
Output is correct |
39 |
Correct |
76 ms |
2944 KB |
Output is correct |
40 |
Correct |
105 ms |
3064 KB |
Output is correct |
41 |
Correct |
92 ms |
2936 KB |
Output is correct |
42 |
Correct |
87 ms |
3072 KB |
Output is correct |
43 |
Correct |
0 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
0 ms |
384 KB |
Output is correct |
46 |
Correct |
0 ms |
384 KB |
Output is correct |
47 |
Correct |
0 ms |
384 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
99 ms |
3064 KB |
Output is correct |
50 |
Correct |
97 ms |
2944 KB |
Output is correct |
51 |
Correct |
88 ms |
2848 KB |
Output is correct |
52 |
Correct |
94 ms |
2936 KB |
Output is correct |
53 |
Correct |
203 ms |
2816 KB |
Output is correct |
54 |
Correct |
205 ms |
2944 KB |
Output is correct |
55 |
Correct |
207 ms |
3064 KB |
Output is correct |
56 |
Correct |
176 ms |
1024 KB |
Output is correct |
57 |
Correct |
176 ms |
1024 KB |
Output is correct |
58 |
Correct |
203 ms |
2424 KB |
Output is correct |
59 |
Correct |
207 ms |
2304 KB |
Output is correct |
60 |
Correct |
204 ms |
2392 KB |
Output is correct |
61 |
Correct |
205 ms |
2428 KB |
Output is correct |
62 |
Correct |
204 ms |
2304 KB |
Output is correct |
63 |
Correct |
202 ms |
2304 KB |
Output is correct |
64 |
Correct |
201 ms |
2680 KB |
Output is correct |
65 |
Correct |
180 ms |
3328 KB |
Output is correct |
66 |
Correct |
156 ms |
3328 KB |
Output is correct |
67 |
Correct |
18 ms |
2748 KB |
Output is correct |
68 |
Correct |
111 ms |
4096 KB |
Output is correct |
69 |
Correct |
110 ms |
4088 KB |
Output is correct |
70 |
Correct |
160 ms |
4088 KB |
Output is correct |
71 |
Correct |
160 ms |
3968 KB |
Output is correct |
72 |
Correct |
161 ms |
4092 KB |
Output is correct |
73 |
Correct |
157 ms |
4224 KB |
Output is correct |
74 |
Correct |
157 ms |
4088 KB |
Output is correct |