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;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
typedef long long ll;
typedef long double ld;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
typedef pair<ll,ll>pi; typedef pair<ll,pi>spi; typedef pair<pi,pi>dpi;
#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (50006)
ll n,m,dist[206][206],disty[206];
vector<ll>v[206];
struct edge{
ll a,b,c,d;
}E[MAXN];
ll sp(ll start,ll ep){
mmst(disty,-1);
priority_queue<pi,vector<pi>,greater<pi>> pq;
disty[start]=0, pq.emplace(disty[start],start);
while(pq.size()){
ll x=pq.top().s,d=pq.top().f;
pq.pop();
if(disty[x]^d)continue;
for(auto i:v[x]) if(E[i].a==x&&(disty[E[i].b]==-1||disty[E[i].b]>d+E[i].c)) {
disty[E[i].b]=d+E[i].c;
pq.emplace(disty[E[i].b],E[i].b);
}
}
return disty[ep] == -1 ? LLINF : disty[ep];
}
ll get_dist(){
return sp(1,n) + sp(n,1);
}
int main(){
FAST
cin>>n>>m;
FOR(i,1,n)FOR(j,1,n)dist[i][j]=(i==j)?0:LLINF;
FOR(i,1,m){
ll a,b,c,d;cin>>a>>b>>c>>d;
v[a].pb(i), v[b].pb(i);
E[i]={a,b,c,d};
dist[a][b]=min(dist[a][b],c);
}
FOR(k,1,n)FOR(i,1,n)FOR(j,1,n)dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
ll ans=min(LLINF,dist[1][n]+dist[n][1]);
vector<int> p; FOR(i,1,m)p.pb(i); shuffle(all(p),rng);
shuffle(all(p),rng);
for(auto i:p) if(min(dist[1][n],dist[1][E[i].b]+E[i].c+dist[E[i].a][n])+min(dist[n][1],dist[n][E[i].b]+E[i].c+dist[E[i].a][1])+E[i].d < ans) {
swap(E[i].a,E[i].b);
ans=min(ans,get_dist()+E[i].d);
swap(E[i].a,E[i].b);
}
if(ans>=LLINF)ans=-1;
cout<<ans<<'\n';
}
# | 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... |