#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1 << 9, M = 1 << 16;
const ll inf = 1e18;
int n;
ll apsp[N][N], sssp[N];
int a[M], b[M], w[M], c[M];
vector<int> adj[N];
bool vis[N];
ll dijkstra(int s, int t) {
fill(sssp, sssp + n + 1, inf);
fill(vis, vis + n, false);
sssp[s] = 0;
for (int i = 0; i < n; ++i) {
int u = n;
for (int v = 0; v < n; ++v) if (!vis[v] && sssp[v] < sssp[u]) u = v;
vis[u] = true;
if (sssp[u] == inf) break;
for (int e : adj[u]) sssp[b[e]] = min(sssp[b[e]], sssp[u] + w[e]);
}
return sssp[t];
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int m;
cin >> n >> m;
for (int u = 0; u < n; ++u) for (int v = 0; v < n; ++v) apsp[u][v] = inf;
for (int u = 0; u < n; ++u) apsp[u][u] = 0;
for (int e = 0; e < m; ++e) {
cin >> a[e] >> b[e] >> w[e] >> c[e];
--a[e];
--b[e];
apsp[a[e]][b[e]] = min(apsp[a[e]][b[e]], (ll)w[e]);
adj[a[e]].push_back(e);
}
for (int w = 0; w < n; ++w) for (int u = 0; u < n; ++u) for (int v = 0; v < n; ++v) apsp[u][v] = min(apsp[u][v], apsp[u][w] + apsp[w][v]);
int s = 0, t = n - 1;
ll ans = apsp[s][t] + apsp[t][s];
for (int e = 0; e < m; ++e) {
ll to = min(apsp[s][t], apsp[s][b[e]] + w[e] + apsp[a[e]][t]),
fr = min(apsp[t][s], apsp[t][b[e]] + w[e] + apsp[a[e]][s]);
if (c[e] + to + fr < ans) {
adj[a[e]].erase(find(adj[a[e]].begin(), adj[a[e]].end(), e));
swap(a[e], b[e]);
adj[a[e]].push_back(e);
ans = min(ans, c[e] + dijkstra(s, t) + dijkstra(t, s));
adj[a[e]].pop_back();
swap(a[e], b[e]);
adj[a[e]].push_back(e);
}
}
cout << (ans < inf ? ans : -1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
2384 KB |
Output is correct |
2 |
Correct |
8 ms |
2384 KB |
Output is correct |
3 |
Correct |
8 ms |
2544 KB |
Output is correct |
4 |
Correct |
11 ms |
2384 KB |
Output is correct |
5 |
Correct |
2 ms |
2384 KB |
Output is correct |
6 |
Correct |
8 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2552 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
13 ms |
2716 KB |
Output is correct |
11 |
Correct |
27 ms |
2384 KB |
Output is correct |
12 |
Correct |
9 ms |
2556 KB |
Output is correct |
13 |
Correct |
8 ms |
2384 KB |
Output is correct |
14 |
Correct |
8 ms |
2384 KB |
Output is correct |
15 |
Correct |
8 ms |
2384 KB |
Output is correct |
16 |
Correct |
8 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
2896 KB |
Output is correct |
2 |
Correct |
20 ms |
2896 KB |
Output is correct |
3 |
Correct |
19 ms |
2896 KB |
Output is correct |
4 |
Correct |
9 ms |
2384 KB |
Output is correct |
5 |
Correct |
8 ms |
2384 KB |
Output is correct |
6 |
Correct |
8 ms |
2384 KB |
Output is correct |
7 |
Correct |
7 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
22 ms |
2896 KB |
Output is correct |
10 |
Correct |
21 ms |
2896 KB |
Output is correct |
11 |
Correct |
30 ms |
2912 KB |
Output is correct |
12 |
Correct |
19 ms |
2940 KB |
Output is correct |
13 |
Correct |
19 ms |
2896 KB |
Output is correct |
14 |
Correct |
19 ms |
2896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
2384 KB |
Output is correct |
2 |
Correct |
8 ms |
2384 KB |
Output is correct |
3 |
Correct |
15 ms |
2896 KB |
Output is correct |
4 |
Correct |
8 ms |
2384 KB |
Output is correct |
5 |
Correct |
18 ms |
3052 KB |
Output is correct |
6 |
Correct |
1 ms |
2556 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
23 ms |
2896 KB |
Output is correct |
9 |
Correct |
23 ms |
3048 KB |
Output is correct |
10 |
Correct |
19 ms |
2896 KB |
Output is correct |
11 |
Correct |
17 ms |
3068 KB |
Output is correct |
12 |
Correct |
18 ms |
2896 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
17 ms |
2852 KB |
Output is correct |
20 |
Correct |
20 ms |
2896 KB |
Output is correct |
21 |
Correct |
17 ms |
2896 KB |
Output is correct |
22 |
Correct |
19 ms |
2896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
2384 KB |
Output is correct |
2 |
Correct |
8 ms |
2384 KB |
Output is correct |
3 |
Correct |
8 ms |
2544 KB |
Output is correct |
4 |
Correct |
11 ms |
2384 KB |
Output is correct |
5 |
Correct |
2 ms |
2384 KB |
Output is correct |
6 |
Correct |
8 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2552 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
13 ms |
2716 KB |
Output is correct |
11 |
Correct |
27 ms |
2384 KB |
Output is correct |
12 |
Correct |
9 ms |
2556 KB |
Output is correct |
13 |
Correct |
8 ms |
2384 KB |
Output is correct |
14 |
Correct |
8 ms |
2384 KB |
Output is correct |
15 |
Correct |
8 ms |
2384 KB |
Output is correct |
16 |
Correct |
8 ms |
2384 KB |
Output is correct |
17 |
Correct |
29 ms |
2896 KB |
Output is correct |
18 |
Correct |
20 ms |
2896 KB |
Output is correct |
19 |
Correct |
19 ms |
2896 KB |
Output is correct |
20 |
Correct |
9 ms |
2384 KB |
Output is correct |
21 |
Correct |
8 ms |
2384 KB |
Output is correct |
22 |
Correct |
8 ms |
2384 KB |
Output is correct |
23 |
Correct |
7 ms |
2384 KB |
Output is correct |
24 |
Correct |
1 ms |
2384 KB |
Output is correct |
25 |
Correct |
22 ms |
2896 KB |
Output is correct |
26 |
Correct |
21 ms |
2896 KB |
Output is correct |
27 |
Correct |
30 ms |
2912 KB |
Output is correct |
28 |
Correct |
19 ms |
2940 KB |
Output is correct |
29 |
Correct |
19 ms |
2896 KB |
Output is correct |
30 |
Correct |
19 ms |
2896 KB |
Output is correct |
31 |
Correct |
8 ms |
2384 KB |
Output is correct |
32 |
Correct |
8 ms |
2384 KB |
Output is correct |
33 |
Correct |
15 ms |
2896 KB |
Output is correct |
34 |
Correct |
8 ms |
2384 KB |
Output is correct |
35 |
Correct |
18 ms |
3052 KB |
Output is correct |
36 |
Correct |
1 ms |
2556 KB |
Output is correct |
37 |
Correct |
1 ms |
2384 KB |
Output is correct |
38 |
Correct |
23 ms |
2896 KB |
Output is correct |
39 |
Correct |
23 ms |
3048 KB |
Output is correct |
40 |
Correct |
19 ms |
2896 KB |
Output is correct |
41 |
Correct |
17 ms |
3068 KB |
Output is correct |
42 |
Correct |
18 ms |
2896 KB |
Output is correct |
43 |
Correct |
1 ms |
2384 KB |
Output is correct |
44 |
Correct |
1 ms |
2384 KB |
Output is correct |
45 |
Correct |
1 ms |
2384 KB |
Output is correct |
46 |
Correct |
1 ms |
2384 KB |
Output is correct |
47 |
Correct |
1 ms |
2384 KB |
Output is correct |
48 |
Correct |
1 ms |
2384 KB |
Output is correct |
49 |
Correct |
17 ms |
2852 KB |
Output is correct |
50 |
Correct |
20 ms |
2896 KB |
Output is correct |
51 |
Correct |
17 ms |
2896 KB |
Output is correct |
52 |
Correct |
19 ms |
2896 KB |
Output is correct |
53 |
Correct |
20 ms |
3920 KB |
Output is correct |
54 |
Correct |
22 ms |
3920 KB |
Output is correct |
55 |
Correct |
19 ms |
3940 KB |
Output is correct |
56 |
Correct |
8 ms |
2384 KB |
Output is correct |
57 |
Correct |
9 ms |
2568 KB |
Output is correct |
58 |
Correct |
19 ms |
3664 KB |
Output is correct |
59 |
Correct |
18 ms |
3664 KB |
Output is correct |
60 |
Correct |
23 ms |
3664 KB |
Output is correct |
61 |
Correct |
18 ms |
3664 KB |
Output is correct |
62 |
Correct |
40 ms |
3664 KB |
Output is correct |
63 |
Correct |
66 ms |
3664 KB |
Output is correct |
64 |
Correct |
20 ms |
3664 KB |
Output is correct |
65 |
Correct |
30 ms |
3780 KB |
Output is correct |
66 |
Correct |
42 ms |
3664 KB |
Output is correct |
67 |
Correct |
12 ms |
3664 KB |
Output is correct |
68 |
Correct |
21 ms |
4176 KB |
Output is correct |
69 |
Correct |
21 ms |
4176 KB |
Output is correct |
70 |
Correct |
21 ms |
4176 KB |
Output is correct |
71 |
Correct |
20 ms |
4176 KB |
Output is correct |
72 |
Correct |
20 ms |
4080 KB |
Output is correct |
73 |
Correct |
20 ms |
4176 KB |
Output is correct |
74 |
Correct |
22 ms |
4176 KB |
Output is correct |