This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3","unroll-loops","Ofast")
#include<iostream>
#include<vector>
#include<queue>
#include<set>
#define ll long long
using namespace std;
int n,m;
vector<bool> crit;
vector<pair<pair<int,int>,pair<ll,ll>>> edges;
void dijk(vector<vector<pair<pair<int,int>,pair<ll,ll>>>> &g,vector<ll> &dist,int s,int skip,bool flag){
dist[s]=0;
priority_queue<pair<int,ll>,vector<pair<int,ll>>,greater<pair<int,ll>>> pq;
vector<int> pre(n+1);
pq.push(make_pair(0,s));
while(!pq.empty()){
auto node=pq.top();
pq.pop();
if(dist[node.second]<node.first) continue;
for(auto x:g[node.second]){
if(x.first.second==skip) continue;
if(dist[x.first.first]>dist[node.second]+x.second.first){
if(flag) pre[x.first.first]=x.first.second;
dist[x.first.first]=dist[node.second]+x.second.first;
pq.push(make_pair(dist[x.first.first],x.first.first));
}
}
if(skip!=-1&&edges[skip].first.second==node.second){
if(dist[edges[skip].first.first]>dist[node.second]+edges[skip].second.first){
dist[edges[skip].first.first]=dist[node.second]+edges[skip].second.first;
pq.push(make_pair(dist[edges[skip].first.first],edges[skip].first.first));
}
}
}
if(flag){
for(int i=1;i<=n;i++){
if(i==s) continue;
crit[pre[i]]=1;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
ll a,b,c,d;
cin>>n>>m;
crit.resize(m);
vector<vector<pair<pair<int,int>,pair<ll,ll>>>> graph(n+1),hparg(n+1);
for(int i=0;i<m;i++){
cin>>a>>b>>c>>d;
edges.push_back(make_pair(make_pair(a,b),make_pair(c,d)));
graph[a].push_back(make_pair(make_pair(b,i),make_pair(c,d)));
hparg[b].push_back(make_pair(make_pair(a,i),make_pair(c,d)));
}
vector<ll> from1(n+1,1e18),fromn(n+1,1e18),to1(n+1,1e18),ton(n+1,1e18);
dijk(graph,from1,1,-1,1);
dijk(graph,fromn,n,-1,1);
dijk(hparg,to1,1,-1,1);
dijk(hparg,ton,n,-1,1);
ll ans=from1[n]+fromn[1];
for(int i=0;i<m;i++){
if(crit[i]){
vector<ll> tmp1(n+1,1e18),tmp2(n+1,1e18);
dijk(graph,tmp1,1,i,0);
dijk(graph,tmp2,n,i,0);
ans=min(ans,tmp1[n]+tmp2[1]+edges[i].second.second);
}
else{
ans=min(ans,min(from1[n],from1[edges[i].first.second]+edges[i].second.first+ton[edges[i].first.first])+min(fromn[1],fromn[edges[i].first.second]+edges[i].second.first+to1[edges[i].first.first])+edges[i].second.second);
}
}
if(ans>=1e18) cout<<-1;
else cout<<ans;
}
# | 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... |