이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define MOEZ ios_base::sync_with_stdio(false);cin.tie(NULL),cout.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define F first
#define S second
#define pb push_back
#define all(v) v.begin() , v.end()
#define eb emplace_back
#define getunique(v) {sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());}
#define mem(d , x) memset(d , x , sizeof(d));
#define sz(x) (int)x.size()
const int N = 30005;
//const ll M = 1e9+7;
//const ll inf = 1e18;
//const ld eps = 1e-6;
#define int long long
int n , id[N] , p[N] , m;
vector<pair<int , int>>adj[N];
int d[N];
signed main(){
MOEZ
cin >> n >> m;
for(int i = 0; i < n;i++){
d[i] = 1e18;
}
vector<int>v;
for(int i = 0; i < m;i++){
cin >> id[i];
cin >> p[i];
v.pb(id[i]);
}
sort(all(v));
for(int i = 0; i < m;i++){
for(auto x:v){
if(x >= id[i])break;
if((id[i]-x)%p[i] == 0){
adj[id[i]].pb({x , (id[i]-x)/p[i]});
}
}
for(int j = m-1; v[j] > id[i];j--){
if((v[j]-id[i])%p[i] == 0)adj[id[i]].pb({v[j] , (v[j]-id[i])/p[i]});
}
}
priority_queue<pair<int , int> , vector<pair<int , int>> , greater<>>q;
q.push({id[0] , 0});
d[id[0]] = 0;
while(!q.empty()){
int u = q.top().F , cost = q.top().S;
q.pop();
if(d[u] < cost)continue;
for(auto x:adj[u]){
int v = x.F , nc = cost+x.S;
if(nc < d[v]){
d[v] = nc;
q.push({v , nc});
}
}
}
cout << (d[id[1]] == 1e18 ? -1 : d[id[1]]);
}
# | 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... |