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 ll long long
#define int long long
const int nmax = 200 + 5, N = 3e5 + 5;
const ll oo = 1e18;
const int lg = 7, M = 4e3;
const int base = 2e5, mod = 998244353;
#define pii pair<ll, ll>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';cout << endl
using namespace std;
multiset<pii> adj[nmax];
struct node{
int u, v, c, d;
}a[N];
ll f[nmax][nmax];
int n, m;
priority_queue<pii, vector<pii>, greater<pii>> q;
ll dp[nmax];
ll dist(int x){
for(int i = 1; i <= n; ++i) dp[i] = oo;
q.push({0, x});
dp[x] =0;
while(q.size()){
pii tmp = q.top();q.pop();
int u = tmp.se;
ll val = tmp.fi;
if(val != dp[u]) continue;
for(auto [v, w] : adj[u]){
if(dp[v] > dp[u] + w){
dp[v] = dp[u] + w;
q.push({dp[v], v});
}
}
}
if(x == 1) return dp[n];
return dp[1];
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> n >> m;
memset(f, 0x3f, sizeof f);
for(int i = 1; i <= n; ++i) f[i][i] =0;
for(int i = 1; i <= m; ++i){
int u, v, c, d;
cin >> u >> v >> c >> d;
a[i] = {u, v, c, d};
adj[u].insert({v, c});
f[u][v] = min(f[u][v], 1ll*c);
}
for(int k = 1; k <= n; ++k){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
}
}
}
ll ans = f[1][n] + f[n][1];
for(int i = 1; i <= m; ++i){
int u = a[i].v, v = a[i].u;
ll c1 = min(f[1][n], f[1][u] + f[v][n] + a[i].c) + f[n][1] + a[i].d;
ll c2 = f[1][n]+ min(f[n][1], f[n][u] + f[v][1] + a[i].c) + a[i].d;
ll cur = min(c1, c2);
// cout << cur << ' ';
if(cur >= ans) continue;
swap(u, v);
adj[u].erase(adj[u].find({v, a[i].c}));
adj[v].insert({u, a[i].c});
ans = min(ans, dist(1) + dist(n) + a[i].d);
swap(u, v);
adj[u].erase(adj[u].find({v, a[i].c}));
adj[v].insert({u, a[i].c});
}
if(ans >= oo / 2) cout << -1;
else cout << ans;
}
/*
3 1
4 1 1
5 2 2
*/
Compilation message (stderr)
ho_t4.cpp:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
44 | main(){
| ^~~~
# | 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... |