Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int Solution::solve(vector<vector<int> > &A) {
- priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> q;
- q.push({A[0][0],{0,0}});
- q.push({A[0][1],{1,0}});
- q.push({A[0][2],{2,0}});
- vector<int> hPaint(A.size(),-1);
- vector<int> hCost(A.size(),1e7);
- while(!q.empty()) {
- auto element = q.top();
- q.pop();
- int cost=element.first;
- int color = element.second.first;
- int no = element.second.second;
- if(no==A.size()-1) {
- if(hPaint[no-1]!=color) return cost;
- }
- if(cost<hCost[no]) {
- if(no==0 || (hPaint[no-1]!=color)) {
- hCost[no] = cost;
- hPaint[no] = color;
- } else continue;
- } else { continue; }
- q.push({A[no+1][1]+cost,{1,no+1}});
- q.push({A[no+1][2]+cost,{2,no+1}});
- q.push({A[no+1][0]+cost,{0,no+1}});
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement