Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <vector>
- 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<int> graph[11];
- int main()
- {
- int n, V;
- cin >> n >> V;
- vector<int> portal(n);
- for(int i = 0; i < n; i++) {
- cin >> portal[i];
- graph[portal[i]].push_back(i);
- }
- priority_queue<node> pq;
- pq.push(node(0, 0));
- vector<bool> visited(n, false);
- vector<bool> 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement