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 F first
#define S second
#define FOR(i,a,b) for (auto i = (a); i <= (b); ++i)
#define NFOR(i,a,b) for(auto i = (a); i >= (b); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define mp(i,a) make_pair(i,a)
#define pb(a) push_back(a)
#define bit(x,b) (x&(1LL<<b))
typedef int lli;
typedef pair <lli,lli> ii;
typedef pair <lli,ii> iii;
typedef vector <lli> vi;
vector<int> al[5220000];
lli dis[5220000],n,rn;
map<ii,int> ma;
int minp=10000000, maxp=-1;
set<int> pset;
bool cmp(const ii& a,const ii& b)
{
return (mp(a.F,mp(a.S%n, a.S/n)) >
mp(b.F,mp(b.S%n, b.S/n)));
}
lli dij(lli so,lli t)
{
// priority_queue<ii,vector<ii>, decltype(&cmp)> s(&cmp);
priority_queue<ii,vector<ii>, greater<ii>> s;
// set<ii> s;
for(lli i=0;i<5220000;++i)
dis[i]=1000000000;
s.push(mp(0,so));
// s.insert(mp(0,so));
dis[so%10000000]=0;
while(s.size()!=0)
{
if(s.size()>rn*n+n)
exit(1);
ii v=(s.top());
s.pop();
// ii v=*(s.begin());
// s.erase(s.begin());
if(dis[v.S]!=v.F)
continue;
if((v.S)%n==t)
return(v.F);
dis[v.S]=v.F;
if(v.S<rn*n)
{
if(pset.find(v.S%n)!=pset.end())
al[v.S].pb(v.S%n+rn*n);
if(v.S%n+v.S/n<=maxp)
al[v.S].pb(v.S+v.S/n+1*10000000);
if(v.S%n-v.S/n>=minp)
al[v.S].pb(v.S-v.S/n+1*10000000);
}
for(lli i=0;i<al[v.S].size();++i)
{
if((al[v.S][i]%10000000)%n < minp or
(al[v.S][i]%10000000)%n > maxp) continue;
if(al[v.S][i]/10000000+v.F<dis[al[v.S][i]%10000000])
{
// s.erase(mp(dis[al[v.S][i]%10000000],al[v.S][i]%10000000));
dis[al[v.S][i]%10000000]=al[v.S][i]/10000000+v.F;
s.push(mp(dis[al[v.S][i]%10000000],al[v.S][i]%10000000));
// s.insert(mp(dis[al[v.S][i]%10000000],al[v.S][i]%10000000));
}
}
if(v.S<rn*n)
{
if(pset.find(v.S%n)!=pset.end())
al[v.S].pop_back();
if(v.S%n+v.S/n<=maxp)
al[v.S].pop_back();
if(v.S%n-v.S/n>=minp)
al[v.S].pop_back();
}
}
if(dis[t]==1000000000)
return(-1);
return(dis[t]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
lli test=1;
for(;test>0;--test)
{
lli m, from, to,t1=0;
vector<ii> input;
int g = -1;
cin>>n>>m;
rn=150;
for(lli i=0;i<m;++i) {
lli f,se;
cin>>f>>se;
minp = min(minp, f);
maxp = max(maxp, f);
pset.insert(f);
input.pb(mp(f,se));
}
for(lli i=0;i<m;++i)
{
lli f,se;
f = input[i].F;
se = input[i].S;
g = (g==-1)?se:__gcd(g, se);
if(i==0) from = f;
if(i==1) to = f;
if(ma.find(mp(f, se))==ma.end()) {
ma[mp(f, se)] = 1;
} else continue;
if(se<rn)
{
al[rn*n+f].pb((se)*n+f+0*10000000);
t1+=2;
}
if(se>=rn)
{
lli p=f+se;
while(p<=maxp)
{
if(pset.find(p) != pset.end())
al[rn*n+f].pb(rn*n+p+((p-f)/se)*10000000);
p+=se;
t1++;
}
p=f-se;
while(p>=minp)
{
if(pset.find(p) != pset.end())
al[rn*n+f].pb(rn*n+p+((f-p)/se)*10000000);
p-=se;
t1++;
}
}
}
if(abs(to-from)%g!=0) cout << -1 << endl;
else cout<<dij(rn*n+from,rn*n+to)<<endl;
}
return(0);
}
Compilation message (stderr)
skyscraper.cpp: In function 'lli dij(lli, lli)':
skyscraper.cpp:40:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(s.size()>rn*n+n)
~~~~~~~~^~~~~~~
skyscraper.cpp:60:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(lli i=0;i<al[v.S].size();++i)
~^~~~~~~~~~~~~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:144:37: warning: 'to' may be used uninitialized in this function [-Wmaybe-uninitialized]
else cout<<dij(rn*n+from,rn*n+to)<<endl;
^
skyscraper.cpp:144:37: warning: 'from' may be used uninitialized in this function [-Wmaybe-uninitialized]
# | 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... |