Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- vector<pair<int,int>>v[100000];
- struct node{
- int idx;
- int dist;
- node(){}
- node(int _idx,int _dist){
- idx=_idx;
- dist=_dist;
- }
- bool operator < (const node & tmp)const{
- return dist > tmp.dist;
- }
- };
- void dijkstra(int s,int n){
- vector<bool>visited(n,false);
- vector<int>dist(n,2e9);
- dist[s]=0;
- priority_queue<node>pq;
- pq.push(node(s,0));
- while(!pq.empty()){
- node c = pq.top();
- pq.pop();
- if(visited[c.idx]==true){
- continue;
- }
- else{
- visited[c.idx]=true;
- }
- for(int i = 0;i<v[c.idx].size();i++){
- int sosed = v[c.idx][i].first;
- int dist2 = v[c.idx][i].second;
- if(visited[sosed]==false && c.dist + dist2 < dist[sosed]){
- dist[sosed] = c.dist + dist2;
- pq.push(node(sosed,dist[sosed]));
- }
- }
- }
- cout<<dist[n-1]<<endl;
- }
- int main()
- {
- int n,V;
- cin>>n>>V;
- vector<int>x(n);
- for(int i = 0;i<n;i++){
- cin>>x[i];
- }
- for(int i = 0;i<n;i++){
- if(i>0){
- v[i].push_back(make_pair(i-1,1));
- v[i - 1].push_back(make_pair(i,1));
- }
- if(i + 1 < n){
- v[i].push_back(make_pair(i+1,1));
- v[i + 1].push_back(make_pair(i,1));
- }
- for(int j = 0;j<n;j++){
- if(x[i]==x[j]){
- v[i].push_back(make_pair(j,V));
- v[j].push_back(make_pair(i,V));
- }
- }
- }
- dijkstra(0, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement