#include<bits/stdc++.h>
using namespace std;
const int N=1500000009;
int a,b,c,d,e,i,j,ii,jj,zx,xc,C,D,A[50009],B[50009],dis[209][209],bo[50009],dis2[209][209],pas,z[209],zi,BO[50009],DIS[209][209],Adis[209][209];
pair <pair <int, int>, pair <int, int> > p[50009];
pair <int, int> P;
multiset <pair <pair <int, int>, int> > v[209],V[209];
multiset <pair <pair <int, int>, int> >::iterator it;
priority_queue <pair <int, int> > s;
void DJI(int q){
int h,qw,we,er;
for(h=1; h<=a; h++){
dis[q][h]=N;bo[h]=0;
}
while(s.size()) s.pop();
s.push({0,q});dis[q][q]=0;
while(s.size()){
while(s.size()){
P=s.top();
if(bo[P.second]==0) break;
s.pop();
}
if(s.size()==0) break;
qw=-P.first;we=P.second;bo[we]=1;
s.pop();
for(it=v[we].begin(); it!=v[we].end(); it++){
if(dis[q][(*it).first.first]>dis[q][we]+(*it).first.second){
dis[q][(*it).first.first]=dis[q][we]+(*it).first.second;
dis2[q][(*it).first.first]=(*it).second;
s.push({-dis[q][(*it).first.first],(*it).first.first});
}
}
}
}
void ADJI(int q){
int h,qw,we,er;
for(h=1; h<=a; h++){
Adis[q][h]=N;bo[h]=0;
}
while(s.size()) s.pop();
s.push({0,q});Adis[q][q]=0;
while(s.size()){
while(s.size()){
P=s.top();
if(bo[P.second]==0) break;
s.pop();
}
if(s.size()==0) break;
qw=-P.first;we=P.second;bo[we]=1;
s.pop();
for(it=V[we].begin(); it!=V[we].end(); it++){
if(Adis[q][(*it).first.first]>Adis[q][we]+(*it).first.second){
Adis[q][(*it).first.first]=Adis[q][we]+(*it).first.second;
s.push({-Adis[q][(*it).first.first],(*it).first.first});
}
}
}
}
//
void DJI2(int q){
int h,qw,we,er;
for(h=1; h<=a; h++){
DIS[q][h]=N;bo[h]=0;
}
while(s.size()) s.pop();
s.push({0,q});DIS[q][q]=0;
while(s.size()){
while(s.size()){
P=s.top();
if(bo[P.second]==0) break;
s.pop();
}
if(s.size()==0) break;
qw=-P.first;we=P.second;bo[we]=1;
s.pop();
for(it=v[we].begin(); it!=v[we].end(); it++){
if(DIS[q][(*it).first.first]>DIS[q][we]+(*it).first.second){
DIS[q][(*it).first.first]=DIS[q][we]+(*it).first.second;
s.push({-DIS[q][(*it).first.first],(*it).first.first});
}
}
}
}
//
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a>>b;
for(i=1; i<=b; i++){
cin>>c>>d>>C>>D;
p[i]={{c,d},{C,D}};
v[c].insert({{d,C},i});
V[d].insert({{c,C},i});
}
DJI(1);
DJI(a);
ADJI(1);
ADJI(a);
if(dis[1][a]!=N&&dis[a][1]!=N) pas=dis[1][a]+dis[a][1]; else pas=N;
if(1==0){
cout<<dis[1][a]<<" "<<dis[a][1]<<endl;
cout<<dis2[1][a]<<" "<<dis2[a][1]<<endl;
cout<<pas<<endl;
return 0;
}
for(i=1; i<=b; i++){
BO[i]=0;
}
c=a;zi=0;
while(c!=1&&c!=0){
zi++;z[zi]=dis2[1][c];
e=dis2[1][c];
if(p[e].first.first!=c){
c=p[e].first.first;
}else{
c=p[e].first.second;
}
}
for(i=1; i<=zi; i++) BO[z[i]]=1;
A[0]=dis[1][a];
for(i=1; i<=b; i++){
if(BO[i]==1) continue;
if(dis[1][p[i].first.second]!=N&&Adis[a][p[i].first.first]!=N) A[i]=dis[1][p[i].first.second]+p[i].second.first+Adis[a][p[i].first.first]; else A[i]=N;
A[i]=min(A[i],dis[1][a]);
}
for(i=1; i<=b; i++){
if(BO[i]==0) continue;
it=v[p[i].first.first].lower_bound({{p[i].first.second,p[i].second.first},i});
v[p[i].first.first].erase(it);
v[p[i].first.second].insert({{p[i].first.first,p[i].second.first},i});
DJI2(1);
A[i]=DIS[1][a];
it=v[p[i].first.second].lower_bound({{p[i].first.first,p[i].second.first},i});
v[p[i].first.second].erase(it);
v[p[i].first.first].insert({{p[i].first.second,p[i].second.first},i});
}
//
/*for(i=0; i<=b; i++){
cout<<A[i]<<"\n";
}*/
//
for(i=1; i<=b; i++){
BO[i]=0;
}
c=1;zi=0;
while(c!=a&&c!=0){
zi++;z[zi]=dis2[a][c];
e=dis2[a][c];
if(p[e].first.first!=c){
c=p[e].first.first;
}else{
c=p[e].first.second;
}
}
for(i=1; i<=zi; i++) BO[z[i]]=1;
B[0]=dis[a][1];
for(i=1; i<=b; i++){
if(BO[i]==1) continue;
if(dis[a][p[i].first.second]!=N&&Adis[1][p[i].first.first]!=N) B[i]=dis[a][p[i].first.second]+p[i].second.first+Adis[1][p[i].first.first]; else B[i]=N;
B[i]=min(B[i],dis[a][1]);
}
for(i=1; i<=b; i++){
if(BO[i]==0) continue;
it=v[p[i].first.first].lower_bound({{p[i].first.second,p[i].second.first},i});
v[p[i].first.first].erase(it);
v[p[i].first.second].insert({{p[i].first.first,p[i].second.first},i});
DJI2(a);
B[i]=DIS[a][1];
it=v[p[i].first.second].lower_bound({{p[i].first.first,p[i].second.first},i});
v[p[i].first.second].erase(it);
v[p[i].first.first].insert({{p[i].first.second,p[i].second.first},i});
}
//
/*for(i=0; i<=b; i++){
cout<<B[i]<<"\n";
}*/
//
for(i=0; i<=b; i++){
//pas=min(pas,A[i]+B[i]+p[i].second.second);
if(A[i]!=N&&B[i]!=N){
pas=min(pas,A[i]+B[i]+p[i].second.second);
}
}
if(pas!=N) cout<<pas; else cout<<-1;
return 0;
}
Compilation message
ho_t4.cpp: In function 'void DJI(int)':
ho_t4.cpp:11:8: warning: variable 'qw' set but not used [-Wunused-but-set-variable]
11 | int h,qw,we,er;
| ^~
ho_t4.cpp:11:14: warning: unused variable 'er' [-Wunused-variable]
11 | int h,qw,we,er;
| ^~
ho_t4.cpp: In function 'void ADJI(int)':
ho_t4.cpp:36:8: warning: variable 'qw' set but not used [-Wunused-but-set-variable]
36 | int h,qw,we,er;
| ^~
ho_t4.cpp:36:14: warning: unused variable 'er' [-Wunused-variable]
36 | int h,qw,we,er;
| ^~
ho_t4.cpp: In function 'void DJI2(int)':
ho_t4.cpp:61:8: warning: variable 'qw' set but not used [-Wunused-but-set-variable]
61 | int h,qw,we,er;
| ^~
ho_t4.cpp:61:14: warning: unused variable 'er' [-Wunused-variable]
61 | int h,qw,we,er;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
17 ms |
488 KB |
Output is correct |
11 |
Correct |
18 ms |
484 KB |
Output is correct |
12 |
Correct |
17 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
544 KB |
Output is correct |
16 |
Correct |
2 ms |
480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
7976 KB |
Output is correct |
2 |
Correct |
65 ms |
8964 KB |
Output is correct |
3 |
Correct |
70 ms |
9108 KB |
Output is correct |
4 |
Correct |
2 ms |
708 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
43 ms |
9152 KB |
Output is correct |
10 |
Correct |
63 ms |
9100 KB |
Output is correct |
11 |
Correct |
58 ms |
9160 KB |
Output is correct |
12 |
Correct |
55 ms |
9120 KB |
Output is correct |
13 |
Correct |
63 ms |
9064 KB |
Output is correct |
14 |
Correct |
57 ms |
9156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
42 ms |
6228 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
60 ms |
7900 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
53 ms |
7996 KB |
Output is correct |
9 |
Correct |
56 ms |
7996 KB |
Output is correct |
10 |
Correct |
64 ms |
7956 KB |
Output is correct |
11 |
Correct |
53 ms |
8816 KB |
Output is correct |
12 |
Correct |
73 ms |
8900 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
64 ms |
8832 KB |
Output is correct |
20 |
Correct |
53 ms |
8820 KB |
Output is correct |
21 |
Correct |
82 ms |
8924 KB |
Output is correct |
22 |
Correct |
71 ms |
8908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
17 ms |
488 KB |
Output is correct |
11 |
Correct |
18 ms |
484 KB |
Output is correct |
12 |
Correct |
17 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
544 KB |
Output is correct |
16 |
Correct |
2 ms |
480 KB |
Output is correct |
17 |
Correct |
62 ms |
7976 KB |
Output is correct |
18 |
Correct |
65 ms |
8964 KB |
Output is correct |
19 |
Correct |
70 ms |
9108 KB |
Output is correct |
20 |
Correct |
2 ms |
708 KB |
Output is correct |
21 |
Correct |
2 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
352 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
43 ms |
9152 KB |
Output is correct |
26 |
Correct |
63 ms |
9100 KB |
Output is correct |
27 |
Correct |
58 ms |
9160 KB |
Output is correct |
28 |
Correct |
55 ms |
9120 KB |
Output is correct |
29 |
Correct |
63 ms |
9064 KB |
Output is correct |
30 |
Correct |
57 ms |
9156 KB |
Output is correct |
31 |
Correct |
2 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
42 ms |
6228 KB |
Output is correct |
34 |
Correct |
0 ms |
332 KB |
Output is correct |
35 |
Correct |
60 ms |
7900 KB |
Output is correct |
36 |
Correct |
0 ms |
332 KB |
Output is correct |
37 |
Correct |
0 ms |
332 KB |
Output is correct |
38 |
Correct |
53 ms |
7996 KB |
Output is correct |
39 |
Correct |
56 ms |
7996 KB |
Output is correct |
40 |
Correct |
64 ms |
7956 KB |
Output is correct |
41 |
Correct |
53 ms |
8816 KB |
Output is correct |
42 |
Correct |
73 ms |
8900 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
64 ms |
8832 KB |
Output is correct |
50 |
Correct |
53 ms |
8820 KB |
Output is correct |
51 |
Correct |
82 ms |
8924 KB |
Output is correct |
52 |
Correct |
71 ms |
8908 KB |
Output is correct |
53 |
Correct |
67 ms |
8916 KB |
Output is correct |
54 |
Correct |
98 ms |
8916 KB |
Output is correct |
55 |
Correct |
88 ms |
8888 KB |
Output is correct |
56 |
Correct |
3 ms |
488 KB |
Output is correct |
57 |
Correct |
1 ms |
588 KB |
Output is correct |
58 |
Correct |
852 ms |
7308 KB |
Output is correct |
59 |
Correct |
506 ms |
7244 KB |
Output is correct |
60 |
Correct |
660 ms |
7332 KB |
Output is correct |
61 |
Correct |
501 ms |
7332 KB |
Output is correct |
62 |
Correct |
556 ms |
7292 KB |
Output is correct |
63 |
Correct |
624 ms |
7324 KB |
Output is correct |
64 |
Correct |
978 ms |
7484 KB |
Output is correct |
65 |
Correct |
885 ms |
7496 KB |
Output is correct |
66 |
Correct |
948 ms |
7480 KB |
Output is correct |
67 |
Correct |
44 ms |
7180 KB |
Output is correct |
68 |
Correct |
57 ms |
9132 KB |
Output is correct |
69 |
Correct |
51 ms |
9052 KB |
Output is correct |
70 |
Correct |
74 ms |
9124 KB |
Output is correct |
71 |
Correct |
71 ms |
9164 KB |
Output is correct |
72 |
Correct |
77 ms |
9272 KB |
Output is correct |
73 |
Correct |
83 ms |
9128 KB |
Output is correct |
74 |
Correct |
75 ms |
9140 KB |
Output is correct |