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 int long long
#define pii pair<int,int>
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define pb push_back
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
struct edge{
int a,b,c,d;
};
const int mxn=405;
vector<edge>e;
vector<int>temp;
pii w[mxn][mxn];
pii w2[mxn][mxn];
int id[mxn];
vector<bool>check(50005);
vector<int> dij(int st,int n,bool tf){
vector<int>dis(mxn);
for(int i=0;i<mxn;i++)dis[i]=1e15;
dis[st]=0;
if(tf){
for(int i=1;i<=n;i++){
id[i]=-1;
}
}
vector<bool>used(n+1,0);
for(int i=1;i<=n;i++){
int t=-1;
for(int j=1;j<=n;j++){
if(used[j])continue;
if(t==-1 or dis[j]<dis[t]){
t=j;
}
}
used[t]=1;
for(int j=1;j<=n;j++){
if(dis[t]+w[t][j].F<dis[j]){
dis[j]=dis[t]+w[t][j].F;
if(!tf)continue;
if(id[j]!=-1){
check[id[j]]=0;
continue;
}
id[j]=w[t][j].S;
check[id[j]]=1;
}
}
}
return dis;
}
vector<int> rdij(int st,int n,bool tf){
vector<int>dis(mxn);
for(int i=0;i<mxn;i++)dis[i]=1e15;
dis[st]=0;
if(tf){
for(int i=1;i<=n;i++){
id[i]=-1;
}
}
vector<bool>used(n+1,0);
for(int i=1;i<=n;i++){
int t=-1;
for(int j=1;j<=n;j++){
if(used[j])continue;
if(t==-1 or dis[j]<dis[t]){
t=j;
}
}
used[t]=1;
for(int j=1;j<=n;j++){
if(dis[t]+w[j][t].F<dis[j]){
dis[j]=dis[t]+w[j][t].F;
if(!tf)continue;
if(id[j]!=-1){
check[id[j]]=0;
continue;
}
id[j]=w[j][t].S;
check[id[j]]=1;
}
}
}
return dis;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
setio();
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
w[i][j]={1e15,1e15};
w2[i][j]={1e15,1e15};
}
}
for(int i=0;i<m;i++){
int a,b,c,d;
cin>>a>>b>>c>>d;
e.pb({a,b,c,d});
if(c<w[a][b].F or (c==w[a][b].F and d<e[w[a][b].S].d)){
w2[a][b]=w[a][b];
w[a][b]={c,i};
}
else if(c<w2[a][b].F or (c==w2[a][b].F and d<e[w2[a][b].S].d)){
w2[a][b]={c,i};
}
}
vector<int>dd=dij(1,n,1);
vector<int>dd2=rdij(1,n,1);
vector<int>rd2=rdij(n,n,1);
vector<int>rd=dij(n,n,1);
int ans=dd[n]+rd[1];
for(int i=0;i<m;i++){
//cout<<check[i]<<'\n';
auto [a,b,c,d]=e[i];
if(check[i]){
auto x=w[a][b];
if(w[a][b].S==i){
if(w2[a][b]!=make_pair((int)1e15,(int)1e15)){
w[a][b]=w2[a][b];
}
}
auto y=w[b][a];
w[b][a].F=min(w[b][a].F,c+d);
ans=min(ans,dij(1,n,0)[n]+dij(n,n,0)[1]);
w[a][b]=x;
w[b][a]=y;
}
else{
ans=min(ans,min(dd[b]+c+rd[a],dd[n])+min(rd[1],rd[b]+c+dd[a])+d);
}
//cout<<ans<<'\n';
}
if(ans>=1e15){
cout<<-1<<'\n';
return 0;
}
cout<<ans<<'\n';
}
/*
*/
Compilation message (stderr)
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:126:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
126 | auto [a,b,c,d]=e[i];
| ^
# | 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... |