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

Автор lvisbl_, история, 18 месяцев назад, По-английски

Recently, I have encountered this Floyd Warshall problem, that the problem adds extra constraint on the shortest path (all edge weights + 1 largest vertice on the path). Following my understanding, Floyd Warshall is dynamic programming and when we "relax" the state, I will add that extra condition.

So, I call dp[i][j][k]: the min distance between i and j vertices, and the middle vertices consisting only the first k vertices. And maxCost[i][j][k] is the max cost vertice of the path I have chosen in dp[i][j][k]. So my transition:

if (dp[i][j][k] + maxCost[i][j][k] > dp[i][k][k — 1] + dp[k][j][k — 1] + max(maxCost[i][k][k — 1], maxCost[k][j][k — 1]) or if the last chosen path for two vertices i and j has larger cost for the path for i, j and k is in the middle of the path, then I relax distance between i and j, and set new maxCost for that path. But there is problem with this logic can you help to point it out. Thank!

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

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

Consider this test case:

5 5 1
100 1000 1 1 10000
4 1 10
1 3 10
4 2 1
2 3 1
3 5 1
4 5

Your code prints 10021, but I think the correct answer is 10003. If I understand your logic correctly, here is your problem: you calculate dp[4][3][3] to be 20 (and not 2): even though $$$4 \to 2 \to 3$$$ has edges summing to only 2, the feast at 2 would cost 1000, meanwhile by going $$$4 \to 1 \to 3$$$ you pay 20 for the edges but 100 for the feast. Here's why this breaks: once you include 5, the feast will be held there and it turns out that going $$$4 \to 2 \to 3$$$ would have been cheaper after all.

Here's how I would solve this problem
  • »
    »
    18 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Thank you so much for pointing it out, I have understood why my logic failed. But during debug this kind of problem, it took me so much time and still did not know where the logic failed. So, another question.. How do you come up with the test case.

    • »
      »
      »
      18 месяцев назад, скрыть # ^ |
       
      Проголосовать: нравится +9 Проголосовать: не нравится

      Experience I guess. After reading what you said in the blog I quickly thought "but what if you choose a more expensive path because it has a cheaper feast, and then it turns out you won't use that feast anyway". Then I just constructed a test case to prove that this can happen.

      What you also can do is:

      • write a very naive solution (try all paths or something) or copy someone else's if you can
      • write a program to generate thousands of small test cases
      • use those two to find a test case where your solution gives the wrong answer.
    • »
      »
      »
      18 месяцев назад, скрыть # ^ |
      Rev. 2  
      Проголосовать: нравится 0 Проголосовать: не нравится

      Red coders will always say it is just practice but, in reality, it is $$$IQ$$$ and practice is just a way to express the $$$IQ$$$. Imagine $$$IQ$$$ is like how tall you are and codeforces is like basketball. No amount of practice can make a $$$5'0$$$ guy have the same skill as a $$$8'0$$$ guy.

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

        I think your mindset is the problem

        • »
          »
          »
          »
          »
          18 месяцев назад, скрыть # ^ |
          Rev. 2  
          Проголосовать: нравится 0 Проголосовать: не нравится

          But let me ask you this: if it were $$$100$$$% practice, then why'd he add this in?

          It seems to me like he just thought of it and had no idea how the thought came about. My thought is: if it were experience, surely he'd see that his thought came from something he did in his past experience (and so he would have said "experience" more confidently in his comment). But maybe I am wrong.