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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define INF 1e9+7
#define all(x) x.begin(),x.end()
using namespace std;
using namespace __gnu_pbds;
using ll=long long;
using pii=pair<int,int>;
using ppi=pair<int,pii>;
using oset=tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>;
template<typename T>
void _print(vector<T> x) {cerr<<"{"; for(auto e:x) cerr<<e<<","; cerr<<"}";}
void _print(pii x) {cerr<<"{"<<x.first<<","<<x.second<<"}";}
template<typename T>
void _print(T x) {cerr<<x;}
void dbg() {cerr<<endl;}
template<typename Head,typename... Tail>
void dbg(Head H,Tail... T) {
_print(H);
if(sizeof...(T)) cerr<<",";
else cerr<<"\"]";
dbg(T...);
}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:[\"",dbg(__VA_ARGS__)
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mxn=2010;
int n,m;
int adjm[mxn][mxn];
vector<pii> adj[mxn];
vector<int> a[mxn];
int s,t;
void dijk() {
priority_queue<pii,vector<pii>,greater<pii>> pq;
vector<int> dist(n,INF);
dist[s]=0;
pq.push({dist[s],s});
while(!pq.empty()) {
pii cur=pq.top(); pq.pop();
int u=cur.second,d=cur.first;
for(auto vw:adj[u]) {
int v=vw.first,w=vw.second;
if(d+w<dist[v]) {
dist[v]=d+w;
pq.push({dist[v],v});
}
}
}
if(dist[t]==INF) cout<<"-1"<<endl;
else cout<<dist[t]<<endl;
exit(0);
}
int main () {
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>m;
for(int i=0;i<m;i++) {
int u,w; cin>>u>>w;
if(!i) s=u;
else if(i==1) t=u;
a[u].push_back(w);
}
for(int i=0;i<n;i++) {
sort(all(a[i]));
a[i].erase(unique(all(a[i])),end(a[i]));
}
memset(adjm,INF,sizeof(adjm));
for(int u=0;u<n;u++) {
for(int w:a[u]) {
for(int v=u-w,cnt=1;v>=0;v-=w,cnt++) {
adjm[u][v]=min(adjm[u][v],cnt);
}
for(int v=u+w,cnt=1;v<n;v+=w,cnt++) {
adjm[u][v]=min(adjm[u][v],cnt);
}
}
for(int v=0;v<n;v++) {
if(v==u) continue;
adj[u].push_back({v,adjm[u][v]});
}
}
dijk();
}
# | 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... |