Submission #231956

#TimeUsernameProblemLanguageResultExecution timeMemory
231956peijarJakarta Skyscrapers (APIO15_skyscraper)C++17
36 / 100
1096 ms19192 KiB
#include <bits/stdc++.h>
using namespace std;

#define SZ(v) ((int)(v).size())
using ll = long long;

const int MAX = 2000;

map<pair<int, int>, int> dis;
set<pair<int, int>> seen;

vector<int> types[MAX];

struct Etat
{
	int pos, last_type;

	int get() const
	{
		return dis[{pos, last_type}];
	}

};

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int nb_doge, len_route;
	cin >> len_route >> nb_doge;
	vector<pair<int, int> > doge(nb_doge);
	for (auto &[building, skip] : doge)
	{
		cin >> building >> skip;
		types[building].push_back(skip);
	}

	int target = doge[1].first;

	queue<Etat> pq;
	dis[{doge[0].first, doge[0].second}] = 0;
	pq.push({doge[0].first, doge[0].second});

	while (SZ(pq))
	{
		Etat cur = pq.front(); pq.pop();
		auto [pos, last_type] = cur;
		if (seen.count({pos, last_type})) continue;
		seen.insert({pos, last_type});

		if (pos == target)
		{
			cout << cur.get() << endl;
			return 0;
		}
		types[pos].push_back(last_type);
		for (auto type : types[pos])
		{
			if (pos >= type and !dis.count({pos-type, type}))
			{
				dis[{pos - type, type}] = 1 + cur.get();
				pq.push({pos - type, type});
			}
			if (pos + type < len_route and !dis.count({pos+type, type}))
			{
				dis[{pos+type, type}] = 1 + cur.get();
				pq.push({pos+type, type});
			}
		}
		types[pos].pop_back();
	}

	cout << -1 << 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...
#Verdict Execution timeMemoryGrader output
Fetching results...