이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define mem(a,i) memset(a,i,sizeof(a))
#define sz(s) (int)s.size()
#define int ll
#define y1 yy
#define ppb pop_back
#define gcd(a,b) __gcd(a,b)
#define in insert
const int dx[4]={-1,0,1,0};
const int dy[4]={0,-1,0,1};
const int inf=1e18;
const int N=5e4+100;
const int MAX=210;
const int mod=1e9+7;
int n,m;
vector<pair<int,pii>> g[MAX];
int d[MAX][N+N];
int d1[MAX][N+N];
void solve(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v,c,d;
cin>>u>>v>>c>>d;
g[u].pb({v,{c,i+m}});
g[v].pb({u,{c+d,i}});
}
{
for(int i=1;i<=n;i++){
for(int j=0;j<=m;j++){
d[i][j]=inf;
}
}
set<pair<int,pii>> st;
for(int j=0;j<=m;j++){
d[1][j]=0;
st.in({d[1][j],{1,j}});
}
while(!st.empty()){
int v=st.begin()->S.F;
int r=st.begin()->S.S;
st.erase(st.begin());
for(auto to:g[v]){
if(to.S.S>m){
if(to.S.S-m==r)continue;
if(d[to.F][r]>d[v][r]+to.S.F){
st.erase({d[to.F][r],{to.F,r}});
d[to.F][r]=d[v][r]+to.S.F;
st.in({d[to.F][r],{to.F,r}});
}
}
else{
if(to.S.S!=r)continue;
if(d[to.F][r]>d[v][r]+to.S.F){
st.erase({d[to.F][r],{to.F,r}});
d[to.F][r]=d[v][r]+to.S.F;
st.in({d[to.F][r],{to.F,r}});
}
}
}
}
}
{
for(int i=1;i<=n;i++){
for(int j=0;j<=m;j++){
d1[i][j]=inf;
}
}
set<pair<int,pii>> st;
for(int j=0;j<=m;j++){
d1[n][j]=0;
st.in({d1[n][j],{n,j}});
}
while(!st.empty()){
int v=st.begin()->S.F;
int r=st.begin()->S.S;
st.erase(st.begin());
for(auto to:g[v]){
if(to.S.S>m){
if(to.S.S-m==r)continue;
if(d1[to.F][r]>d1[v][r]+to.S.F){
st.erase({d1[to.F][r],{to.F,r}});
d1[to.F][r]=d1[v][r]+to.S.F;
st.in({d1[to.F][r],{to.F,r}});
}
}
else{
if(to.S.S!=r)continue;
if(d1[to.F][r]>d1[v][r]+to.S.F){
st.erase({d1[to.F][r],{to.F,r}});
d1[to.F][r]=d1[v][r]+to.S.F;
st.in({d1[to.F][r],{to.F,r}});
}
}
}
}
}
int ans=d[n][0]+d1[1][0];
for(int i=1;i<=m;i++){
ans=min(ans,d[n][i]+d1[1][i]);
}
if(ans<inf)cout<<ans<<"\n";
else cout<<-1<<"\n";
}
main(){
// freopen("prizes.in", "r", stdin);
// freopen("prizes.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
ho_t4.cpp:124:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
124 | main(){
| ^~~~
# | 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... |