이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct edge{
int a,b,c,d;
};
const int mxn=205;
vector<tuple<int,int,int>> adj[2][mxn];
int n,m;
ll dist[5][mxn];
vector<int> from[4][mxn];
void dijk(int type,int rot,int st,int en){
dist[type][st]=0;
priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> pq;
pq.push({0,st});
while(!pq.empty()){
auto tmp=pq.top();
pq.pop();
ll len=tmp.f;
int v=tmp.s;
if(len!=dist[type][v]) continue;
for(auto u:adj[rot][v]){
int nxt,w,id;
tie(nxt,w,id)=u;
if(dist[type][v]+1ll*w<dist[type][nxt]){
dist[type][nxt]=dist[type][v]+1ll*w;
from[type][nxt]=from[type][v];
from[type][nxt].push_back(id);
pq.push({dist[type][nxt],nxt});
}
}
}
sort(all(from[type][en]));
}
ll dijk2(int type,int rot,int st,int en,int ban){
for(int j=1;j<=n;j++){
dist[type][j]=0x3f3f3f3f3f3f3f3f;
}
dist[type][st]=0;
priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> pq;
pq.push({0,st});
while(!pq.empty()){
auto tmp=pq.top();
pq.pop();
ll len=tmp.f;
int v=tmp.s;
if(len!=dist[type][v]) continue;
for(auto u:adj[rot][v]){
int nxt,w,id;
tie(nxt,w,id)=u;
if(id==ban) continue;
if(dist[type][v]+1ll*w<dist[type][nxt]){
dist[type][nxt]=dist[type][v]+1ll*w;
pq.push({dist[type][nxt],nxt});
}
}
}
return dist[type][en];
}
int main() {_
//setIO("wayne");
cin>>n>>m;
edge E[m];
for(int i=0;i<m;i++){
int a,b,c,d;
cin>>a>>b>>c>>d;
E[i]={a,b,c,d};
adj[0][a].push_back({b,c,i});
adj[1][b].push_back({a,c,i});
}
memset(dist,0x3f3f3f3f,sizeof(dist));
dijk(0,0,1,n);
dijk(1,1,1,n);
dijk(2,0,n,1);
dijk(3,1,n,1);
ll ans=dist[0][n]+dist[2][1];
for(int i=0;i<m;i++){
ll a,b,c,d;
a=E[i].a,b=E[i].b,c=E[i].c,d=E[i].d;
ll ans1;
ll ans2;
{
if(upper_bound(all(from[0][n]),i)-lower_bound(all(from[0][n]),i)>=1){
ans1=dijk2(4,0,1,n,i);
}
else{
ans1=min(dist[0][b]+c+dist[3][a],dist[0][n]);
}
}
{
if(upper_bound(all(from[2][1]),i)-lower_bound(all(from[2][1]),i)>=1){
ans2=dijk2(4,0,n,1,i);
}
else{
ans2=min(dist[2][b]+c+dist[1][a],dist[2][1]);
}
}
ans=min(ans,ans1+ans2+d);
}
cout<<(ans>=0x3f3f3f3f3f3f3f3f?-1:ans);
return 0;
}
//maybe its multiset not set
컴파일 시 표준 에러 (stderr) 메시지
ho_t4.cpp: In function 'void setIO(std::string)':
ho_t4.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |