#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<long long 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+1LL*10000000);
if(v.S%n-v.S/n>=minp)
al[v.S].pb(v.S-v.S/n+1LL*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=100;
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*10000000LL);
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)*10000000LL);
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)*10000000LL);
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
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 |
1 |
Correct |
96 ms |
143356 KB |
Output is correct |
2 |
Correct |
86 ms |
143352 KB |
Output is correct |
3 |
Correct |
86 ms |
143352 KB |
Output is correct |
4 |
Correct |
88 ms |
143352 KB |
Output is correct |
5 |
Correct |
86 ms |
143352 KB |
Output is correct |
6 |
Correct |
86 ms |
143352 KB |
Output is correct |
7 |
Correct |
86 ms |
143352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
143352 KB |
Output is correct |
2 |
Correct |
98 ms |
143352 KB |
Output is correct |
3 |
Correct |
85 ms |
143480 KB |
Output is correct |
4 |
Correct |
85 ms |
143356 KB |
Output is correct |
5 |
Correct |
85 ms |
143352 KB |
Output is correct |
6 |
Correct |
86 ms |
143352 KB |
Output is correct |
7 |
Correct |
86 ms |
143480 KB |
Output is correct |
8 |
Correct |
86 ms |
143352 KB |
Output is correct |
9 |
Correct |
87 ms |
143352 KB |
Output is correct |
10 |
Correct |
86 ms |
143480 KB |
Output is correct |
11 |
Correct |
90 ms |
143736 KB |
Output is correct |
12 |
Correct |
88 ms |
143352 KB |
Output is correct |
13 |
Correct |
86 ms |
143352 KB |
Output is correct |
14 |
Correct |
89 ms |
143608 KB |
Output is correct |
15 |
Correct |
87 ms |
143736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
143352 KB |
Output is correct |
2 |
Correct |
88 ms |
143352 KB |
Output is correct |
3 |
Correct |
87 ms |
143352 KB |
Output is correct |
4 |
Correct |
86 ms |
143352 KB |
Output is correct |
5 |
Correct |
85 ms |
143352 KB |
Output is correct |
6 |
Correct |
85 ms |
143352 KB |
Output is correct |
7 |
Correct |
86 ms |
143352 KB |
Output is correct |
8 |
Correct |
85 ms |
143352 KB |
Output is correct |
9 |
Correct |
84 ms |
143352 KB |
Output is correct |
10 |
Correct |
86 ms |
143480 KB |
Output is correct |
11 |
Correct |
88 ms |
143608 KB |
Output is correct |
12 |
Correct |
99 ms |
143352 KB |
Output is correct |
13 |
Correct |
85 ms |
143356 KB |
Output is correct |
14 |
Correct |
86 ms |
143608 KB |
Output is correct |
15 |
Correct |
89 ms |
143736 KB |
Output is correct |
16 |
Correct |
85 ms |
143480 KB |
Output is correct |
17 |
Correct |
92 ms |
143992 KB |
Output is correct |
18 |
Correct |
87 ms |
143480 KB |
Output is correct |
19 |
Correct |
87 ms |
143352 KB |
Output is correct |
20 |
Correct |
88 ms |
143736 KB |
Output is correct |
21 |
Correct |
88 ms |
143480 KB |
Output is correct |
22 |
Correct |
85 ms |
143480 KB |
Output is correct |
23 |
Correct |
87 ms |
143608 KB |
Output is correct |
24 |
Correct |
90 ms |
143868 KB |
Output is correct |
25 |
Correct |
99 ms |
143736 KB |
Output is correct |
26 |
Correct |
88 ms |
143736 KB |
Output is correct |
27 |
Correct |
88 ms |
143608 KB |
Output is correct |
28 |
Correct |
93 ms |
144376 KB |
Output is correct |
29 |
Correct |
110 ms |
145016 KB |
Output is correct |
30 |
Correct |
92 ms |
143864 KB |
Output is correct |
31 |
Correct |
98 ms |
144236 KB |
Output is correct |
32 |
Correct |
102 ms |
144120 KB |
Output is correct |
33 |
Correct |
114 ms |
146680 KB |
Output is correct |
34 |
Correct |
118 ms |
147320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
143352 KB |
Output is correct |
2 |
Correct |
86 ms |
143480 KB |
Output is correct |
3 |
Correct |
85 ms |
143352 KB |
Output is correct |
4 |
Correct |
96 ms |
143352 KB |
Output is correct |
5 |
Correct |
94 ms |
143484 KB |
Output is correct |
6 |
Correct |
97 ms |
143352 KB |
Output is correct |
7 |
Correct |
86 ms |
143344 KB |
Output is correct |
8 |
Correct |
97 ms |
143316 KB |
Output is correct |
9 |
Correct |
90 ms |
143352 KB |
Output is correct |
10 |
Correct |
86 ms |
143440 KB |
Output is correct |
11 |
Correct |
100 ms |
143608 KB |
Output is correct |
12 |
Correct |
87 ms |
143352 KB |
Output is correct |
13 |
Correct |
87 ms |
143352 KB |
Output is correct |
14 |
Correct |
100 ms |
143608 KB |
Output is correct |
15 |
Correct |
91 ms |
143736 KB |
Output is correct |
16 |
Correct |
86 ms |
143480 KB |
Output is correct |
17 |
Correct |
94 ms |
143992 KB |
Output is correct |
18 |
Correct |
86 ms |
143464 KB |
Output is correct |
19 |
Correct |
85 ms |
143480 KB |
Output is correct |
20 |
Correct |
88 ms |
143736 KB |
Output is correct |
21 |
Correct |
88 ms |
143484 KB |
Output is correct |
22 |
Correct |
88 ms |
143480 KB |
Output is correct |
23 |
Correct |
88 ms |
143608 KB |
Output is correct |
24 |
Correct |
91 ms |
143864 KB |
Output is correct |
25 |
Correct |
97 ms |
143824 KB |
Output is correct |
26 |
Correct |
90 ms |
143608 KB |
Output is correct |
27 |
Correct |
89 ms |
143608 KB |
Output is correct |
28 |
Correct |
107 ms |
144376 KB |
Output is correct |
29 |
Correct |
99 ms |
145148 KB |
Output is correct |
30 |
Correct |
89 ms |
143736 KB |
Output is correct |
31 |
Correct |
106 ms |
144248 KB |
Output is correct |
32 |
Correct |
91 ms |
144120 KB |
Output is correct |
33 |
Correct |
112 ms |
146684 KB |
Output is correct |
34 |
Correct |
122 ms |
147320 KB |
Output is correct |
35 |
Correct |
134 ms |
147828 KB |
Output is correct |
36 |
Correct |
96 ms |
144248 KB |
Output is correct |
37 |
Correct |
160 ms |
149620 KB |
Output is correct |
38 |
Correct |
162 ms |
149724 KB |
Output is correct |
39 |
Correct |
161 ms |
149744 KB |
Output is correct |
40 |
Correct |
161 ms |
149620 KB |
Output is correct |
41 |
Correct |
163 ms |
149620 KB |
Output is correct |
42 |
Correct |
94 ms |
143992 KB |
Output is correct |
43 |
Correct |
93 ms |
143736 KB |
Output is correct |
44 |
Correct |
95 ms |
143916 KB |
Output is correct |
45 |
Correct |
156 ms |
151104 KB |
Output is correct |
46 |
Correct |
166 ms |
152008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
143356 KB |
Output is correct |
2 |
Correct |
85 ms |
143352 KB |
Output is correct |
3 |
Correct |
97 ms |
143352 KB |
Output is correct |
4 |
Correct |
88 ms |
143480 KB |
Output is correct |
5 |
Correct |
86 ms |
143352 KB |
Output is correct |
6 |
Correct |
101 ms |
143352 KB |
Output is correct |
7 |
Correct |
94 ms |
143480 KB |
Output is correct |
8 |
Correct |
87 ms |
143352 KB |
Output is correct |
9 |
Correct |
87 ms |
143352 KB |
Output is correct |
10 |
Correct |
86 ms |
143480 KB |
Output is correct |
11 |
Correct |
88 ms |
143736 KB |
Output is correct |
12 |
Correct |
95 ms |
143480 KB |
Output is correct |
13 |
Correct |
86 ms |
143352 KB |
Output is correct |
14 |
Correct |
91 ms |
143608 KB |
Output is correct |
15 |
Correct |
89 ms |
143736 KB |
Output is correct |
16 |
Correct |
86 ms |
143352 KB |
Output is correct |
17 |
Correct |
93 ms |
143992 KB |
Output is correct |
18 |
Correct |
86 ms |
143480 KB |
Output is correct |
19 |
Correct |
86 ms |
143352 KB |
Output is correct |
20 |
Correct |
88 ms |
143736 KB |
Output is correct |
21 |
Correct |
88 ms |
143480 KB |
Output is correct |
22 |
Correct |
88 ms |
143480 KB |
Output is correct |
23 |
Correct |
98 ms |
143608 KB |
Output is correct |
24 |
Correct |
90 ms |
143864 KB |
Output is correct |
25 |
Correct |
87 ms |
143736 KB |
Output is correct |
26 |
Correct |
88 ms |
143612 KB |
Output is correct |
27 |
Correct |
102 ms |
143608 KB |
Output is correct |
28 |
Correct |
96 ms |
144376 KB |
Output is correct |
29 |
Correct |
112 ms |
145016 KB |
Output is correct |
30 |
Correct |
101 ms |
143736 KB |
Output is correct |
31 |
Correct |
101 ms |
144252 KB |
Output is correct |
32 |
Correct |
93 ms |
144120 KB |
Output is correct |
33 |
Correct |
128 ms |
146680 KB |
Output is correct |
34 |
Correct |
119 ms |
147320 KB |
Output is correct |
35 |
Correct |
152 ms |
147868 KB |
Output is correct |
36 |
Correct |
108 ms |
144248 KB |
Output is correct |
37 |
Correct |
157 ms |
149748 KB |
Output is correct |
38 |
Correct |
158 ms |
149620 KB |
Output is correct |
39 |
Correct |
165 ms |
149748 KB |
Output is correct |
40 |
Correct |
158 ms |
149744 KB |
Output is correct |
41 |
Correct |
161 ms |
149624 KB |
Output is correct |
42 |
Correct |
102 ms |
143860 KB |
Output is correct |
43 |
Correct |
106 ms |
143864 KB |
Output is correct |
44 |
Correct |
94 ms |
144196 KB |
Output is correct |
45 |
Correct |
154 ms |
150912 KB |
Output is correct |
46 |
Correct |
175 ms |
151936 KB |
Output is correct |
47 |
Correct |
408 ms |
164940 KB |
Output is correct |
48 |
Correct |
149 ms |
146420 KB |
Output is correct |
49 |
Correct |
145 ms |
146164 KB |
Output is correct |
50 |
Correct |
113 ms |
145272 KB |
Output is correct |
51 |
Correct |
241 ms |
153588 KB |
Output is correct |
52 |
Correct |
252 ms |
154616 KB |
Output is correct |
53 |
Correct |
133 ms |
148724 KB |
Output is correct |
54 |
Correct |
86 ms |
123000 KB |
Output is correct |
55 |
Correct |
101 ms |
144120 KB |
Output is correct |
56 |
Correct |
129 ms |
149236 KB |
Output is correct |
57 |
Correct |
272 ms |
157816 KB |
Output is correct |
58 |
Correct |
113 ms |
145656 KB |
Output is correct |
59 |
Correct |
120 ms |
146684 KB |
Output is correct |
60 |
Correct |
131 ms |
146820 KB |
Output is correct |
61 |
Correct |
113 ms |
146564 KB |
Output is correct |
62 |
Correct |
269 ms |
160760 KB |
Output is correct |
63 |
Correct |
134 ms |
146568 KB |
Output is correct |
64 |
Correct |
126 ms |
146308 KB |
Output is correct |
65 |
Correct |
139 ms |
146432 KB |
Output is correct |
66 |
Correct |
813 ms |
228240 KB |
Output is correct |
67 |
Correct |
898 ms |
235392 KB |
Output is correct |