#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define piii pair<pii,pii>
#define precise cout<<fixed<<setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int MAX=1<<19;
const int inf=(int)1e18+9;
int a[MAX];
int b[MAX];
int c[MAX];
int d[MAX];
int s1[MAX],s2[MAX];
int t[MAX*2];
map<int,int>compress;
set<int>pom;
int id(int x){
return compress[x];
}
int dp1[MAX],dp2[MAX];
void wypelnij(){
fill(t,t+MAX*2,inf);
}
void update(int u,int c){
u+=MAX;
t[u]=min(t[u],c);
for (;u>1;u>>=1){
t[u>>1]=min(t[u],t[u^1]);
}
}
int query(int u,int v){
int mini=inf;
for (u+=MAX,v+=MAX+1;u<v;u>>=1,v>>=1){
if (u&1)mini=min(mini,t[u++]);
if (v&1)mini=min(mini,t[--v]);
}
return mini;
}
int ans=inf;
int32_t main(){
BOOST;
int n,m;
cin>>m>>n;
pom.ins(1);
pom.ins(n);
for (int i=1;i<=m;i++){
cin>>a[i]>>b[i]>>c[i]>>d[i];
pom.ins(a[i]);
pom.ins(b[i]);
pom.ins(c[i]);
}
int cnt=1;
for (auto it:pom)compress[it]=cnt++;
wypelnij();
update(id(1),0);
for (int i=1;i<=m;i++){
s1[i]=min(inf,query(id(a[i]),id(b[i]))+d[i]);
update(id(c[i]),s1[i]);
}
wypelnij();
update(id(n),0);
for (int i=1;i<=m;i++){
s2[i]=min(inf,query(id(a[i]),id(b[i]))+d[i]);
update(id(c[i]),s2[i]);
if (s1[i]==inf || s2[i]==inf)continue;
ans=min(ans,s1[i]+s2[i]-d[i]);
}
if (ans==inf)ans=-1;
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
5 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8532 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
5 ms |
8532 KB |
Output is correct |
6 |
Correct |
6 ms |
8532 KB |
Output is correct |
7 |
Correct |
7 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
5 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8532 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
5 ms |
8532 KB |
Output is correct |
6 |
Correct |
6 ms |
8532 KB |
Output is correct |
7 |
Correct |
7 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8584 KB |
Output is correct |
9 |
Correct |
7 ms |
8532 KB |
Output is correct |
10 |
Correct |
7 ms |
8532 KB |
Output is correct |
11 |
Correct |
6 ms |
8628 KB |
Output is correct |
12 |
Correct |
5 ms |
8660 KB |
Output is correct |
13 |
Correct |
5 ms |
8532 KB |
Output is correct |
14 |
Correct |
5 ms |
8660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
5 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8532 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
5 ms |
8532 KB |
Output is correct |
6 |
Correct |
6 ms |
8532 KB |
Output is correct |
7 |
Correct |
7 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8584 KB |
Output is correct |
9 |
Correct |
7 ms |
8532 KB |
Output is correct |
10 |
Correct |
7 ms |
8532 KB |
Output is correct |
11 |
Correct |
6 ms |
8628 KB |
Output is correct |
12 |
Correct |
5 ms |
8660 KB |
Output is correct |
13 |
Correct |
5 ms |
8532 KB |
Output is correct |
14 |
Correct |
5 ms |
8660 KB |
Output is correct |
15 |
Correct |
5 ms |
8532 KB |
Output is correct |
16 |
Correct |
5 ms |
8660 KB |
Output is correct |
17 |
Correct |
9 ms |
8788 KB |
Output is correct |
18 |
Correct |
6 ms |
8668 KB |
Output is correct |
19 |
Correct |
10 ms |
8916 KB |
Output is correct |
20 |
Correct |
7 ms |
8776 KB |
Output is correct |
21 |
Correct |
6 ms |
8788 KB |
Output is correct |
22 |
Correct |
7 ms |
8916 KB |
Output is correct |
23 |
Correct |
7 ms |
8992 KB |
Output is correct |
24 |
Correct |
7 ms |
8916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
5 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8532 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
5 ms |
8532 KB |
Output is correct |
6 |
Correct |
6 ms |
8532 KB |
Output is correct |
7 |
Correct |
7 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8584 KB |
Output is correct |
9 |
Correct |
7 ms |
8532 KB |
Output is correct |
10 |
Correct |
7 ms |
8532 KB |
Output is correct |
11 |
Correct |
6 ms |
8628 KB |
Output is correct |
12 |
Correct |
5 ms |
8660 KB |
Output is correct |
13 |
Correct |
5 ms |
8532 KB |
Output is correct |
14 |
Correct |
5 ms |
8660 KB |
Output is correct |
15 |
Correct |
5 ms |
8532 KB |
Output is correct |
16 |
Correct |
5 ms |
8660 KB |
Output is correct |
17 |
Correct |
9 ms |
8788 KB |
Output is correct |
18 |
Correct |
6 ms |
8668 KB |
Output is correct |
19 |
Correct |
10 ms |
8916 KB |
Output is correct |
20 |
Correct |
7 ms |
8776 KB |
Output is correct |
21 |
Correct |
6 ms |
8788 KB |
Output is correct |
22 |
Correct |
7 ms |
8916 KB |
Output is correct |
23 |
Correct |
7 ms |
8992 KB |
Output is correct |
24 |
Correct |
7 ms |
8916 KB |
Output is correct |
25 |
Correct |
30 ms |
11164 KB |
Output is correct |
26 |
Correct |
89 ms |
16716 KB |
Output is correct |
27 |
Correct |
343 ms |
25512 KB |
Output is correct |
28 |
Correct |
159 ms |
17176 KB |
Output is correct |
29 |
Correct |
229 ms |
22852 KB |
Output is correct |
30 |
Correct |
243 ms |
19124 KB |
Output is correct |
31 |
Correct |
702 ms |
35516 KB |
Output is correct |
32 |
Correct |
528 ms |
30376 KB |
Output is correct |
33 |
Correct |
55 ms |
16556 KB |
Output is correct |
34 |
Correct |
309 ms |
29248 KB |
Output is correct |
35 |
Correct |
308 ms |
49916 KB |
Output is correct |
36 |
Correct |
884 ms |
49904 KB |
Output is correct |
37 |
Correct |
691 ms |
50092 KB |
Output is correct |
38 |
Correct |
614 ms |
49896 KB |
Output is correct |