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 <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 1LL << 60;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
struct edge{
int from, to, c, d, id;
};
int main(){
StarBurstStream
int n, m;
cin >> n >> m;
vector<vector<edge>> g(n + 1);
vector<vector<ll>> dis(n + 1, vector<ll>(n + 1, MAX));
vector<edge> e(m + 1);
for(int i = 1; i <= n; i++) dis[i][i] = 0;
for(int i = 1; i <= m; i++){
int u, v, c, d;
cin >> u >> v >> c >> d;
g[u].eb(e[i] = edge({u, v, c, d, i}));
dis[u][v] = min(dis[u][v], (ll)c);
}
for(int k = 1; k <= n; k++){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
vector<bool> tr(m + 1);
auto eliminate = [&](int s){
vector<ll> d(n + 1, MAX);
vector<int> lst(n + 1, -1);
d[s] = 0;
std::priority_queue<pll, vector<pll>, greater<>> pq;
pq.push(mp(0, s));
while(!pq.empty()){
int now = pq.top().S;
ll nd = pq.top().F;
pq.pop();
if(nd != d[now]) continue;
if(lst[now] != -1) tr[lst[now]] = true;
for(auto i : g[now]){
if(nd + i.c >= d[i.to]) continue;
d[i.to] = nd + i.c;
pq.push(mp(nd + i.c, i.to));
lst[i.to] = i.id;
}
}
};
eliminate(1);
eliminate(n);
auto calc = [&](int s, int t, int no){
vector<ll> d(n + 1, MAX);
d[s] = 0;
std::priority_queue<pll, vector<pll>, greater<>> pq;
pq.push(mp(0, s));
while(!pq.empty()){
int now = pq.top().S;
ll nd = pq.top().F;
pq.pop();
if(nd != d[now]) continue;
for(auto i : g[now]){
if(i.id == no) continue;
if(nd + i.c >= d[i.to]) continue;
d[i.to] = nd + i.c;
pq.push(mp(nd + i.c, i.to));
}
}
return d[t];
};
ll ans = dis[1][n] + dis[n][1];
for(int i = 1; i <= m; i++){
if(tr[i]){
g[e[i].to].eb(edge({e[i].to, e[i].from, e[i].c, e[i].d, -1}));
ans = min(ans, calc(1, n, i) + calc(n, 1, i) + e[i].d);
g[e[i].to].pob;
}
else{
ans = min(ans,
min(dis[1][n], dis[1][e[i].to] + dis[e[i].from][n] + e[i].c) +
min(dis[n][1], dis[n][e[i].to] + dis[e[i].from][1] + e[i].c) +
e[i].d);
//cerr << "test " << i << " " << ans << "\n";
}
}
if(ans >= MAX) ans = -1;
cout << ans << "\n";
return 0;
}
# | 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... |