We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
@shashank21j why this solution give wrong answer for test cases 4 5 6 8:
#include<bits/stdc++.h>#include<stdio.h>#include<limits.h>usingnamespacestd;#define ROW 1001#define COL 1001typedefunsignedlonglongull;constlonglongM=1e9+7;structcell{ullx,y;ulldistance;cell(ullx,ully,ulldistance):x(x),y(y),distance(distance){}};booloperator<(constcell&a,constcell&b){if(a.distance==b.distance){if(a.x!=b.x)return(a.x<b.x);elsereturn(a.y<b.y);}return(a.distance<b.distance);}ullshortest(ullgrid[ROW][COL],ullrow,ullcol){ulldis[row][col];for(ulli=0;i<row;i++)for(ullj=0;j<col;j++)dis[i][j]=INT_MAX;intdx[]={-1,0,1,0};intdy[]={0,1,0,-1};set<cell>st;st.insert(cell(0,0,0));dis[0][0]=grid[0][0];while(!st.empty()){cellk=*st.begin();st.erase(st.begin());for(ulli=0;i<4;i++){ullx=k.x+dx[i];ully=k.y+dy[i];if(!(x>=0&&x<=row-1&&y>=0&&y<=col-1))continue;if(dis[x][y]>dis[k.x][k.y]+grid[x][y]){if(dis[x][y]!=INT_MAX)st.erase(st.find(cell(x,y,dis[x][y])));dis[x][y]=dis[k.x][k.y]+grid[x][y];st.insert(cell(x,y,dis[x][y]));}}}returndis[row-1][col-1];}intmain(){ulln;cin>>n;ullcost[ROW][COL];for(ulli=0;i<n;i++){for(ullj=0;j<n;j++){cin>>cost[i][j];}}ullans=shortest(cost,n,n);cout<<ans;return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #83: Path sum: four ways
You are viewing a single comment's thread. Return to all comments →
@shashank21j why this solution give wrong answer for test cases 4 5 6 8: