Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <vector>
- #include <algorithm>
- using namespace std;
- const int maxn = 1e5 + 10;
- const int INF = 2e9;
- vector<pair<int, int>> graph[maxn];
- 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;
- }
- };
- int main(){
- int n, v;
- cin >> n >> v;
- vector<int> portal(n);
- for(int i = 0; i < n; i++) {
- cin >> portal[i];
- }
- for(int i = 0; i < n; i++) {
- if(i - 1 >= 0) {
- graph[i].push_back(make_pair(i - 1, 1));
- }
- if(i + 1 < n) {
- graph[i].push_back(make_pair(i + 1, 1));
- }
- for(int j = 0; j < n; j++) {
- if(portal[i] == portal[j]) {
- graph[i].push_back(make_pair(j, v));
- }
- }
- }
- vector<bool> visited(n, false);
- vector<int> dist(n, INF);
- priority_queue<node> pq;
- pq.push(node(0, 0));
- dist[0] = 0;
- while(!pq.empty()) {
- node c = pq.top();
- pq.pop();
- if(visited[c.idx]) {
- continue;
- }
- visited[c.idx] = true;
- for(int i = 0; i < (int) graph[c.idx].size(); i++) {
- int neighbour = graph[c.idx][i].first;
- int weight = graph[c.idx][i].second;
- if(!visited[neighbour] and c.shortest_path + weight < dist[neighbour]) {
- dist[neighbour] = c.shortest_path + weight;
- pq.push(node(neighbour, dist[neighbour]));
- }
- }
- }
- cout << dist[n - 1] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement