This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |