Блог пользователя i_love_math

Автор i_love_math, история, 10 лет назад, По-английски

i am trying to solve http://www.spoj.com/problems/BITMAP/ problem.

my code is giving TLE on SPOJ .

i am not getting why it's getting TLE.

i approached in this procedure: I stored those pixels which have value 1 in a queue and applied bfs across these,

my code is http://ideone.com/ppgQiW

please help

thanks

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

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

If I understood your code right you do multiple BFS's and all of them pass by all the vertices. That results in a high complexity since there may be up to n*m BFS's, each of them executed in O(n*m).

Try to solve it with only one "complex" BFS that may pass by each vertex more than once, in case a better value for it is found.

This is the code I used: Don't click me . If you still need help message me.

This problem is beautiful, I highly recommend everyone learning basic graph algorithms to solve it.