//#include<i_am_noob_orz>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int,int>
#define X first
#define Y second
#define mod ((ll)1e9+7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x)>0?(x):(-(x)))
#define F(n) Fi(i,n)
#define Fi(i,n) Fl(i,0,n)
#define Fl(i,l,n) for(int i=l;i<n;i++)
#define memres(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define bit_count(x) __builtin_popcountll((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define jimmy_is_kind false
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
//#define LOCAL
#ifdef LOCAL
#define bug(...) cerr<<"#"<<__LINE__<<' '<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios_base::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define bug(...)
#endif
int add(int a,int b){return(a+b>=mod?a+b-mod:a+b);}
int sub(int a,int b){return(a<b?a+mod-b:a-b);}
int po(int a,int b){
if(b==0)return 1;
if(b==1)return(a%mod);
int tem=po(a,b/2);
if(b&1)return(((tem*tem)%mod)*a)%mod;
else return(tem*tem)%mod;
}
int GCD(int a,int b){
int x=0;
int ra,rb;
while(a&&b){
if(((a&1)==0)&&((b&1)==0)){
a>>=1,b>>=1,x++;
}
else if((a^b)&1){
if(a&1)b>>=1;
else a>>=1;
}
else{
ra=abs(a-b),rb=min(a,b);
a=ra,b=rb;
}
}
return max(a,b)<<x;
}
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
int n,m,a[205],aa[205],b[205],c[205],cc[205],d[205];
vector<int> g[205];
vector<pair<pii,pii> > edge;
priority_queue<pii,vector<pii>,greater<pii> >pq;
bool ud[205],ued[50010];
int tab[205][205];
int fdmn(){
F(n)a[i]=1e15;
memres(ud);
a[0]=0;
F(n){
int p=-1,mn=1e16;
Fi(j,n)if(a[j]<mn&&!ud[j])p=j,mn=a[j];
//bug(p);
if(p==-1)break;
ud[p]=1;
Fi(j,n){
if(ud[j])continue;
a[j]=min(a[j],a[p]+tab[p][j]);
}
}
int ts=a[n-1];
//bug(ts);
F(n)a[i]=1e15;
memres(ud);
a[n-1]=0;
F(n){
int p=-1,mn=1e16;
Fi(j,n)if(a[j]<mn&&!ud[j])p=j,mn=a[j];
if(p==-1)break;
ud[p]=1;
Fi(j,n){
if(ud[j])continue;
a[j]=min(a[j],a[p]+tab[p][j]);
}
}
return ts+a[0];
}
signed main(){
IOS();
//freopen("01-13.txt","r",stdin);
cin>>n>>m;
F(m){
int v,u,c,d;
cin>>v>>u>>c>>d;
d+=c;
//if(d<1e7)bug(u,v,i,d);
--u,--v;
g[u].pb(i);
g[v].pb(i);
edge.pb({{v,u},{c,d}});
}
F(n)a[i]=b[i]=c[i]=d[i]=1e15;
memset(aa,-1,sizeof(aa));
memset(cc,-1,sizeof(cc));
memres(ud);
pq.push({0,-1});
while(!pq.empty()){
pii pt=pq.top();pq.pop();
int p=(pt.Y==-1?0:edge[pt.Y].X.Y);
if(ud[p])continue;
ud[p]=1;
a[p]=pt.X;
aa[p]=pt.Y;
if(p==n-1)continue;
for(int id:g[p]){
if(edge[id].X.X!=p)continue;
if(ud[edge[id].X.Y])continue;
pq.push({pt.X+edge[id].Y.X,id});
}
}
pq.push({0,0});
memres(ud);
while(!pq.empty()){
pii pt=pq.top();pq.pop();
if(ud[pt.Y])continue;
ud[pt.Y]=1;
b[pt.Y]=pt.X;
if(pt.Y==n-1)continue;
for(int id:g[pt.Y]){
if(edge[id].X.Y!=pt.Y)continue;
if(ud[edge[id].X.X])continue;
pq.push({pt.X+edge[id].Y.X,edge[id].X.X});
}
}
pq.push({0,-1});
memres(ud);
while(!pq.empty()){
pii pt=pq.top();pq.pop();
int p=(pt.Y==-1?n-1:edge[pt.Y].X.Y);
if(ud[p])continue;
ud[p]=1;
c[p]=pt.X;
//bug(p,pt.X);
cc[p]=pt.Y;
if(p==0)continue;
for(int id:g[p]){
if(edge[id].X.X!=p)continue;
if(ud[edge[id].X.Y])continue;
pq.push({pt.X+edge[id].Y.X,id});
}
}
pq.push({0,n-1});
memres(ud);
while(!pq.empty()){
pii pt=pq.top();pq.pop();
if(ud[pt.Y])continue;
ud[pt.Y]=1;
d[pt.Y]=pt.X;
if(pt.Y==0)continue;
for(int id:g[pt.Y]){
if(edge[id].X.Y!=pt.Y)continue;
if(ud[edge[id].X.X])continue;
pq.push({pt.X+edge[id].Y.X,edge[id].X.X});
}
}
int ans=a[n-1]+c[0];
//F(n)bug(a[i]);
//F(n)bug(c[i]);
int p=n-1;
vector<int> v;
F(n){
//bug(aa[i],cc[i]);
if(aa[i]!=-1)v.pb(aa[i]);
if(cc[i]!=-1)v.pb(cc[i]);
}
memres(ued);
//bug(ans);
for(int i:v)ued[i]=1;
F(sz(edge)){
if(ued[i])continue;
pair<pii,pii> tp=edge[i];
ans=min(ans,a[tp.X.Y]+tp.Y.Y+d[tp.X.X]+c[0]);
ans=min(ans,a[n-1]+c[tp.X.Y]+tp.Y.Y+b[tp.X.X]);
ans=min(ans,a[tp.X.Y]+tp.Y.Y+d[tp.X.X]+c[tp.X.Y]+tp.Y.X+b[tp.X.X]);
//bug(i,ans,tp.Y.Y,a[n-1],c[tp.X.Y],b[tp.X.X]);
//bug(i,ans,tp.Y.Y,a[tp.X.Y],d[tp.X.X],c[0]);
}
//bug(ans);
int cnt=0;
F(m){
if(!ued[i])continue;
bug(i);
Fi(j,n)Fi(k,n)tab[j][k]=1e15;
Fi(j,sz(edge)){
if(j==i)tab[edge[j].X.Y][edge[j].X.X]=min(tab[edge[j].X.Y][edge[j].X.X],edge[j].Y.Y);
else tab[edge[j].X.X][edge[j].X.Y]=min(tab[edge[j].X.X][edge[j].X.Y],edge[j].Y.X);
}
//Fi(j,n)Fi(k,n)bug(j,k,tab[j][k]);
ans=min(ans,fdmn());
//bug(ans);
}
//bug(sz(v));
cout<<(ans<1e15?ans:-1)<<endl;
return 0;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:192:6: warning: unused variable 'p' [-Wunused-variable]
192 | int p=n-1;
| ^
ho_t4.cpp:212:6: warning: unused variable 'cnt' [-Wunused-variable]
212 | int cnt=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
772 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
113 ms |
776 KB |
Output is correct |
4 |
Correct |
103 ms |
780 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
724 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
88 ms |
776 KB |
Output is correct |
11 |
Correct |
107 ms |
780 KB |
Output is correct |
12 |
Correct |
100 ms |
724 KB |
Output is correct |
13 |
Correct |
46 ms |
764 KB |
Output is correct |
14 |
Correct |
96 ms |
736 KB |
Output is correct |
15 |
Correct |
100 ms |
796 KB |
Output is correct |
16 |
Correct |
80 ms |
808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
217 ms |
4404 KB |
Output is correct |
2 |
Correct |
247 ms |
4376 KB |
Output is correct |
3 |
Correct |
217 ms |
4400 KB |
Output is correct |
4 |
Correct |
98 ms |
852 KB |
Output is correct |
5 |
Correct |
99 ms |
768 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
11 ms |
744 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
110 ms |
3924 KB |
Output is correct |
10 |
Correct |
106 ms |
5056 KB |
Output is correct |
11 |
Correct |
179 ms |
5548 KB |
Output is correct |
12 |
Correct |
206 ms |
5552 KB |
Output is correct |
13 |
Correct |
178 ms |
5572 KB |
Output is correct |
14 |
Correct |
165 ms |
5716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
788 KB |
Output is correct |
2 |
Correct |
25 ms |
728 KB |
Output is correct |
3 |
Correct |
122 ms |
3600 KB |
Output is correct |
4 |
Correct |
13 ms |
728 KB |
Output is correct |
5 |
Correct |
136 ms |
4340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
87 ms |
3824 KB |
Output is correct |
9 |
Correct |
108 ms |
4800 KB |
Output is correct |
10 |
Correct |
138 ms |
5208 KB |
Output is correct |
11 |
Correct |
122 ms |
5188 KB |
Output is correct |
12 |
Correct |
145 ms |
5040 KB |
Output is correct |
13 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
137 ms |
5464 KB |
Output is correct |
20 |
Correct |
138 ms |
5296 KB |
Output is correct |
21 |
Correct |
154 ms |
5352 KB |
Output is correct |
22 |
Correct |
135 ms |
5312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
772 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
113 ms |
776 KB |
Output is correct |
4 |
Correct |
103 ms |
780 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
724 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
88 ms |
776 KB |
Output is correct |
11 |
Correct |
107 ms |
780 KB |
Output is correct |
12 |
Correct |
100 ms |
724 KB |
Output is correct |
13 |
Correct |
46 ms |
764 KB |
Output is correct |
14 |
Correct |
96 ms |
736 KB |
Output is correct |
15 |
Correct |
100 ms |
796 KB |
Output is correct |
16 |
Correct |
80 ms |
808 KB |
Output is correct |
17 |
Correct |
217 ms |
4404 KB |
Output is correct |
18 |
Correct |
247 ms |
4376 KB |
Output is correct |
19 |
Correct |
217 ms |
4400 KB |
Output is correct |
20 |
Correct |
98 ms |
852 KB |
Output is correct |
21 |
Correct |
99 ms |
768 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
11 ms |
744 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
110 ms |
3924 KB |
Output is correct |
26 |
Correct |
106 ms |
5056 KB |
Output is correct |
27 |
Correct |
179 ms |
5548 KB |
Output is correct |
28 |
Correct |
206 ms |
5552 KB |
Output is correct |
29 |
Correct |
178 ms |
5572 KB |
Output is correct |
30 |
Correct |
165 ms |
5716 KB |
Output is correct |
31 |
Correct |
53 ms |
788 KB |
Output is correct |
32 |
Correct |
25 ms |
728 KB |
Output is correct |
33 |
Correct |
122 ms |
3600 KB |
Output is correct |
34 |
Correct |
13 ms |
728 KB |
Output is correct |
35 |
Correct |
136 ms |
4340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
87 ms |
3824 KB |
Output is correct |
39 |
Correct |
108 ms |
4800 KB |
Output is correct |
40 |
Correct |
138 ms |
5208 KB |
Output is correct |
41 |
Correct |
122 ms |
5188 KB |
Output is correct |
42 |
Correct |
145 ms |
5040 KB |
Output is correct |
43 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
137 ms |
5464 KB |
Output is correct |
50 |
Correct |
138 ms |
5296 KB |
Output is correct |
51 |
Correct |
154 ms |
5352 KB |
Output is correct |
52 |
Correct |
135 ms |
5312 KB |
Output is correct |
53 |
Correct |
205 ms |
5316 KB |
Output is correct |
54 |
Correct |
222 ms |
5308 KB |
Output is correct |
55 |
Correct |
231 ms |
5300 KB |
Output is correct |
56 |
Correct |
116 ms |
804 KB |
Output is correct |
57 |
Correct |
98 ms |
724 KB |
Output is correct |
58 |
Correct |
146 ms |
4168 KB |
Output is correct |
59 |
Correct |
167 ms |
4164 KB |
Output is correct |
60 |
Correct |
232 ms |
4140 KB |
Output is correct |
61 |
Correct |
141 ms |
4096 KB |
Output is correct |
62 |
Correct |
174 ms |
4180 KB |
Output is correct |
63 |
Correct |
214 ms |
4156 KB |
Output is correct |
64 |
Correct |
157 ms |
4552 KB |
Output is correct |
65 |
Correct |
183 ms |
4540 KB |
Output is correct |
66 |
Correct |
203 ms |
4484 KB |
Output is correct |
67 |
Correct |
29 ms |
5048 KB |
Output is correct |
68 |
Correct |
128 ms |
5016 KB |
Output is correct |
69 |
Correct |
110 ms |
5064 KB |
Output is correct |
70 |
Correct |
186 ms |
5460 KB |
Output is correct |
71 |
Correct |
220 ms |
5428 KB |
Output is correct |
72 |
Correct |
196 ms |
5424 KB |
Output is correct |
73 |
Correct |
194 ms |
5552 KB |
Output is correct |
74 |
Correct |
189 ms |
5476 KB |
Output is correct |