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>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll inf = 1e17;
vector<ll> dijkstra(vector<vector<array<ll, 3>>> v, int source){
int n = v.size();
vector<ll> dis(n, inf);
vector<bool> bl(n, 0);
priority_queue<pair<ll, int>> pq;
dis[source] = 0;
pq.push({0, source});
while(!pq.empty()){
int nd = pq.top().sc; pq.pop();
if(bl[nd]) continue;
bl[nd] = 1;
for(auto [w, c, x]: v[nd]) if(dis[nd]+w < dis[x]){
dis[x] = dis[nd]+w;
pq.push({-dis[x], x});
}
}
return dis;
}
vector<ll> d1, d2, d3, d4;
vector<vector<array<ll, 3>>> v, vr;
map<array<int, 4>, int> mp;
vector<bool> s1, s2;
vector<bool> bl1, bl2;
vector<int> ddd1, ddd2;
void dfs_1(int nd, int d){
if(bl1[nd]){
return;
}
bl1[nd] = 1;
ddd1[nd] = d;
for(auto [w, c, x]: v[nd]) dfs_1(x, d+1);
}
void dfs_2(int nd, int d){
if(bl2[nd]){
return;
}
bl2[nd] = 1;
ddd2[nd] = d;
for(auto [w, c, x]: v[nd]) dfs_2(x, d+1);
}
void dfs1(int nd){
for(auto [w, c, x]: vr[nd]) if((d1[x] < d1[nd] || (d1[x] == d1[nd] && ddd1[x] < ddd1[nd])) && d1[x]+w == d1[nd]){
s1[mp[{(int)x, (int)nd, (int)w, (int)c}]] = 1;
dfs1(x);
break;
}
}
void dfs2(int nd){
for(auto [w, c, x]: vr[nd]) if((d3[x] < d3[nd] || (d3[x] == d3[nd] && ddd2[x] < ddd2[nd])) && d3[x]+w == d3[nd]){
s2[mp[{(int)x, (int)nd, (int)w, (int)c}]] = 1;
dfs2(x);
break;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
vector<array<int, 4>> e(m);
v.resize(n);
vr.resize(n);
for(int i = 0; i < m; i++){
cin >> e[i][0] >> e[i][1] >> e[i][2] >> e[i][3];
e[i][0]--, e[i][1]--;
mp[e[i]] = i;
v[e[i][0]].pb({e[i][2], e[i][3], e[i][1]});
vr[e[i][1]].pb({e[i][2], e[i][3], e[i][0]});
}
s1.assign(m, 0), s2.assign(m, 0);
d1 = dijkstra(v, 0), d2 = dijkstra(vr, n-1), d3 = dijkstra(v, n-1), d4 = dijkstra(vr, 0);
bl1.assign(n, 0);
bl2.assign(n, 0);
ddd1.assign(n, 0);
ddd2.assign(n, 0);
dfs_1(0, 0);
dfs_2(n-1, 0);
dfs1(n-1);
dfs2(0);
ll ans = inf;
ans = min(ans, d1[n-1]+d3[0]);
for(int i = 0; i < m; i++){
int a = e[i][0], b = e[i][1];
ll w = e[i][2], c = e[i][3];
if(!s2[i] && !s1[i]){
ans = min(ans, c+d1[b]+w+d2[a]+d3[0]);
ans = min(ans, c+d1[n-1]+d3[b]+w+d4[a]);
ans = min(ans, c+d1[b]+w+d2[a]+d3[b]+w+d4[a]);
continue;
}
vector<vector<array<ll, 3>>> v0(n);
for(int j = 0; j < m; j++){
if(j == i) v0[e[j][1]].pb({e[j][2], e[j][3], e[j][0]});
else v0[e[j][0]].pb({e[j][2], e[j][3], e[j][1]});
}
auto dd1 = dijkstra(v0, 0), dd2 = dijkstra(v0, n-1);
ans = min(ans, dd1[n-1]+dd2[0]+e[i][3]);
}
if(ans >= inf) 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... |