#include <bits/stdc++.h>
using namespace std;
int n,p[30004],b[30004],m;
priority_queue <pair<int,int> ,vector <pair<int,int> >,greater <pair<int,int> > > pq;
bool vis[30004];
vector <int> v[30004];
int cur[30004];
void dij()
{
for (int i=1;i<n;i++)
cur[i]=INT_MAX;
pq.push({0,0});
while (!pq.empty())
{
int x= (pq.top()).second;
int y= (pq.top()).first;
pq.pop();
if (vis[x])
continue;
vis[x]=true;
for (int i=1;i*p[x]+b[x]<n;i++)
{
int a= (i*p[x]+b[x]);
for (int j=0;j<v[a].size();j++)
{
if (cur[v[a][j]]>i+y)
{
pq.push({i+y,v[a][j]});
cur[v[a][j]]=i+y;
}
}
}
for (int i=1;b[x]-i*p[x]>=0;i++)
{
int a= (b[x]-i*p[x]);
for (int j=0;j<v[a].size();j++)
{
if (cur[v[a][j]]>i+y)
{
pq.push({i+y,v[a][j]});
cur[v[a][j]]=i+y;
}
}
}
}
}
int main()
{
cin >>n>>m;
for (int i=0;i<m;i++)
{
cin >>b[i]>>p[i];
v[b[i]].push_back(i);
}
dij();
if (cur[1]==INT_MAX)
cout <<-1;
else
cout <<cur[1];
return 0;
}
Compilation message
skyscraper.cpp: In function 'void dij()':
skyscraper.cpp:24:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j=0;j<v[a].size();j++)
^
skyscraper.cpp:36:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j=0;j<v[a].size();j++)
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1016 KB |
Output is correct |
2 |
Correct |
2 ms |
1016 KB |
Output is correct |
3 |
Incorrect |
2 ms |
1048 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1100 KB |
Output is correct |
2 |
Correct |
2 ms |
1228 KB |
Output is correct |
3 |
Incorrect |
2 ms |
1228 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1228 KB |
Output is correct |
2 |
Correct |
2 ms |
1228 KB |
Output is correct |
3 |
Incorrect |
2 ms |
1228 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1228 KB |
Output is correct |
2 |
Correct |
2 ms |
1300 KB |
Output is correct |
3 |
Incorrect |
2 ms |
1372 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1372 KB |
Output is correct |
2 |
Correct |
2 ms |
1372 KB |
Output is correct |
3 |
Incorrect |
2 ms |
1372 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |