이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
بسم الله الرحمن الرحيم
Author:
(:Muhammad Aneeq:)
*/
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
void solve()
{
int n,m;
cin>>n>>m;
pair<int,int>a[m];
vector<int>lo[n];
for (auto& i:a)
{
cin>>i.first>>i.second;
lo[i.first].push_back(i.second);
}
int dp[n]={};
for (int i=0;i<n;i++)
dp[i]=1e9+10;
dp[a[0].first]=0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
pq.push({0,a[0].first});
bool vis[n]={};
while (pq.size())
{
int loc=pq.top().second;
pq.pop();
if (vis[loc])
continue;
vis[loc]=1;
for (auto j:lo[loc])
{
int z=0;
for (int k=loc-j;k>=0;k-=j)
{
z++;
if (dp[k]>dp[loc]+z)
{
dp[k]=dp[loc]+z;
pq.push({dp[k],k});
}
}
z=0;
for (int k=loc+j;k<n;k+=j)
{
z++;
if (dp[k]>dp[loc]+z)
{
dp[k]=dp[loc]+z;
pq.push({dp[k],k});
}
}
}
}
cout<<(dp[a[1].first]!=1e9+10?dp[a[1].first]:-1)<<endl;
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
solve();
}
# | 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... |