Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int MOD = 1e5;
- int djikstra(int start, int end, vector<int> &arr){
- priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
- vector<int> distance(1e5, INT_MAX);
- //push start in pq and init distance
- pq.emplace(0, start);
- distance[start] = 0;
- while(!pq.empty()){
- pair<int, int> top = pq.top();
- pq.pop();
- int node = top.second, nodeDist = top.first;
- if(distance[node] < nodeDist){
- continue;
- }
- for(int i = 0; i < arr.size(); i++){
- int child = ((node % MOD) * arr[i]) % MOD, childDist = nodeDist + 1;
- if(childDist < distance[child]){
- pq.emplace(childDist, child);
- distance[child] = childDist;
- }
- }
- //Early Breakage - The first occurence of end popped from pq, is at the shortest distance
- if(node == end){
- return nodeDist;
- }
- }
- return distance[end] == INT_MAX ? -1 : distance[end];
- }
- int minimumMultiplications(vector<int> &arr, int start, int end){
- return djikstra(start, end, arr);
- }
- int main() {
- // your code goes here
- int n, start, end;
- cin >> n;
- vector<int> arr(n);
- for(int i = 0; i < n; i++){
- cin >> arr[i];
- }
- cin >> start >> end;
- cout << minimumMultiplications(arr, start, end) << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment