CCCPaste

Untitled

#include 
#include 
#include 
using namespace std;

struct node {
    int idx, shortest_path;
    node () {}
    node(int _idx, int _shortest_path) {
        idx = _idx;
        shortest_path = _shortest_path;
    }
    bool operator < (const node & tmp) const {
        return shortest_path > tmp.shortest_path;
    }
};
vector graph[11];

int main()
{
    int n, V;
    cin >> n >> V;
    
    vector portal(n);
    for(int i = 0; i < n; i++) {
        cin >> portal[i];
        graph[portal[i]].push_back(i);
    }
    
    priority_queue pq;
    pq.push(node(0, 0));
    
    vector visited(n, false);
    vector visited_portal(11, false);
    
    while(!pq.empty()) {
        node c = pq.top();
        pq.pop();
        
        if(c.idx == n - 1) {
            cout << c.shortest_path << endl;
            break;
        }
        if(visited[c.idx]) {
            continue;
        }
        visited[c.idx] = true;
        
        if(c.idx + 1 < n and !visited[c.idx + 1]) {
            pq.push(node(c.idx + 1, c.shortest_path + 1));
        }
        if(c.idx - 1 >= 0 and !visited[c.idx - 1]) {
            pq.push(node(c.idx - 1, c.shortest_path + 1));
        }
        
        if(!visited_portal[portal[c.idx]]) {
            for(int i = 0; i < (int) graph[portal[c.idx]].size(); i++) {
                int teleport = graph[portal[c.idx]][i];
                
                if(!visited[teleport]) {
                    pq.push(node(teleport, c.shortest_path + V));
                }
            }
            visited_portal[portal[c.idx]] = true;
        }
        
    }
    
    return 0;
}