제출 #108270

#제출 시각아이디문제언어결과실행 시간메모리
108270kekJakarta Skyscrapers (APIO15_skyscraper)C++17
0 / 100
3 ms512 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int ll
#define all(v) v.begin(), v.end()
#define len(v) ((int)(v).size())
#define pb push_back
#define kek pop_back
#define pii pair<int, int>
#define mp make_pair

#define debug(x) cout << #x << " = " << x << endl;

const int INF = 1e18 + 666;

template<class t1, class t2>
bool cmin(t1 &a, const t2 &b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}

template<class t1, class t2>
bool cmax(t1 &a, const t2 &b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

#ifndef LOCAL
void UseFiles(const string &s) {
	freopen((s + ".in").c_str(), "r", stdin);
	freopen((s + ".out").c_str(), "w", stdout);
}
#else
void UseFiles(const string&) {}
#endif

void run();

signed main() {
	// UseFiles("cowboys");
	iostream::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	run();
}

void run() {
	int n, m;
	cin >> n >> m;
	vector<pii> doges(m);
	for (auto &x : doges) {
		cin >> x.first >> x.second;
	}
	vector<vector<int>> who(n);
	for (int i = 0; i < m; ++i) {
		who[doges[i].first].pb(i);
	}
	vector<int> active(n, 0);
	vector<vector<int>> dist(n, vector<int>(m, INF));
	queue<pii> q;
	auto set_active = [&who, &active, &dist, &q](int v, int d) {
		if (active[v]) {
			return;
		}
		active[v] = 1;
		for (auto &x : who[v]) {
			dist[v][x] = d;
			q.push({v, x});
		}
	};
	set_active(doges[0].first, 0);
	while (len(q)) {
		int d, c;
		tie(c, d) = q.front();
		q.pop();
		if (c - doges[d].second >= 0) {
			if (cmin(dist[c - doges[d].second][d], dist[c][d] + 1)) {
				q.push({c - doges[d].second, d});
			}
			set_active(c - doges[d].second, dist[c][d] + 1);
		}
		if (c + doges[d].second < n) {
			if (cmin(dist[c + doges[d].second][d], dist[c][d] + 1)) {
				q.push({c + doges[d].second, d});
			}
			set_active(c + doges[d].second, dist[c][d] + 1);
		}
	}
	int mn = INF;
	for (int i = 0; i < m; ++i) {
		cmin(mn, dist[doges[1].second][i]);
	}
	if (mn == INF) {
		cout << -1 << endl;
	} else {
		cout << mn << endl;
	}
}

컴파일 시 표준 에러 (stderr) 메시지

skyscraper.cpp: In function 'void UseFiles(const string&)':
skyscraper.cpp:40:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  freopen((s + ".in").c_str(), "r", stdin);
  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:41:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  freopen((s + ".out").c_str(), "w", stdout);
  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...