#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define pb push_back
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
struct edge{
int a,b,c,d;
};
const int mxn=405;
const int inf=1e15;
vector<edge>e;
int from[5][mxn];
int dis[5][mxn];
vector<pii>adj[2][mxn];
vector<pii>adj2[mxn];
bool used[500005];
int n;
void dij(int st,int p,int type){
for(int i=0;i<mxn;i++){
dis[p][i]=inf;
from[p][i]=-1;
}
dis[p][st]=0;
priority_queue<pii,vector<pii>,greater<pii>>pq;
pq.push({0,st});
while(sz(pq)){
auto [dist,v]=pq.top();
pq.pop();
if(dist>dis[p][v])continue;
if(from[p][v]!=-1)used[from[p][v]]=1;
for(auto [u,id]:adj[type][v]){
int w=e[id].c;
if(dist+w<dis[p][u]){
dis[p][u]=dist+w;
pq.push({dis[p][u],u});
from[p][u]=id;
}
}
}
}
int dij2(int st,int ed,int p,int i){
for(int i=0;i<mxn;i++){
dis[p][i]=inf;
from[p][i]=inf;
}
dis[p][st]=0;
priority_queue<pii,vector<pii>,greater<pii>>pq;
pq.push({0,st});
while(sz(pq)){
auto [dist,v]=pq.top();
pq.pop();
if(dist>dis[p][v])continue;
for(auto [u,id]:adj2[v]){
if(id==i and e[id].a==v)continue;
int w=e[id].c;
if(dist+w<dis[p][u]){
dis[p][u]=dist+w;
pq.push({dis[p][u],u});
}
}
}
return dis[p][ed];
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
setio();
int m;
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b,c,d;
cin>>a>>b>>c>>d;
e.pb({a,b,c,d});
adj[0][a].pb({b,i});
adj2[a].pb({b,i});
adj[1][b].pb({a,i});
}
dij(1,0,0);
dij(1,1,1);
dij(n,2,0);
dij(n,3,1);
int ans=dis[0][n]+dis[2][1];
for(int i=0;i<m;i++){
auto [a,b,c,d]=e[i];
if(used[i]){
e[i].c+=d;
adj2[b].pb({a,i});
ans=min(ans,dij2(1,n,4,i)+dij2(n,1,4,i));
adj2[b].pop_back();
e[i].c-=d;
}
else{
ans=min(ans,min(dis[0][n],dis[0][b]+c+dis[3][a])+min(dis[2][1],dis[2][b]+dis[1][a]+c)+d);
}
}
if(ans>=(int)1e15){
cout<<-1<<'\n';
return 0;
}
cout<<ans<<'\n';
}
/*
*/
Compilation message
ho_t4.cpp: In function 'void dij(long long int, long long int, long long int)':
ho_t4.cpp:38:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
38 | auto [dist,v]=pq.top();
| ^
ho_t4.cpp:42:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
42 | for(auto [u,id]:adj[type][v]){
| ^
ho_t4.cpp: In function 'long long int dij2(long long int, long long int, long long int, long long int)':
ho_t4.cpp:61:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
61 | auto [dist,v]=pq.top();
| ^
ho_t4.cpp:64:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
64 | for(auto [u,id]:adj2[v]){
| ^
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:95:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
95 | auto [a,b,c,d]=e[i];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
18 ms |
644 KB |
Output is correct |
4 |
Correct |
22 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
14 ms |
652 KB |
Output is correct |
11 |
Correct |
17 ms |
604 KB |
Output is correct |
12 |
Correct |
20 ms |
648 KB |
Output is correct |
13 |
Correct |
4 ms |
604 KB |
Output is correct |
14 |
Correct |
10 ms |
512 KB |
Output is correct |
15 |
Correct |
9 ms |
604 KB |
Output is correct |
16 |
Correct |
10 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
185 ms |
5836 KB |
Output is correct |
2 |
Correct |
197 ms |
6120 KB |
Output is correct |
3 |
Correct |
195 ms |
5832 KB |
Output is correct |
4 |
Correct |
16 ms |
600 KB |
Output is correct |
5 |
Correct |
7 ms |
856 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
352 KB |
Output is correct |
9 |
Correct |
70 ms |
7244 KB |
Output is correct |
10 |
Correct |
53 ms |
7116 KB |
Output is correct |
11 |
Correct |
134 ms |
6988 KB |
Output is correct |
12 |
Correct |
122 ms |
7152 KB |
Output is correct |
13 |
Correct |
115 ms |
7112 KB |
Output is correct |
14 |
Correct |
129 ms |
7616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
154 ms |
5072 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
191 ms |
5888 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
61 ms |
5964 KB |
Output is correct |
9 |
Correct |
66 ms |
6604 KB |
Output is correct |
10 |
Correct |
111 ms |
6604 KB |
Output is correct |
11 |
Correct |
125 ms |
7096 KB |
Output is correct |
12 |
Correct |
129 ms |
7108 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
376 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
128 ms |
7072 KB |
Output is correct |
20 |
Correct |
123 ms |
6796 KB |
Output is correct |
21 |
Correct |
119 ms |
6820 KB |
Output is correct |
22 |
Correct |
121 ms |
6856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
18 ms |
644 KB |
Output is correct |
4 |
Correct |
22 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
14 ms |
652 KB |
Output is correct |
11 |
Correct |
17 ms |
604 KB |
Output is correct |
12 |
Correct |
20 ms |
648 KB |
Output is correct |
13 |
Correct |
4 ms |
604 KB |
Output is correct |
14 |
Correct |
10 ms |
512 KB |
Output is correct |
15 |
Correct |
9 ms |
604 KB |
Output is correct |
16 |
Correct |
10 ms |
604 KB |
Output is correct |
17 |
Correct |
185 ms |
5836 KB |
Output is correct |
18 |
Correct |
197 ms |
6120 KB |
Output is correct |
19 |
Correct |
195 ms |
5832 KB |
Output is correct |
20 |
Correct |
16 ms |
600 KB |
Output is correct |
21 |
Correct |
7 ms |
856 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
352 KB |
Output is correct |
25 |
Correct |
70 ms |
7244 KB |
Output is correct |
26 |
Correct |
53 ms |
7116 KB |
Output is correct |
27 |
Correct |
134 ms |
6988 KB |
Output is correct |
28 |
Correct |
122 ms |
7152 KB |
Output is correct |
29 |
Correct |
115 ms |
7112 KB |
Output is correct |
30 |
Correct |
129 ms |
7616 KB |
Output is correct |
31 |
Correct |
9 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
154 ms |
5072 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
191 ms |
5888 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
61 ms |
5964 KB |
Output is correct |
39 |
Correct |
66 ms |
6604 KB |
Output is correct |
40 |
Correct |
111 ms |
6604 KB |
Output is correct |
41 |
Correct |
125 ms |
7096 KB |
Output is correct |
42 |
Correct |
129 ms |
7108 KB |
Output is correct |
43 |
Correct |
1 ms |
344 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
376 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
128 ms |
7072 KB |
Output is correct |
50 |
Correct |
123 ms |
6796 KB |
Output is correct |
51 |
Correct |
119 ms |
6820 KB |
Output is correct |
52 |
Correct |
121 ms |
6856 KB |
Output is correct |
53 |
Correct |
207 ms |
6824 KB |
Output is correct |
54 |
Correct |
213 ms |
6856 KB |
Output is correct |
55 |
Correct |
192 ms |
6736 KB |
Output is correct |
56 |
Correct |
22 ms |
672 KB |
Output is correct |
57 |
Correct |
19 ms |
604 KB |
Output is correct |
58 |
Correct |
88 ms |
5836 KB |
Output is correct |
59 |
Correct |
103 ms |
5840 KB |
Output is correct |
60 |
Correct |
159 ms |
5836 KB |
Output is correct |
61 |
Correct |
90 ms |
5836 KB |
Output is correct |
62 |
Correct |
98 ms |
5840 KB |
Output is correct |
63 |
Correct |
136 ms |
5832 KB |
Output is correct |
64 |
Correct |
558 ms |
6036 KB |
Output is correct |
65 |
Correct |
592 ms |
6092 KB |
Output is correct |
66 |
Correct |
781 ms |
6048 KB |
Output is correct |
67 |
Correct |
14 ms |
5832 KB |
Output is correct |
68 |
Correct |
65 ms |
7100 KB |
Output is correct |
69 |
Correct |
58 ms |
7116 KB |
Output is correct |
70 |
Correct |
140 ms |
7060 KB |
Output is correct |
71 |
Correct |
135 ms |
6904 KB |
Output is correct |
72 |
Correct |
141 ms |
7116 KB |
Output is correct |
73 |
Correct |
150 ms |
7296 KB |
Output is correct |
74 |
Correct |
136 ms |
7112 KB |
Output is correct |