#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1 << 9, M = 1 << 16;
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, INT_MAX);
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] == INT_MAX) 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] = INT_MAX;
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]);
ll 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);
bool flag = (dijkstra(s, t) < INT_MAX) && (dijkstra(t, s) < INT_MAX);
if (flag) ans = min(ans, c[e] + to + fr);
adj[a[e]].pop_back();
swap(a[e], b[e]);
adj[a[e]].push_back(e);
}
}
cout << (ans == INT_MAX ? -1 : ans) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2384 KB |
Output is correct |
2 |
Incorrect |
8 ms |
2556 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
3036 KB |
Output is correct |
2 |
Correct |
20 ms |
4024 KB |
Output is correct |
3 |
Correct |
20 ms |
4188 KB |
Output is correct |
4 |
Correct |
8 ms |
2640 KB |
Output is correct |
5 |
Correct |
9 ms |
2384 KB |
Output is correct |
6 |
Incorrect |
8 ms |
2384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2384 KB |
Output is correct |
2 |
Correct |
9 ms |
2384 KB |
Output is correct |
3 |
Correct |
17 ms |
2932 KB |
Output is correct |
4 |
Incorrect |
8 ms |
2384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2384 KB |
Output is correct |
2 |
Incorrect |
8 ms |
2556 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |