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 first,second,d;
edge(int f,int s,int c):first(f),second(s),d(c){}
};
vector<edge> adj[201];
bool visited[201];
int n;
struct comp{
bool operator()(pair<int,int> a,pair<int,int> b){
return a.second==b.second ? a.first>b.first : a.second>b.second;
}
};
int dijkastra(){
priority_queue<pair<int,int>,vector<pair<int,int>>,comp> q;
int c = INF;
for(int i=1;i<=n;i++)visited[i]=false;
q.emplace(1,0);
while(!q.empty()){
auto curr = q.top();q.pop();
if(visited[curr.first])continue;
visited[curr.first]=true;
if(curr.first==n){
c = curr.second;
break;
}
for(auto&x:adj[curr.first])if(!visited[x.first])q.emplace(x.first,x.second+curr.second);
}
if(!visited[n])return INF;
for(int i=1;i<=n;i++)visited[i]=false;
q.emplace(n,0);
while(!q.empty()){
auto curr = q.top();q.pop();
if(visited[curr.first])continue;
visited[curr.first]=true;
if(curr.first==1){
c += curr.second;
break;
}
for(auto&x:adj[curr.first])if(!visited[x.first])q.emplace(x.first,x.second+curr.second);
}
if(!visited[1])return INF;
return c;
}
int32_t main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a,b,c,d;cin>>a>>b>>c>>d;
adj[a].emplace_back(b,c,d);
}
for(int i=1;i<=n;i++)adj[i].emplace_back(i,INF,INF);
int ans = dijkastra();
for(int i=1;i<=n;i++){
for(auto&x:adj[i]){
adj[x.first].emplace_back(i,x.second,x.d);
x.second=INF;
ans = min(ans,dijkastra()+x.d);
x.second=adj[x.first].back().second;
adj[x.first].erase(--adj[x.first].end());
}
}
cout << (ans>=INF ? -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... |