This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 2000+100, M = 1e5+10, K = 52, MX = 30;
int n, m;
vector<array<ll, 4>> g[N], r[N];
vector<array<ll, 4>> E;
ll d1[N], d2[N], d3[N], d4[N];
vector<bool> in;
void solve(){
cin >> n >> m;
in.resize(m);
for(int i = 0; i < m; ++i){
int u, v, c, d; cin >> u >> v >> c >> d;
g[u].pb({v, c, d, i});
r[v].pb({u, c, d, i});
E.pb({u, v, c, d});
}
for(int i = 1; i <= n; ++i) d1[i] = d2[i] = d3[i] = d4[i] = 1e15;
vector<bool> used(n+1);
priority_queue<array<ll, 3>> Q;
Q.push({0, 1, -1});
d1[1] = 0;
while(!Q.empty()){
int v = Q.top()[1]; int e = Q.top()[2]; Q.pop();
if(used[v]) continue;
if(e >= 0)
in[e] = 1;
used[v] = 1;
for(auto U: g[v]){
int u = U[0];
if(d1[u] > d1[v] + U[1]){
d1[u] = d1[v] + U[1];
Q.push({-d1[u], u, U[3]});
}
}
}
fill(all(used), false);
Q.push({0, 1, -1});
d3[1] = 0;
while(!Q.empty()){
int v = Q.top()[1]; int e = Q.top()[2]; Q.pop();
if(used[v]) continue;
if(e >= 0)
in[e] = 1;
used[v] = 1;
for(auto U: r[v]){
int u = U[0];
if(d3[u] > d3[v] + U[1]){
d3[u] = d3[v] + U[1];
Q.push({-d3[u], u, U[3]});
}
}
}
fill(all(used), false);
Q.push({0, n, -1});
d2[n] = 0;
while(!Q.empty()){
int v = Q.top()[1]; int e = Q.top()[2]; Q.pop();
if(used[v]) continue;
if(e >= 0)
in[e] = 1;
used[v] = 1;
for(auto U: g[v]){
int u = U[0];
if(d2[u] > d2[v] + U[1]){
d2[u] = d2[v] + U[1];
Q.push({-d2[u], u, U[3]});
}
}
}
fill(all(used), false);
Q.push({0, n, -1});
d4[n] = 0;
while(!Q.empty()){
int v = Q.top()[1]; int e = Q.top()[2]; Q.pop();
if(used[v]) continue;
if(e >= 0)
in[e] = 1;
used[v] = 1;
for(auto U: r[v]){
int u = U[0];
if(d4[u] > d4[v] + U[1]){
d4[u] = d4[v] + U[1];
Q.push({-d4[u], u, U[3]});
}
}
}
ll ans = d1[n] + d2[1];
// cout << d1[n] << ' ' << d2[1] << '\n';
for(int i = 0; i < m; ++i){
int u = E[i][0], v = E[i][1];
ll w = E[i][2], d = E[i][3];
if(in[i]){
fill(all(used), false);
vector<ll> D(n+1, 1e15), D1(n+1, 1e15);
D[1] = 0;
Q.push({0, 1, -1});
while(!Q.empty()){
int x = Q.top()[1]; Q.pop();
if(used[x]) continue;
used[x] = 1;
for(auto U: g[x]){
if(U[3] == i) continue;
int y = U[0];
if(D[y] > D[x] + U[1]){
D[y] = D[x] + U[1];
Q.push({-D[y], y, -1});
}
}
if(x == v){
if(D[u] > D[v] + w){
D[u] = D[v] + w;
Q.push({-D[u], u, -1});
}
}
}
fill(all(used), false);
D1[n] = 0;
Q.push({0, n, -1});
while(!Q.empty()){
int x = Q.top()[1]; Q.pop();
if(used[x]) continue;
used[x] = 1;
for(auto U: g[x]){
if(U[3] == i) continue;
int y = U[0];
if(D1[y] > D1[x] + U[1]){
D1[y] = D1[x] + U[1];
Q.push({-D1[y], y, -1});
}
}
if(x == v){
if(D1[u] > D1[v] + w){
D1[u] = D1[v] + w;
Q.push({-D1[u], u, -1});
}
}
}
ans = min(ans, D[n] + D1[1] + d);
// for(int i = 1; i <= n; ++i) cout << D[i] << ' ';
// en;
}else{
ans = min({
ans,
d1[v] + d4[u] + d + w + min(d2[1], d2[v] + d3[u] + w),
min(d1[n], d1[v] + d4[u] + w) + d2[v] + d3[u] + d + w
});
}
}
// for(int i = 1; i <= n; ++i){
// cout << d1[i] << ' ' << d2[i] << '\n';
// }
cout << (ans > 1e10 ? -1 : ans);
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
// en;
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message (stderr)
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:178:15: warning: unused variable 'aa' [-Wunused-variable]
178 | int tt = 1, aa;
| ^~
# | 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... |