#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
const ll INF = 1e18+10;
const int N = 1e5+5;
int m;
ll n,a[N],b[N],c[N],d[N],dpl[N],dpr[N],tree[4*N];
unordered_map<ll,int>mp;
vector<ll>v;
void resetTree(){
for(int i=0;i<4*N;i++)tree[i]=INF;
}
void Set(int c,ll ss,ll se,ll ind,ll val){
if(ss==se){
tree[c]=min(tree[c],val);
return;
}
ll mid=(ss+se)>>1LL;
if(ind<=mid)Set(c<<1,ss,mid,ind,val);
else Set(c<<1|1,mid+1,se,ind,val);
tree[c]=min(tree[c<<1],tree[c<<1|1]);
}
ll Get(int c,ll ss,ll se,ll qs,ll qe){
if(qs>se||qe<ss||ss>se)return INF;
if(qs<=ss&&se<=qe)return tree[c];
ll mid=(ss+se)>>1LL;
return min(Get(c<<1,ss,mid,qs,qe),Get(c<<1|1,mid+1,se,qs,qe));
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>m>>n;
for(int i=1;i<=m;i++){
cin>>a[i]>>b[i]>>c[i]>>d[i];
v.push_back(a[i]);
v.push_back(b[i]);
v.push_back(c[i]);
}
sort(v.begin(),v.end());
int cnt=0;
ll prev=-1;
for(auto e:v){
if(e!=prev)cnt++;
mp[e]=cnt;
prev=e;
}
ll ans=2LL*INF;
resetTree();
for(int i=1;i<=m;i++){
if(a[i]==1){
dpl[i]=d[i];
Set(1,1,cnt,mp[c[i]],dpl[i]);
}else{
dpl[i]=INF;
dpl[i]=min(dpl[i],d[i]+Get(1,1,cnt,mp[a[i]],mp[b[i]]));
Set(1,1,cnt,mp[c[i]],dpl[i]);
}
}
resetTree();
for(int i=1;i<=m;i++){
if(b[i]==n){
dpr[i]=d[i];
Set(1,1,cnt,mp[c[i]],dpr[i]);
}else{
dpr[i]=INF;
dpr[i]=min(dpr[i],d[i]+Get(1,1,cnt,mp[a[i]],mp[b[i]]));
Set(1,1,cnt,mp[c[i]],dpr[i]);
}
}
for(int i=1;i<=m;i++)ans=min(ans,dpl[i]+dpr[i]-d[i]);
if(ans==INF)ans=-1;
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3448 KB |
Output is correct |
2 |
Correct |
4 ms |
3572 KB |
Output is correct |
3 |
Correct |
5 ms |
3572 KB |
Output is correct |
4 |
Correct |
5 ms |
3608 KB |
Output is correct |
5 |
Correct |
4 ms |
3664 KB |
Output is correct |
6 |
Correct |
5 ms |
3712 KB |
Output is correct |
7 |
Correct |
4 ms |
3744 KB |
Output is correct |
8 |
Correct |
5 ms |
3820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3448 KB |
Output is correct |
2 |
Correct |
4 ms |
3572 KB |
Output is correct |
3 |
Correct |
5 ms |
3572 KB |
Output is correct |
4 |
Correct |
5 ms |
3608 KB |
Output is correct |
5 |
Correct |
4 ms |
3664 KB |
Output is correct |
6 |
Correct |
5 ms |
3712 KB |
Output is correct |
7 |
Correct |
4 ms |
3744 KB |
Output is correct |
8 |
Correct |
5 ms |
3820 KB |
Output is correct |
9 |
Correct |
5 ms |
3820 KB |
Output is correct |
10 |
Correct |
5 ms |
3820 KB |
Output is correct |
11 |
Correct |
5 ms |
3848 KB |
Output is correct |
12 |
Correct |
5 ms |
3848 KB |
Output is correct |
13 |
Correct |
5 ms |
3848 KB |
Output is correct |
14 |
Correct |
5 ms |
3848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3448 KB |
Output is correct |
2 |
Correct |
4 ms |
3572 KB |
Output is correct |
3 |
Correct |
5 ms |
3572 KB |
Output is correct |
4 |
Correct |
5 ms |
3608 KB |
Output is correct |
5 |
Correct |
4 ms |
3664 KB |
Output is correct |
6 |
Correct |
5 ms |
3712 KB |
Output is correct |
7 |
Correct |
4 ms |
3744 KB |
Output is correct |
8 |
Correct |
5 ms |
3820 KB |
Output is correct |
9 |
Correct |
5 ms |
3820 KB |
Output is correct |
10 |
Correct |
5 ms |
3820 KB |
Output is correct |
11 |
Correct |
5 ms |
3848 KB |
Output is correct |
12 |
Correct |
5 ms |
3848 KB |
Output is correct |
13 |
Correct |
5 ms |
3848 KB |
Output is correct |
14 |
Correct |
5 ms |
3848 KB |
Output is correct |
15 |
Correct |
6 ms |
3848 KB |
Output is correct |
16 |
Correct |
5 ms |
3848 KB |
Output is correct |
17 |
Correct |
9 ms |
3848 KB |
Output is correct |
18 |
Correct |
8 ms |
3848 KB |
Output is correct |
19 |
Correct |
7 ms |
3968 KB |
Output is correct |
20 |
Correct |
6 ms |
3968 KB |
Output is correct |
21 |
Correct |
5 ms |
3968 KB |
Output is correct |
22 |
Correct |
7 ms |
3968 KB |
Output is correct |
23 |
Correct |
7 ms |
3988 KB |
Output is correct |
24 |
Correct |
9 ms |
4052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3448 KB |
Output is correct |
2 |
Correct |
4 ms |
3572 KB |
Output is correct |
3 |
Correct |
5 ms |
3572 KB |
Output is correct |
4 |
Correct |
5 ms |
3608 KB |
Output is correct |
5 |
Correct |
4 ms |
3664 KB |
Output is correct |
6 |
Correct |
5 ms |
3712 KB |
Output is correct |
7 |
Correct |
4 ms |
3744 KB |
Output is correct |
8 |
Correct |
5 ms |
3820 KB |
Output is correct |
9 |
Correct |
5 ms |
3820 KB |
Output is correct |
10 |
Correct |
5 ms |
3820 KB |
Output is correct |
11 |
Correct |
5 ms |
3848 KB |
Output is correct |
12 |
Correct |
5 ms |
3848 KB |
Output is correct |
13 |
Correct |
5 ms |
3848 KB |
Output is correct |
14 |
Correct |
5 ms |
3848 KB |
Output is correct |
15 |
Correct |
6 ms |
3848 KB |
Output is correct |
16 |
Correct |
5 ms |
3848 KB |
Output is correct |
17 |
Correct |
9 ms |
3848 KB |
Output is correct |
18 |
Correct |
8 ms |
3848 KB |
Output is correct |
19 |
Correct |
7 ms |
3968 KB |
Output is correct |
20 |
Correct |
6 ms |
3968 KB |
Output is correct |
21 |
Correct |
5 ms |
3968 KB |
Output is correct |
22 |
Correct |
7 ms |
3968 KB |
Output is correct |
23 |
Correct |
7 ms |
3988 KB |
Output is correct |
24 |
Correct |
9 ms |
4052 KB |
Output is correct |
25 |
Correct |
29 ms |
5244 KB |
Output is correct |
26 |
Correct |
84 ms |
8476 KB |
Output is correct |
27 |
Correct |
260 ms |
12748 KB |
Output is correct |
28 |
Correct |
182 ms |
12748 KB |
Output is correct |
29 |
Correct |
172 ms |
12748 KB |
Output is correct |
30 |
Correct |
236 ms |
12748 KB |
Output is correct |
31 |
Incorrect |
480 ms |
17756 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |