Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- struct node {
- int index,najmal_pat;
- node(int s,int p){
- index = s;
- najmal_pat = p;
- }
- bool operator < (const node&tmp) const {
- return najmal_pat > tmp.najmal_pat;
- }
- };
- int main() {
- int n,v;
- cin >> n >> v;
- vector<int> graph[15];
- int niza[n];
- for (int i = 0; i < n; i++) {
- cin >> niza[i];
- graph[niza[i]].push_back(i);
- }
- priority_queue<node> pq;
- vector<bool> visited (n+1, false);
- vector<bool> portali(15, false);
- pq.push(node(0,0));
- while(!pq.empty()){
- node current = pq.top();
- pq.pop();
- visited[current.index] = true;
- if(current.index == n-1){
- cout << current.najmal_pat;
- break;
- }
- if(!portali[niza[current.index]]) {
- for (int i = 0; i < graph[niza[current.index]].size(); i++) {
- int sosed = graph[niza[current.index]][i];
- int pat = current.najmal_pat + v;
- if(!visited[sosed])
- pq.push(node(sosed, pat));
- }
- portali[niza[current.index]] = true;
- }
- if(current.index > 0 and !visited[current.index - 1]){
- pq.push(node(current.index -1,current.najmal_pat +1));
- }
- if(current.index < n and !visited[current.index + 1]){
- pq.push(node(current.index +1,current.najmal_pat+1));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement