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;
typedef long long ll;
const int MX=5e4+5;
int N,M;
ll U[MX], V[MX], C[MX], D[MX];
pair<ll, ll> dist[205][205], DIST[205];
vector<array<ll,3>> adj[205];
vector<ll> edges[205], redges[205];
bool crucial[MX], rcrucial[MX];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
for(int i=0;i<205;i++)
for(int j=0;j<205;j++)
dist[i][j]=make_pair(2e9,2e9);
cin>>N>>M;
for(int i=0;i<M;i++) {
cin>>U[i]>>V[i]>>C[i]>>D[i];
adj[U[i]].push_back({V[i],C[i],i});
}
for(int s=1;s<=N;s++) {
priority_queue<pair<pair<ll,ll>,ll>, vector<pair<pair<ll,ll>,ll>>, greater<pair<pair<ll,ll>,ll>>> pq;
dist[s][s]=make_pair(0,0);
pq.push({dist[s][s],s});
while(!pq.empty()) {
auto [d,v]=pq.top(); pq.pop();
if(d>dist[s][v]) continue;
for(auto [u,w,id]:adj[v]) {
if(dist[s][u]>make_pair(d.first+w,d.second+1)) {
dist[s][u]=make_pair(d.first+w,d.second+1);
pq.push({dist[s][u],u});
}
}
}
}
for(int i=0;i<M;i++) {
int u=U[i],v=V[i],c=C[i];
if(make_pair(dist[1][u].first+dist[v][N].first+c,dist[1][u].second+dist[v][N].second+1)==dist[1][N]) {
edges[dist[1][u].second+1].push_back(i);
}
}
for(int i=0;i<M;i++) {
int u=U[i],v=V[i],c=C[i];
if(make_pair(dist[N][u].first+dist[v][1].first+c,dist[N][u].second+dist[v][1].second+1)==dist[N][1]) {
redges[dist[N][u].second+1].push_back(i);
}
}
for(int d=1;d<=N;d++) {
if(edges[d].size()==1) {
crucial[edges[d][0]]=1;
}
if(redges[d].size()==1) {
rcrucial[redges[d][0]]=1;
}
}
ll ans=2e9;
if(dist[1][N].first!=2e9 && dist[N][1].first!=2e9) ans=min(ans,dist[1][N].first+dist[N][1].first);
for(int i=0;i<M;i++) {
ll cost=2e9,rcost=2e9;
ll u=U[i],v=V[i],c=C[i],d=D[i];
if(crucial[i]) {
priority_queue<pair<pair<ll,ll>,ll>, vector<pair<pair<ll,ll>,ll>>, greater<pair<pair<ll,ll>,ll>>> pq;
for(int i=1;i<=N;i++) DIST[i]=make_pair(2e9,2e9);
DIST[1]=make_pair(0,0);
pq.push({DIST[1],1});
while(!pq.empty()) {
auto [d,v]=pq.top(); pq.pop();
if(d>DIST[v]) continue;
for(auto [u,w,id]:adj[v]) {
if(id==i) continue;
if(DIST[u]>make_pair(d.first+w,d.second+1)) {
DIST[u]=make_pair(d.first+w,d.second+1);
pq.push({DIST[u],u});
}
}
// new edge
if(v==V[i]) {
int u=U[i];
if(DIST[u]>make_pair(d.first+c,d.second+1)) {
DIST[u]=make_pair(d.first+c,d.second+1);
pq.push({DIST[u],u});
}
}
}
cost=min(cost,DIST[N].first);
} else {
cost=min(cost,dist[1][v].first+c+dist[u][N].first);
cost=min(cost,dist[1][N].first);
}
if(rcrucial[i]) {
priority_queue<pair<pair<ll,ll>,ll>, vector<pair<pair<ll,ll>,ll>>, greater<pair<pair<ll,ll>,ll>>> pq;
for(int i=1;i<=N;i++) DIST[i]=make_pair(2e9,2e9);
DIST[N]=make_pair(0,0);
pq.push({DIST[N],N});
while(!pq.empty()) {
auto [d,v]=pq.top(); pq.pop();
if(d>DIST[v]) continue;
for(auto [u,w,id]:adj[v]) {
if(id==i) continue;
if(DIST[u]>make_pair(d.first+w,d.second+1)) {
DIST[u]=make_pair(d.first+w,d.second+1);
pq.push({DIST[u],u});
}
}
// new edge
if(v==V[i]) {
int u=U[i];
if(DIST[u]>make_pair(d.first+c,d.second+1)) {
DIST[u]=make_pair(d.first+c,d.second+1);
pq.push({DIST[u],u});
}
}
}
rcost=min(rcost,DIST[1].first);
} else {
rcost=min(rcost,dist[N][v].first+c+dist[u][1].first);
rcost=min(rcost,dist[N][1].first);
}
ans=min(ans,d+cost+rcost);
}
cout<<(ans==2e9?-1: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... |