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 all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const int inf = 1e12;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
// subtask #2
vector <int> U(m), V(m), C(m), D(m);
for ( int i = 0; i < m; i++ ){
cin >> U[i] >> V[i] >> C[i] >> D[i];
--U[i], --V[i];
}
vector <int> is(m);
auto get = [&](auto U, auto V, auto C, int sx, int rev = -1){
vector <vector<ar<int,3>>> adj(n);
for ( int i = 0; i < (int)U.size(); i++ ){
int u = U[i], v = V[i];
if ( i == rev ) swap(u, v);
adj[u].pb({v, C[i], i});
}
vector <int> dp(n, inf), fa(n, -1);
priority_queue <ar<int,2>> q;
q.push({0, sx}); dp[sx] = 0;
while ( !q.empty() ){
auto [val, u] = q.top();
q.pop();
if ( -val != dp[u] ) continue;
for ( auto &[v, w, j]: adj[u] ){
if ( chmin(dp[v], dp[u] + w) ){
q.push({-dp[v], v});
fa[v] = j;
}
}
}
for ( auto &x: fa ){
if ( x != -1 ) is[x] = 1;
}
return dp;
};
auto f = [&](int j){
return get(U, V, C, 0, j)[n - 1] + get(U, V, C, n - 1, j)[0] + D[j];
};
int s = 0, e = n - 1;
auto fs = get(U, V, C, s), fe = get(U, V, C, e);
auto IS = is;
auto ts = get(V, U, C, s), te = get(V, U, C, e);
int opt = fs[e] + fe[s];
for ( int i = 0; i < m; i++ ){
int x = min(fs[e], fs[V[i]] + C[i] + te[U[i]]);
int y = min(fe[s], fe[V[i]] + C[i] + ts[U[i]]);
if ( IS[i] ){
chmin(opt, f(i));
} else{
chmin(opt, x + y + D[i]);
}
}
if ( opt >= inf ){
opt = -1;
}
cout << opt;
cout << '\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... |