#include <bits/stdc++.h>
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];
bool onPath[M], vis[N];
bool dfs(int u, int t) {
if (u == t) return true;
vis[u] = true;
for (int e : g[u]) {
assert(a[e] == u);
int v = b[e];
assert(d[u][t] <= w[e] + d[v][t]);
if (!vis[v] && w[e] + d[v][t] == d[u][t] && dfs(v, t))
return onPath[e] = true;
}
return false;
}
ll dist[N];
ll dij(int s, int t) {
fill(dist, dist + n, inf);
priority_queue<pair<ll, int>> pq;
pq.push({0, s});
while (!pq.empty()) {
int u = pq.top().second;
if (dist[u] == inf) {
dist[u] = -pq.top().first;
for (int e : g[u]) {
int v = b[e];
pq.push({-(dist[u] + w[e]), v});
}
} else assert(!ckmin(dist[u], -pq.top().first));
pq.pop();
if (u == t) break;
}
return dist[t];
}
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;
fill(vis, vis + n, 0); dfs(s, t);
fill(vis, vis + n, 0); dfs(t, s);
ll ans = d[s][t] + d[t][s];
rep(e, 0, m) {
if (!onPath[e]) {
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]);
ckmin(ans, c[e] + to + fr);
} else {
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(0, n - 1) + dij(n - 1, 0));
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 |
23 ms |
1280 KB |
Output is correct |
2 |
Correct |
10 ms |
1152 KB |
Output is correct |
3 |
Correct |
19 ms |
1272 KB |
Output is correct |
4 |
Correct |
13 ms |
1280 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
10 ms |
1152 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
41 ms |
1280 KB |
Output is correct |
11 |
Correct |
53 ms |
1280 KB |
Output is correct |
12 |
Correct |
62 ms |
1280 KB |
Output is correct |
13 |
Correct |
11 ms |
1280 KB |
Output is correct |
14 |
Correct |
12 ms |
1280 KB |
Output is correct |
15 |
Correct |
12 ms |
1280 KB |
Output is correct |
16 |
Correct |
12 ms |
1280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
5056 KB |
Output is correct |
2 |
Correct |
54 ms |
4900 KB |
Output is correct |
3 |
Correct |
45 ms |
4180 KB |
Output is correct |
4 |
Correct |
13 ms |
1280 KB |
Output is correct |
5 |
Correct |
12 ms |
1280 KB |
Output is correct |
6 |
Correct |
10 ms |
1152 KB |
Output is correct |
7 |
Correct |
10 ms |
1152 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
33 ms |
3456 KB |
Output is correct |
10 |
Correct |
33 ms |
3456 KB |
Output is correct |
11 |
Correct |
114 ms |
4480 KB |
Output is correct |
12 |
Correct |
76 ms |
4588 KB |
Output is correct |
13 |
Correct |
81 ms |
5140 KB |
Output is correct |
14 |
Correct |
94 ms |
5184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
1400 KB |
Output is correct |
2 |
Correct |
10 ms |
1152 KB |
Output is correct |
3 |
Correct |
817 ms |
3220 KB |
Output is correct |
4 |
Correct |
10 ms |
1280 KB |
Output is correct |
5 |
Execution timed out |
1087 ms |
3896 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
1280 KB |
Output is correct |
2 |
Correct |
10 ms |
1152 KB |
Output is correct |
3 |
Correct |
19 ms |
1272 KB |
Output is correct |
4 |
Correct |
13 ms |
1280 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
10 ms |
1152 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
41 ms |
1280 KB |
Output is correct |
11 |
Correct |
53 ms |
1280 KB |
Output is correct |
12 |
Correct |
62 ms |
1280 KB |
Output is correct |
13 |
Correct |
11 ms |
1280 KB |
Output is correct |
14 |
Correct |
12 ms |
1280 KB |
Output is correct |
15 |
Correct |
12 ms |
1280 KB |
Output is correct |
16 |
Correct |
12 ms |
1280 KB |
Output is correct |
17 |
Correct |
51 ms |
5056 KB |
Output is correct |
18 |
Correct |
54 ms |
4900 KB |
Output is correct |
19 |
Correct |
45 ms |
4180 KB |
Output is correct |
20 |
Correct |
13 ms |
1280 KB |
Output is correct |
21 |
Correct |
12 ms |
1280 KB |
Output is correct |
22 |
Correct |
10 ms |
1152 KB |
Output is correct |
23 |
Correct |
10 ms |
1152 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
33 ms |
3456 KB |
Output is correct |
26 |
Correct |
33 ms |
3456 KB |
Output is correct |
27 |
Correct |
114 ms |
4480 KB |
Output is correct |
28 |
Correct |
76 ms |
4588 KB |
Output is correct |
29 |
Correct |
81 ms |
5140 KB |
Output is correct |
30 |
Correct |
94 ms |
5184 KB |
Output is correct |
31 |
Correct |
32 ms |
1400 KB |
Output is correct |
32 |
Correct |
10 ms |
1152 KB |
Output is correct |
33 |
Correct |
817 ms |
3220 KB |
Output is correct |
34 |
Correct |
10 ms |
1280 KB |
Output is correct |
35 |
Execution timed out |
1087 ms |
3896 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |