This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**------------------------------------------
---------Author: BePhuong--------------------
---------From: TK4-CHT ----------------------
---------Training To Win Voi 25 !!! ---------
---------------------------------------------
--------------------PhamVuPhuong2008<BeL>-**/
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define int long long
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii, ii>
#define base 341
#define MASK(i) (1ll << i)
#define oo 1e18
#define isOn(x,i) ((x) & MASK(i))
#define bitOn(x,i) ((x) | MASK(i))
#define bitOff(x,i) ((x) & ~MASK(i))
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
using namespace std;
//using namespace __gnu_pbds;
const int maxn = 5e4 + 5;
int n,m, dist[maxn], b[maxn];
vector<int> a[maxn];
void djk(int st) {
memset(dist, 0x3f, sizeof dist);
priority_queue<ii, vector<ii>, greater<ii>> q;
dist[st] = 0;
q.push({0, st});
while(!q.empty()) {
int cost = q.top().fi, z = q.top().se;
// int z = q.front();
q.pop();
if (cost > dist[z]) continue;
for (int cost : a[z]) {
for (int i = 1; z + i*cost < n; i++) {
if (dist[z + i*cost] > dist[z] + i) {
dist[z + i*cost] = dist[z] + i;
q.push({dist[z + i*cost], z + i*cost});
}
}
for (int i = 1; z - i*cost > 0; i++) {
if (dist[z - i*cost] > dist[z] + i) {
dist[z - i*cost] = dist[z] + i;
q.push({dist[z - i*cost], z - i*cost});
}
}
}
}
}
main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> b[i];
int x; cin >> x;
a[b[i]].pb(x);
}
// for (int i = 0; i < m; i++) cout << b[i] << endl;
djk(b[0]);
if (dist[b[1]] >= 1e15) cout << "-1";
else cout << dist[b[1]];
}
Compilation message (stderr)
skyscraper.cpp:58:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
58 | main() {
| ^~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |