Guest User

Untitled

a guest
Dec 15th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <cstdio>
  2. #include <queue>
  3. #include <tuple>
  4. #define INF 987654321
  5. using namespace std;
  6.  
  7. int row,col;
  8. int miro[101][101]; // 가로 세로
  9. int dist[101][101];
  10.  
  11. int dx[]={1,-1,0,0};
  12. int dy[]={0,0,1,-1};
  13.  
  14. void djikstra(int startx,int starty)
  15. {
  16. priority_queue<tuple<int,int,int>> pq;
  17. dist[startx][starty]=0;
  18. pq.push(make_tuple(0,startx,starty)); //dist 가로 세로
  19.  
  20. while(!pq.empty())
  21. {
  22. int cost,herex,herey;
  23. tie(cost,herex,herey)=pq.top();
  24. cost=-cost;
  25. pq.pop();
  26. if(cost>dist[herex][herey]) continue;
  27.  
  28. for(int i=0; i<4; ++i)
  29. {
  30. int newx=herex+dx[i];
  31. int newy=herey+dy[i];
  32.  
  33. if(newx<=0||newy<=0||newx>row||newy>col) continue;
  34.  
  35. int via = cost + miro[newx][newy];
  36.  
  37. if(via<dist[newx][newy])
  38. {
  39. dist[newx][newy]=via;
  40. pq.push(make_tuple(-via,newx,newy));
  41. }
  42. }
  43. }
  44. printf("%d",dist[row][col]);
  45. }
  46.  
  47. int main() {
  48.  
  49. scanf("%d %d",&row,&col);
  50.  
  51. for(int i=1; i<=col; ++i)
  52. {
  53. for(int j=1; j<=row; ++j)
  54. {
  55. scanf("%1d",&miro[j][i]);
  56. dist[j][i]=INF;
  57. }
  58. }
  59.  
  60. djikstra(1,1);
  61. // your code goes here
  62. return 0;
  63. }
Add Comment
Please, Sign In to add comment