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
const int INF = 1e15;
struct edge{
int u,v,c,d;
};
edge edges[50001];
set<pair<int,int>> adj[201];
bool visited[50001];
int dist[201][201];
int n;
int dijkastra(){
priority_queue<pair<int,int>> q;
for(bool&i:visited)i=false;
int ans1 = INF;
q.emplace(0,1);
while(!q.empty()){
auto curr = q.top();q.pop();curr.first=-curr.first;
if(visited[curr.second])continue;
visited[curr.second]=true;
if(curr.second==n){ans1=curr.first;break;}
for(auto&i:adj[curr.second]){
if(!visited[i.first])q.emplace(-curr.first-i.second,i.first);
}
}
while(!q.empty())q.pop();
for(bool&i:visited)i=false;
int ans2 = INF;
q.emplace(0,n);
while(!q.empty()){
auto curr = q.top();q.pop();curr.first=-curr.first;
if(visited[curr.second])continue;
visited[curr.second]=true;
if(curr.second==1){ans2=curr.first;break;}
for(auto&i:adj[curr.second]){
if(!visited[i.first])q.emplace(-curr.first-i.second,i.first);
}
}
return ans1+ans2;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int m;
cin >> n >> m;
for(auto&i:dist)for(int&j:i)j=INF;
for(int i=1;i<=m;i++){
int u,v,c,d;
cin >> u >> v >> c >> d;
edges[i] = {u,v,c,d};
adj[u].insert(make_pair(edges[i].v,edges[i].c));
dist[u][v]=min(dist[u][v],c);
}
for(int i=1;i<=n;i++)dist[i][i]=0;
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
}
}
}
int baseans = dist[1][n]+dist[n][1];
for(int i=1;i<=m;i++){
int to = min(dist[1][n],dist[1][edges[i].v]+dist[edges[i].u][n]+edges[i].c);
int back = min(dist[n][1],dist[n][edges[i].v]+dist[edges[i].u][1]+edges[i].c);
if(to+back+edges[i].d<baseans){
adj[edges[i].u].erase(adj[edges[i].u].find(make_pair(edges[i].v,edges[i].c)));
adj[edges[i].v].insert(make_pair(edges[i].u,edges[i].c));
baseans = min(baseans,edges[i].d+dijkastra());
adj[edges[i].v].erase(adj[edges[i].v].find(make_pair(edges[i].u,edges[i].c)));
adj[edges[i].u].insert(make_pair(edges[i].v,edges[i].c));
}
}
cout << (baseans>=INF ? -1 : baseans) << '\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... |