#include <bits/stdc++.h>
#include <experimental/random>
#include <random>
using namespace std;
using ll = long long;
using ld = long double;
const ll INF = 2e18, MOD = 998244353;
void solve();
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int q = 1;
//cin >> q;
while (q--) {
solve();
}
}
struct e {
int v, c, d, id;
};
int n, m;
vector<bool> calc(vector<vector<e>>& g, int s) {
vector<pair<int, int>> pred(n, {-1, -1});
vector<ll> dp(n, INF);
dp[s] = 0;
set<pair<ll, int>> dj;
dj.insert({0, s});
while (!empty(dj)) {
auto [w, u] = *dj.begin();
dj.erase(dj.begin());
for (auto [v, c, d, id] : g[u]) {
if (dp[v] > w + c) {
pred[v] = {u, id};
dj.erase({dp[v], v});
dp[v] = w + c;
dj.insert({dp[v], v});
}
}
}
vector<bool> ans(m);
for (int i = 0; i < n; i++) {
if (pred[i].second != -1) {
ans[pred[i].second] = 1;
}
}
return ans;
}
vector<ll> cost(vector<vector<e>>& g, int s) {
vector<ll> dp(n, INF);
dp[s] = 0;
set<pair<int, int>> dj;
dj.insert({0, s});
while (!empty(dj)) {
auto [w, u] = *dj.begin();
dj.erase(dj.begin());
for (auto [v, c, d, id] : g[u]) {
if (dp[v] > w + c) {
dj.erase({dp[v], v});
dp[v] = w + c;
dj.insert({dp[v], v});
}
}
}
return dp;
}
void rev_ed(int u, int v, int idx, vector<vector<e>>& g) {
vector<e> x = g[u];
g[u].clear();
e hv{};
for (auto ed : x) {
if (ed.id != idx) {
g[u].push_back(ed);
} else {
hv = ed;
}
}
g[v].push_back({u, hv.c, hv.d, hv.id});
}
void del_ed(int u, int idx, vector<vector<e>>& g) {
vector<e> x = g[u];
g[u].clear();
for (auto ed : x) {
if (ed.id != idx) {
g[u].push_back(ed);
}
}
}
void solve() {
cin >> n >> m;
vector<vector<e>> g(n), rev_g(n);
vector<e> edges;
for (int i = 0; i < m; i++) {
int u, v, c, d;
cin >> u >> v >> c >> d;
u--, v--;
edges.push_back({u, v, c, d});
g[u].push_back({v, c, d, i});
rev_g[v].push_back({u, c, d, i});
}
vector<bool> dp1 = calc(g, 0);
vector<bool> dp2 = calc(rev_g, n - 1);
vector<bool> dp3 = calc(g, n - 1);
vector<bool> dp4 = calc(rev_g, 0);
vector<ll> c1 = cost(g, 0);
vector<ll> c2 = cost(rev_g, n - 1);
vector<ll> c3 = cost(g, n - 1);
vector<ll> c4 = cost(rev_g, 0);
ll ans = c1[n - 1] + c3[0];
for (int i = 0; i < m; i++) {
auto [u, v, w, d] = edges[i];
rev_ed(u, v, i, g);
rev_ed(v, u, i, rev_g);
ll p1 = c1[n - 1], p2 = c3[0];
if (dp1[i]) {
p1 = cost(g, 0)[n - 1];
}
if (dp3[i]) {
p2 = cost(g, n - 1)[0];
}
ll cnt = c1[v] + w;
if (dp1[i]) {
del_ed(u, i, g);
cnt = cost(g, 0)[v] + w;
g[u].push_back({v, w, d, i});
}
if (cnt < p1) {
ll cnt2 = c2[u];
if (dp2[i]) {
del_ed(v, i, rev_g);
cnt2 = cost(rev_g, n - 1)[u];
rev_g[v].push_back({u, w, d, i});
}
p1 = min(p1, cnt + cnt2);
}
if (p1 + d < ans) {
cnt = c3[v] + w;
if (dp3[i]) {
del_ed(u, i, g);
cnt = cost(g, n - 1)[v] + w;
g[u].push_back({v, w, d, i});
}
if (cnt < p2) {
ll cnt2 = c4[u];
if (dp4[i]) {
del_ed(v, i, rev_g);
cnt2 = cost(rev_g, 0)[u];
rev_g[v].push_back({u, w, d, i});
}
p2 = min(p2, cnt + cnt2);
}
ans = min(ans, p1 + p2 + d);
}
rev_ed(v, u, i, g);
rev_ed(u, v, i, rev_g);
}
if (ans >= INF) {
cout << -1;
return;
}
cout << ans;
}
# | 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... |