Submission #888205

#TimeUsernameProblemLanguageResultExecution timeMemory
888205UnforgettableplOlympic Bus (JOI20_ho_t4)C++17
0 / 100
1094 ms4148 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int INF = 1e12; 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]){ if(x.second==INF)continue; 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].back() = edge(0,INF,INF); } } cout << (ans>=INF ? -1 : ans) << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...