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 <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 201, INF = 4e18+9;
struct edge{
ll u, v, c, d, id;
};
vector<ll> dijkstra(int s, vector<vector<edge>> &adj, edge e){
int ban = e.id;
adj[e.v].push_back({e.v, e.u, e.c, e.d, -1});
int n = adj.size();
vector<ll> dist(n+1, INF);
dist[s] = 0;
vector<bool> vis(n+1, false);
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
pq.push({0, s});
while(!pq.empty()){
ll d_u = pq.top().first;
int u = pq.top().second;
pq.pop();
if(vis[u]){
continue;
}
vis[u] = true;
for(auto it : adj[u]){
int v = it.v, id = it.id;
ll w = it.c;
if(vis[v] || ban == id) continue;
if(dist[v] > d_u+w){
dist[v] = d_u+w;
pq.push({dist[v], v});
}
}
}
adj[e.v].pop_back();
return dist;
}
vector<ll> dijkstra_trace(int s, int t, int m, vector<vector<edge>> &adj){
vector<ll> res(m+1, 0);
int n = adj.size();
vector<ll> dist(n+1, INF);
vector<pair<int, int>> pa(n+1);
dist[s] = 0;
vector<bool> vis(n+1, false);
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
pq.push({0, s});
while(!pq.empty()){
ll d_u = pq.top().first;
int u = pq.top().second;
pq.pop();
if(vis[u]){
continue;
}
vis[u] = true;
for(auto it : adj[u]){
int v = it.v;
ll w = it.c;
if(vis[v]) continue;
if(dist[v] > d_u+w){
dist[v] = d_u+w;
pa[v] = {u, it.id};
pq.push({dist[v], v});
}
}
}
if(dist[t] == INF) return res;
while(t != s){
res[pa[t].second] = 1;
t = pa[t].first;
}
return res;
}
void solve(){
int n, m;
cin >> n >> m;
vector<vector<edge>> adj(n+1), adj_rev(n+1);
vector<edge> e(1);
for(int i = 1; i <= m; i++){
int u, v;
ll c, d;
cin >> u >> v >> c >> d;
e.push_back({u, v, c, d, i});
adj[u].push_back({u, v, c, d, i});
adj_rev[v].push_back({v, u, c, d, i});
}
edge null = {0, 0, INF, INF, -1};
vector<ll> dist_1f = dijkstra(1, adj, null),
dist_1b = dijkstra(n, adj_rev, null),
dist_nf = dijkstra(n, adj, null),
dist_nb = dijkstra(1, adj_rev, null);
vector<ll> bamboo_1 = dijkstra_trace(1, n, m, adj),
bamboo_n = dijkstra_trace(n, 1, m, adj);
auto solvefor = [&](int s, int t, vector<ll> dist_f, vector<ll> dist_b, vector<ll> bamboo) -> vector<ll>{
vector<ll> res(m+1);
for(int i = 1; i <= m; i++){
if(bamboo[i]){
vector<ll> a = dijkstra(s, adj, e[i]);
res[i] = a[t];
}else{
res[i] = min(dist_f[t], dist_f[e[i].v]+e[i].c+dist_b[e[i].u]);
}
}
return res;
};
vector<ll> res_1 = solvefor(1, n, dist_1f, dist_1b, bamboo_1),
res_n = solvefor(n, 1, dist_nf, dist_nb, bamboo_n);
ll ans = dist_1f[n] + dist_nf[1];
for(int i = 1; i <= m; i++){
ans = min(ans, res_1[i] + res_n[i] + e[i].d);
}
if(ans >= INF) ans = -1;
cout << ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# | 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... |