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;
ostream& operator<<(ostream &out, string str) {
for(char c : str) out << c;
return out;
}
template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
out << "{";
for(auto it = a.begin(); it != a.end(); it = next(it))
out << (it != a.begin() ? ", " : "") << *it;
return out << "}";
}
void dump() { cerr << "\n"; }
template<class T, class... Ts> void dump(T a, Ts... x) {
cerr << a << ", ";
dump(x...);
}
#ifdef DEBUG
# define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
# define debug(...) false
#endif
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define ST first
#define ND second
template<class T> int size(T && a) { return a.size(); }
using LL = long long;
using PII = pair<int, int>;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(m), b(m), c(m), d(m);
vector<vector<PII>> g(n), r(n);
REP(i, m) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
a[i]--, b[i]--;
g[a[i]].emplace_back(b[i], i);
r[b[i]].emplace_back(a[i], i);
}
vector<bool> on_path(m);
auto dijkstra = [&](vector<vector<PII>> &adj, int source, int banned, bool mark) {
vector<int> dst(n, 1e9), last(n, -1);
priority_queue<PII> Q;
dst[source] = 0;
Q.emplace(0, source);
while(!Q.empty()) {
int d, v;
tie(d, v) = Q.top();
Q.pop();
if(-d != dst[v]) continue;
for(auto &[u, e] : adj[v]) {
if(banned == e) continue;
if(dst[u] > dst[v] + c[e]) {
if(mark) last[u] = e;
dst[u] = dst[v] + c[e];
Q.emplace(-dst[u], u);
}
}
}
REP(i, n) if(last[i] != -1)
on_path[last[i]] = true;
return dst;
};
auto norm_source = dijkstra(g, 0, -1, true);
auto norm_sink = dijkstra(g, n - 1, -1, true);
auto rev_source = dijkstra(r, 0, -1, false);
auto rev_sink = dijkstra(r, n - 1, -1, false);
auto add_edge = [&](int i) {
g[b[i]].emplace_back(a[i], size(a));
r[a[i]].emplace_back(b[i], size(b));
a.emplace_back(b[i]);
b.emplace_back(a[i]);
c.emplace_back(c[i]);
};
auto pop_edge = [&](int i) {
g[b[i]].pop_back();
r[a[i]].pop_back();
a.pop_back();
b.pop_back();
c.pop_back();
};
int ans = norm_source[n - 1] + norm_sink[0];
REP(i, m) {
if(on_path[i]) {
add_edge(i);
auto source = dijkstra(g, 0, i, false);
auto sink = dijkstra(g, n - 1, i, false);
ans = min(ans, source[n - 1] + sink[0] + d[i]);
pop_edge(i);
}
else {
int source = min(norm_source[n - 1], norm_source[b[i]] + c[i] + rev_sink[a[i]]);
int sink = min(norm_sink[0], norm_sink[b[i]] + c[i] + rev_source[a[i]]);
ans = min(ans, source + sink + d[i]);
}
}
if(ans >= 1e9) ans = -1;
cout << 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... |