This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=1e3+5;
ll dpl[nx], dpr[nx], n, m, a[nx], b[nx], c[nx], d[nx], ml, mr, res=1e17;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>m>>n;
for (int i=1; i<=m; i++) cin>>a[i]>>b[i]>>c[i]>>d[i];
for (int i=1; i<=m; i++)
{
dpl[i]=dpr[i]=ml=mr=1e17;
if (a[i]==1) dpl[i]=d[i], ml=0;
if (b[i]==n) dpr[i]=d[i], mr=0;
for (int j=1; j<i; j++)
{
if (a[i]<=c[j]&&c[j]<=b[i])
{
dpl[i]=min(dpl[i], dpl[j]+d[i]);
dpr[i]=min(dpr[i], dpr[j]+d[i]);
ml=min(ml, dpl[j]);
mr=min(mr, dpr[j]);
}
}
res=min(res, ml+mr+d[i]);
//cout<<i<<' '<<dpl[i]<<' '<<dpr[i]<<' '<<ml<<' '<<mr<<' '<<res<<'\n';
}
cout<<((res==1e17)?-1:res);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |