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<random>
using namespace std;
template<typename T> void _do(T x){cerr<<x<<"\n";}
template<typename T,typename ...U> void _do(T x,U ...y){cerr<<x<<", ";_do(y...);}
#define dbg(...) cerr<<#__VA_ARGS__<<" = ";_do(__VA_ARGS__);
#define float double
#define ss(n) fixed<<setprecision(n)
#define ll long long
#define int ll
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define ld long double
#define pb push_back
#define pii pair<int,int>
#define rep(i,a) for(int i=1;i<=a;i++)
#define rep0(i,a) for(int i=0;i<a;i++)
#define F first
#define S second
#define uni(a) a.resize(distance(a.begin(),unique(a.begin(),a.end())));
#define unisort(a) sort(a.begin(),a.end()); uni(a);
ll gcd(ll a,ll b){if(b==0) return a; return gcd(b,a%b);}
const ld pi=3.14159265358979323846264338327950288419716939931;
const int lar=3e18;
const int mol1=1e9+7;
const int mol2=998244353;
const int MX=3e5;
vector<pair<int,int>>node[205];
int dis[205][205];
void solve()
{
int n,m;
cin>>n>>m;
rep(i,n){
rep(j,n){
if(i==j) dis[i][j]=0;
else dis[i][j]=lar;
}
}
int u[m+1],v[m+1],c[m+1],d[m+1];
rep(i,m){
cin>>u[i]>>v[i]>>c[i]>>d[i];
node[u[i]].pb({v[i],c[i]});
dis[u[i]][v[i]]=min(dis[u[i]][v[i]],c[i]);
}
rep(i,n){
rep(j,n){
if(i==j) continue;
rep(k,n){
if(k==j) continue;
dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]);
}
}
}
int ans=dis[1][n]+dis[n][1];
rep(i,m){
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>now;
int x=u[i],y=v[i];
node[y].pb({x,c[i]+d[i]});
int dp1[n+1],dp2[n+1];
rep(j,n) dp1[j]=lar,dp2[j]=lar;
dp1[1]=0,dp2[n]=0;
now.push({0,1});
while(now.size()){
int v1=now.top().S;int dd=now.top().F;now.pop();
if(dp1[v1]!=dd) continue;
for(pair<int,int> g:node[v1]){
if(v1==u[i]&&g.F==v[i]) continue;
if(dp1[g.F]>dp1[v1]+g.S){
dp1[g.F]=dp1[v1]+g.S;
now.push({dp1[g.F],g.F});
}
}
}
now.push({0,n});
while(now.size()){
int v1=now.top().S;int dd=now.top().F;now.pop();
if(dp2[v1]!=dd) continue;
for(pair<int,int> g:node[v1]){
if(v1==u[i]&&g.F==v[i]) continue;
if(dp2[g.F]>dp2[v1]+g.S){
dp2[g.F]=dp2[v1]+g.S;
now.push({dp2[g.F],g.F});
}
}
}
node[y].pop_back();
ans=min(ans,dp1[n]+dp2[1]);
}
if(ans>=lar) cout<<-1<<"\n";
else cout<<ans<<"\n";
}
signed main()
{
IO
solve();
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... |