이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
vector<pair<int,int>> readj[201];
bool visited[50001];
int distFrom1[201];
int distFromN[201];
int distTo1[201];
int distToN[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(int i=1;i<=m;i++){
int u,v,c,d;
cin >> u >> v >> c >> d;
edges[i] = {u,v,c,d};
adj[u].emplace(v,c);
readj[v].emplace_back(u,c);
}
for(int&i:distFrom1)i=INF;
for(int&i:distFromN)i=INF;
for(int&i:distTo1)i=INF;
for(int&i:distToN)i=INF;
priority_queue<pair<int,int>> q;
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;
distFrom1[curr.second]=curr.first;
for(auto&i:adj[curr.second]){
if(!visited[i.first])q.emplace(-curr.first-i.second,i.first);
}
}
for(bool&i:visited)i=false;
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;
distFromN[curr.second]=curr.first;
for(auto&i:adj[curr.second]){
if(!visited[i.first])q.emplace(-curr.first-i.second,i.first);
}
}
for(bool&i:visited)i=false;
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;
distTo1[curr.second]=curr.first;
for(auto&i:readj[curr.second]){
if(!visited[i.first])q.emplace(-curr.first-i.second,i.first);
}
}
for(bool&i:visited)i=false;
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;
distToN[curr.second]=curr.first;
for(auto&i:readj[curr.second]){
if(!visited[i.first])q.emplace(-curr.first-i.second,i.first);
}
}
int baseans = dijkastra();
for(int i=1;i<=m;i++){
int to = min(distFrom1[n],distFrom1[edges[i].v]+distToN[edges[i].u]+edges[i].c);
int back = min(distFromN[1],distFromN[edges[i].v]+distTo1[edges[i].u]+edges[i].c);
if(to+back+edges[i].d<baseans){
adj[edges[i].u].erase({edges[i].v,edges[i].c});
adj[edges[i].v].emplace(edges[i].u,edges[i].c);
baseans = min(baseans,edges[i].d+dijkastra());
adj[edges[i].v].erase({edges[i].u,edges[i].c});
adj[edges[i].u].emplace(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... |