Submission #423771

#TimeUsernameProblemLanguageResultExecution timeMemory
423771errorgornTreatment Project (JOI20_treatment)C++17
35 / 100
2268 ms524292 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ii pair<ll,ll>
#define fi first
#define se second
#define endl '\n'

#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back

#define rep(x,s,e) for (auto x=s-(s>e);x!=e-(s>e);s<e?x++:x--)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n,k;

struct E{
	int l,r;
	int t;
	int c;
};
vector<E> v;

ll memo[5005];
vector<int> al[5005];

bool proc[5005];
ll w[5005];

int main(){
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(false);
	
	cin>>n>>k;
	
	int a,b,c,d;
	rep(x,0,k){
		cin>>a>>b>>c>>d;
		v.pub({b,c+1,a,d});
	}
	
	rep(x,0,k){
		rep(y,0,k){
			if (v[x].t<=v[y].t && v[y].l+(v[y].t-v[x].t)<=v[x].r) al[x].pub(y);
			if (v[y].t<v[x].t && v[y].l+(v[x].t-v[y].t)<=v[x].r) al[x].pub(y);
		}
	}
	
	ll ans=1e18;
	
	memset(w,63,sizeof(w));
	rep(x,0,k) if (v[x].l==1) w[x]=0;
	
	rep(x,0,k){
		int curr=-1;
		rep(y,0,k) if (!proc[y] && (curr==-1 || w[curr]>w[y])) curr=y;
		
		//cout<<curr<<endl;
		
		proc[curr]=true;
		if (v[curr].r==n+1) ans=min(ans,w[curr]+v[curr].c);
		
		for (auto &it:al[curr]){
			if (w[it]>w[curr]+v[curr].c){
				w[it]=w[curr]+v[curr].c;
			}
		}
	}
	
	if (ans==1e18) cout<<"-1"<<endl;
	else cout<<ans<<endl;
	
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...