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>
using namespace std;
typedef long long ll;
const int N = 1e5 + 12, MOD = (int)1e9 + 7;
#define int ll
const ll inf = 1e16;
int n,m;
int U[N],V[N],C[N],D[N];
void dijkstra(int ver,vector<ll> &d,vector<int> &pr) {
d.resize(n);
pr.resize(n);
for(int i = 0;i < n;i++) {
d[i] = inf;
}
d[ver] = 0;
vector<int> vis(n,0);
vector<vector<int>> g;g.resize(n);
for(int i = 0;i < m;++i) {
g[U[i]].push_back(i);
}
for(int i = 0;i < n;i++) {
int v = -1;
for(int j = 0;j < n;j++) {
if(vis[j]) continue;
if(v == -1 || d[j] < d[v]) {
v = j;
}
}
vis[v] = 1;
for(int t:g[v]) {
int to = V[t],w = C[t];
if(d[to] > d[v] + w) {
d[to] = d[v] + w;
pr[to] = t;
}
}
}
}
vector<ll> d[N];
vector<int> p[N];
ll res = inf;
bool in[N];
void test() {
cin >> n >> m;
for(int i = 0;i < m;i++) {
cin >> U[i] >> V[i] >> C[i] >> D[i];
U[i]--;V[i]--;
}
for(int i = 0;i < n;++i) {
dijkstra(i,d[i],p[i]);
}
ll x = d[0][n - 1],y = d[n - 1][0];
res = x + y;
vector<ll> bfd;
vector<int> bfp;
auto make = [&](int j){
in[j] = 1;
swap(V[j],U[j]);
ll nv = D[j];
dijkstra(0,bfd,bfp);
nv += bfd[n - 1];
dijkstra(n - 1,bfd,bfp);
nv += bfd[0];
res = min(res,nv);
swap(V[j],U[j]);
};
for(int i = 0;i < n;i++) {
if(d[0][i] != inf && i != 0) {
make(p[0][i]);
}
if(d[n - 1][i] != inf && i != n - 1) {
make(p[n - 1][i]);
}
}
for(int i = 0;i < m;i++) {
if(in[i]) continue;
swap(U[i],V[i]);
ll val = min(x,d[0][U[i]] + d[V[i]][n - 1] + C[i]) + D[i];
val += min(y,d[n - 1][U[i]] + d[V[i]][0] + C[i]);
res = min(res,val);
}
if(res >= inf) res = -1;
cout << res << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
# | 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... |