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>
#include<ext/pb_ds/assoc_container.hpp>
#define INF 1e18
#define ve vector
#define vi ve<int>
#define ii pair<int,int>
#define vii ve<ii>
#define pb push_back
#define fi first
#define se second
#define ll long long
using namespace __gnu_pbds;
using namespace std;
const int nax = 200+5;
const int kax = 5e4+5;
const int MOD = 1e9+7;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int x[kax], y[kax];
ll c[kax], d[kax];
int n,m;
ve<ll> dijkstra(int st, vi adj[nax]){
priority_queue<pair<ll,int>, ve<pair<ll,int>>, greater<pair<ll,int>>> pq;
ve<ll> dist(n+1, INF);
dist[st] = 0;
pq.push({0ll, st});
while(!pq.empty()){
ll cst = pq.top().fi;
int u = pq.top().se;
pq.pop();
if(dist[u] != cst) continue;
for(auto j : adj[u]){
if(dist[y[j]] > (c[j] + dist[u])){
dist[y[j]] = c[j] + dist[u];
pq.push({dist[y[j]], y[j]});
}
}
}
return dist;
}
vi adj[nax], adj_rev[nax];
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; ++i)
{
cin >> x[i] >> y[i] >> c[i] >> d[i];
adj[x[i]].pb(i);
adj_rev[y[i]].pb(i);
}
ve<ll> dist1 = dijkstra(1, adj),
distn = dijkstra(n, adj),
distrev1 = dijkstra(1, adj_rev),
distrevn = dijkstra(n, adj_rev);
ll aff = dist1[n] + distn[1];
for (int i = 0; i < m; ++i)
{
aff = min(aff, dist1[y[i]] + distrevn[x[i]] + distn[1] + c[i] + d[i]);
aff = min(aff, dist1[n] + distn[y[i]] + distrev1[x[i]] + c[i] + d[i]);
aff = min(aff, dist1[y[i]] + distrevn[x[i]] + distn[y[i]] + distrev1[x[i]] + 2*c[i] + d[i]);
}
if(aff == INF) cout << -1 << endl;
else cout << aff << endl;
}
# | 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... |