This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
#define f(i,a,b) for(int i = a; i < b; i++)
const int N = 2005, inf = 1e9 + 10;
// B
int n, m, b[N], p[N], d[N];
vector <pair<int,int>> adj[N];
void dij(int x){
f(i,0,m) d[i] = inf;
d[x] = 0;
priority_queue <ii, vector <ii>, greater<ii>> q;
q.push({0, x});
while(!q.empty()){
ii p = q.top();
q.pop();
int d_v = p.first, v = p.second;
if(d_v != d[v]) continue;
for(auto wi: adj[v]){
int u = wi.first, w = wi.second;
if(d[u] > d[v] + w){
d[u] = d[v] + w;
q.push({d[u], u});
}
}
}
}
int main(){
cin >> n >> m;
f(i,0,m){
cin >> b[i] >> p[i];
}
f(i,0,m){
f(j,0,m){
int x = abs(b[i]-b[j]);
if(x == 0 or x%p[i]) continue;
adj[i].push_back({j, x/p[i]});
}
}
if(b[0] == b[1]){
cout << "0\n";
return 0;
}
dij(0);
cout << ((d[1] == inf) ? -1 : d[1]) << "\n";
return 0;
}
# | 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... |