Блог пользователя code.degub

Автор code.degub, история, 5 лет назад, По-английски

Question In this question i am trying to use 0-1 bfs here is my code 115034044 can anyone help me to find why i am getting TLE or what's wrong in this please help

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

Oh lol I tried a number of optimizations before I realized that your complexity is $$$\mathcal O(nmk)$$$ with $$$n, m, k \leq 1000$$$, which is too much.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

your solution is correct 115077277 , I only added the line if(dis[nr][nc] < dis[cr][cc]+1)break; in your bfs . if you meet a cell that has distance less than distance your current cell +1,then you don't need to consider further since it can never be worse if you started traversing in the same direction from that cell instead .I think this simple optimization makes it O(nm) but I am not sure how to prove it