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;
#define ll long long int
#define sz(x) (int)x.size()
#define ff first
#define ss second
const ll N = 30002;
ll T, n, m, dis[N], c[N];
vector <int> v1;
pair<int,int> a[N];
set <int> v;
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> a[i].ff >> a[i].ss;
if(a[i].ff != a[0].ff){
v.insert(a[i].ff);
}
}
int ind = a[0].ff, p = a[1].ff;
sort (a,a+m);
c[a[0].ff] = 0;
for(int i = 1; i < m; i++){
if(a[i].ff != a[i-1].ff) c[a[i].ff] = i;
}
for(int i = 0; i <= n; i++) dis[i] = 1e9;
dis[ind] = 0;
for(int i = 0; i < m; i++){
if(a[i].ff == ind){
if(sz(v1) > 0 and a[i].ss == v1.back()) continue;
v1.push_back(a[i].ss);
}
}
while(sz(v) > 0){
int mn = 1e9, k = -1;
for(auto i : v1){
for(auto j : v){
if(dis[j] <= mn){
mn = dis[j];
k = j;
}
if(abs(j-ind) % i != 0) continue;
dis[j] = min(dis[j],dis[ind]+abs(j-ind)/i);
if(dis[j] <= mn){
mn = dis[j];
k = j;
}
}
}
v1.clear();
int x = c[k];
ind = k;
for(int i = x; i < m; i++){
if(a[i].ff == ind){
if(sz(v1) > 0 and a[i].ss == v1.back()) continue;
v1.push_back(a[i].ss);
}
else break;
}
v.erase(v.find(ind));
if(ind == p) break;
}
cout << dis[p];
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... |