이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
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);
int opt = fs[e] + fe[s];
return opt;
for ( int i = 0; i < m; i++ ){
if ( chmin(opt, fs[e] + fe[V[i]] + ts[U[i]] + D[i] + C[i]) ){
//~ cout << opt << " -> " << fs[e] << " " << fe[V[i]] << " " << ts[U[i]] << ln;
}
}
return opt;
};
int ans = f(0, n - 1);
for ( int i = 0; i < m; i++ ){
swap(U[i], V[i]);
chmin(ans, f(0, n - 1) + D[i]);
swap(U[i], V[i]);
}
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... |