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];
}
auto f = [&](int s, int e){
auto get = [&](auto U, auto V, auto C, int sx){
vector <vector<ar<int,2>>> adj(n);
for ( int i = 0; i < (int)U.size(); i++ ){
adj[U[i]].pb({V[i], C[i]});
}
vector <int> dp(n, inf);
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]: adj[u] ){
if ( chmin(dp[v], dp[u] + w) ){
q.push({-dp[v], v});
}
}
}
return dp;
};
auto fs = get(U, V, C, s);
auto fe = get(U, V, C, e);
auto ts = get(V, U, C, s);
auto te = get(V, U, C, e);
int opt = fs[e] + fe[s];
for ( int i = 0; i < m; i++ ){
if ( chmin(opt, min(fs[e], fs[V[i]] + C[i] + te[U[i]]) + fe[V[i]] + C[i] + ts[U[i]] + D[i]) ){
//~ cout << opt << " -> " << fs[e] << " " << fe[V[i]] << " " << ts[U[i]] << ln;
}
}
return opt;
};
int ans = min(f(0, n - 1), f(n - 1, 0));
if ( ans >= inf ) ans = -1;
cout << ans;
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... |