This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Jakarta skysrcapers
#include <bits/stdc++.h>
#define INF MOD
#define MOD 100000007
#define MAX_N 30005
#define MAX_M 30005
int mg;
using namespace std;
int N, M;
int source, dest;
struct edge{
int d, w;
};
vector<int> soli[MAX_N];
int dist[MAX_N];
bool mark[MAX_N];
clock_t t;
bool legal;
void dijkstra(int s){
memset(dist, INF, sizeof(dist));
memset(mark, 0, sizeof(mark));
dist[s]=0;
priority_queue<pair<int, int> > pq;
pq.emplace(0, s);
while(!pq.empty()){
auto cur=pq.top();
pq.pop();
int v=cur.second;
if(mark[v])continue;
mark[v]=1;
int d=soli[v].size();
for(int j=0; j<d; ++j){
int p=soli[v][j];
int sk=v-p;
while(sk>=0 && sk>v-mg*p){
int w=(v-sk)/p;
if(!mark[sk] && dist[sk]>dist[v]+w && soli[sk].size()>0){
dist[sk]=dist[v]+w;
pq.emplace(-1*(dist[v]+w), sk);
}
sk-=p;
}
sk=v+p;
while(sk<N && sk<v+mg*p){
int w=(sk-v)/p;
if(!mark[sk] && dist[sk]>dist[v]+w && soli[sk].size()>0){
dist[sk]=dist[v]+w;
pq.emplace(-1*(dist[v]+w), sk);
}
sk+=p;
}
}
if(clock()-t>0.900*CLOCKS_PER_SEC){legal=0;return;}
}
}
inline bool cmp(int i, int j){return i>j;}
int main(){
ios::sync_with_stdio(false);
cin>>N>>M;
int b;
cin>>source>>b;
soli[source].push_back(b);
cin>>dest>>b;
soli[dest].push_back(b);
for(int i=2; i<M; ++i){
int p;
cin>>b>>p;
soli[b].push_back(p);
}t=clock();
for(int i=0; i<N; ++i){
sort(soli[i].begin(), soli[i].end(), cmp);
}
mg=5000;
dijkstra(source);
int output=dist[dest]>100000000?-1:dist[dest];
mg=30000;
legal=1;
dijkstra(source);
if(!legal){cout<<output;return 0;}
if(dist[dest]>100000000)cout<<"-1"<<endl;
else
cout<<dist[dest];
return 0;
}
/*
5 3
0 2
1 1
4 1
*/
/*
4 4
0 1 1
1 2 2
2 3 1
1 3 4
0 3
5 6
0 1 1
1 2 2
2 3 1
1 3 4
0 4 2
4 3 1
0 3
3
3 2
1 2 5
2 3 7
1 3
3 3
1 2 4
1 3 7
2 3 1
1 3
3 1
1 2 4
1 3
*/
# | 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... |