#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define all(x...) begin(x), end(x)
using ll = long long;
template<class T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
const int N = 1 << 9, M = 1 << 16;
const ll inf = 1e18;
int n;
ll d[N][N];
int a[M], b[M], w[M], c[M];
vector<int> g[N];
using pq_t = __gnu_pbds::priority_queue<pair<ll, int>, greater<>>;
pq_t::point_iterator it[N];
ll dij(int s, int t) {
pq_t pq;
rep(u, 0, n) if (u != s) it[u] = pq.push({inf, u});
it[s] = pq.push({0, s});
while (!pq.empty()) {
ll dist; int u;
tie(dist, u) = pq.top();
pq.pop(); it[u] = nullptr;
if (u == t) return dist;
for (int e : g[u]) {
int v = b[e];
if (it[v] != nullptr) {
dist += w[e];
if (dist < it[v]->first) pq.modify(it[v], {dist, v});
dist -= w[e];
}
}
}
return inf;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int m; cin >> n >> m;
rep(u, 0, n) rep(v, 0, n) d[u][v] = inf;
rep(u, 0, n) d[u][u] = 0;
rep(e, 0, m) {
cin >> a[e] >> b[e] >> w[e] >> c[e], --a[e], --b[e];
ckmin(d[a[e]][b[e]], ll(w[e]));
g[a[e]].push_back(e);
}
rep(w, 0, n) rep(u, 0, n) rep(v, 0, n) ckmin(d[u][v], d[u][w] + d[w][v]);
int s = 0, t = n - 1;
ll ans = d[s][t] + d[t][s];
rep(e, 0, m) {
ll to = min(d[s][t], d[s][b[e]] + w[e] + d[a[e]][t]);
ll fr = min(d[t][s], d[t][b[e]] + w[e] + d[a[e]][s]);
if (c[e] + to + fr < ans) {
g[a[e]].erase(find(all(g[a[e]]), e)); swap(a[e], b[e]); g[a[e]].push_back(e);
ckmin(ans, c[e] + dij(s, t) + dij(t, s));
g[a[e]].pop_back(); swap(a[e], b[e]); g[a[e]].push_back(e);
}
}
cout << (ans < inf ? ans : -1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1280 KB |
Output is correct |
2 |
Correct |
10 ms |
1152 KB |
Output is correct |
3 |
Correct |
12 ms |
1280 KB |
Output is correct |
4 |
Correct |
45 ms |
1280 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
70 ms |
1152 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
17 ms |
1280 KB |
Output is correct |
11 |
Correct |
32 ms |
1280 KB |
Output is correct |
12 |
Correct |
20 ms |
1312 KB |
Output is correct |
13 |
Correct |
15 ms |
1192 KB |
Output is correct |
14 |
Correct |
13 ms |
1272 KB |
Output is correct |
15 |
Correct |
13 ms |
1408 KB |
Output is correct |
16 |
Correct |
11 ms |
1280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
2940 KB |
Output is correct |
2 |
Correct |
39 ms |
2972 KB |
Output is correct |
3 |
Correct |
98 ms |
3064 KB |
Output is correct |
4 |
Correct |
13 ms |
1312 KB |
Output is correct |
5 |
Correct |
13 ms |
1280 KB |
Output is correct |
6 |
Correct |
10 ms |
1152 KB |
Output is correct |
7 |
Correct |
11 ms |
1152 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
40 ms |
2936 KB |
Output is correct |
10 |
Correct |
54 ms |
2936 KB |
Output is correct |
11 |
Correct |
42 ms |
2936 KB |
Output is correct |
12 |
Correct |
35 ms |
2940 KB |
Output is correct |
13 |
Correct |
34 ms |
2936 KB |
Output is correct |
14 |
Correct |
34 ms |
2936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
1276 KB |
Output is correct |
2 |
Correct |
13 ms |
1408 KB |
Output is correct |
3 |
Correct |
35 ms |
2644 KB |
Output is correct |
4 |
Correct |
10 ms |
1188 KB |
Output is correct |
5 |
Correct |
36 ms |
2812 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
424 KB |
Output is correct |
8 |
Correct |
34 ms |
2552 KB |
Output is correct |
9 |
Correct |
36 ms |
2560 KB |
Output is correct |
10 |
Correct |
33 ms |
2560 KB |
Output is correct |
11 |
Correct |
38 ms |
2552 KB |
Output is correct |
12 |
Correct |
30 ms |
2552 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
36 ms |
2680 KB |
Output is correct |
20 |
Correct |
37 ms |
2680 KB |
Output is correct |
21 |
Correct |
35 ms |
2560 KB |
Output is correct |
22 |
Correct |
33 ms |
2560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1280 KB |
Output is correct |
2 |
Correct |
10 ms |
1152 KB |
Output is correct |
3 |
Correct |
12 ms |
1280 KB |
Output is correct |
4 |
Correct |
45 ms |
1280 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
70 ms |
1152 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
17 ms |
1280 KB |
Output is correct |
11 |
Correct |
32 ms |
1280 KB |
Output is correct |
12 |
Correct |
20 ms |
1312 KB |
Output is correct |
13 |
Correct |
15 ms |
1192 KB |
Output is correct |
14 |
Correct |
13 ms |
1272 KB |
Output is correct |
15 |
Correct |
13 ms |
1408 KB |
Output is correct |
16 |
Correct |
11 ms |
1280 KB |
Output is correct |
17 |
Correct |
36 ms |
2940 KB |
Output is correct |
18 |
Correct |
39 ms |
2972 KB |
Output is correct |
19 |
Correct |
98 ms |
3064 KB |
Output is correct |
20 |
Correct |
13 ms |
1312 KB |
Output is correct |
21 |
Correct |
13 ms |
1280 KB |
Output is correct |
22 |
Correct |
10 ms |
1152 KB |
Output is correct |
23 |
Correct |
11 ms |
1152 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
40 ms |
2936 KB |
Output is correct |
26 |
Correct |
54 ms |
2936 KB |
Output is correct |
27 |
Correct |
42 ms |
2936 KB |
Output is correct |
28 |
Correct |
35 ms |
2940 KB |
Output is correct |
29 |
Correct |
34 ms |
2936 KB |
Output is correct |
30 |
Correct |
34 ms |
2936 KB |
Output is correct |
31 |
Correct |
14 ms |
1276 KB |
Output is correct |
32 |
Correct |
13 ms |
1408 KB |
Output is correct |
33 |
Correct |
35 ms |
2644 KB |
Output is correct |
34 |
Correct |
10 ms |
1188 KB |
Output is correct |
35 |
Correct |
36 ms |
2812 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
424 KB |
Output is correct |
38 |
Correct |
34 ms |
2552 KB |
Output is correct |
39 |
Correct |
36 ms |
2560 KB |
Output is correct |
40 |
Correct |
33 ms |
2560 KB |
Output is correct |
41 |
Correct |
38 ms |
2552 KB |
Output is correct |
42 |
Correct |
30 ms |
2552 KB |
Output is correct |
43 |
Correct |
1 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
36 ms |
2680 KB |
Output is correct |
50 |
Correct |
37 ms |
2680 KB |
Output is correct |
51 |
Correct |
35 ms |
2560 KB |
Output is correct |
52 |
Correct |
33 ms |
2560 KB |
Output is correct |
53 |
Correct |
34 ms |
2552 KB |
Output is correct |
54 |
Correct |
33 ms |
2552 KB |
Output is correct |
55 |
Correct |
38 ms |
2560 KB |
Output is correct |
56 |
Correct |
12 ms |
1280 KB |
Output is correct |
57 |
Correct |
12 ms |
1280 KB |
Output is correct |
58 |
Correct |
34 ms |
2304 KB |
Output is correct |
59 |
Correct |
36 ms |
2304 KB |
Output is correct |
60 |
Correct |
35 ms |
2304 KB |
Output is correct |
61 |
Correct |
34 ms |
2432 KB |
Output is correct |
62 |
Correct |
75 ms |
2304 KB |
Output is correct |
63 |
Correct |
173 ms |
2304 KB |
Output is correct |
64 |
Correct |
56 ms |
2424 KB |
Output is correct |
65 |
Correct |
107 ms |
2432 KB |
Output is correct |
66 |
Correct |
187 ms |
2432 KB |
Output is correct |
67 |
Correct |
16 ms |
1664 KB |
Output is correct |
68 |
Correct |
39 ms |
2560 KB |
Output is correct |
69 |
Correct |
37 ms |
2552 KB |
Output is correct |
70 |
Correct |
34 ms |
2560 KB |
Output is correct |
71 |
Correct |
37 ms |
2560 KB |
Output is correct |
72 |
Correct |
34 ms |
2680 KB |
Output is correct |
73 |
Correct |
33 ms |
2560 KB |
Output is correct |
74 |
Correct |
34 ms |
2560 KB |
Output is correct |