Submission #26839

#TimeUsernameProblemLanguageResultExecution timeMemory
26839szawinisJakarta Skyscrapers (APIO15_skyscraper)C++14
36 / 100
46 ms17848 KiB
#include <bits/stdc++.h>
using namespace std;
const int MAX = (2e3)+1, INF = 1e9;

int n, m, b[MAX], p[MAX], d[MAX], g[MAX][MAX];
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m;
	fill(d+1, d+m, INF);
	for(int i = 0; i < m; i++) for(int j = 0; j < m; j++) if(i != j) g[i][j] = INF;
	for(int i = 0; i < m; i++) cin >> b[i] >> p[i];
	for(int i = 0; i < m; i++) for(int j = 0; j < m; j++) if(i != j) {
		if(abs(b[i] - b[j]) % p[i] == 0) g[i][j] = abs(b[i] - b[j]) / p[i];
	}
	unordered_set<int> st;
	for(int i = 0; i < m; i++) st.insert(i);
	while(!st.empty()) {
		int u = *st.begin();
		for(int v: st) if(d[v] < d[u]) u = v;
		st.erase(u);
		for(int v = 0; v < m; v++) if(d[u] + g[u][v] < d[v]) {
			d[v] = d[u] + g[u][v];
		}
	}
	cout << (d[1] == INF ? -1 : d[1]);
}
#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...