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

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

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
  • Проголосовать: не нравится

»
12 дней назад, # |
  Проголосовать: нравится +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
  • »
    »
    12 дней назад, # ^ |
      Проголосовать: нравится 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.

    • »
      »
      »
      12 дней назад, # ^ |
        Проголосовать: нравится +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.
      • »
        »
        »
        »
        12 дней назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Thank you so much, I have got AC following you suggestion and it's satisfied me enough.

        Btw just for curiosity, below, I have sent another code which run the same failed logic two times and it is working (got AC too). It's like some kind of convergence because of two parameters depend each other (min dist and max cost), but I have no idea how it's work.

      • »
        »
        »
        »
        6 дней назад, # ^ |
        Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

        Hi sir, I have this question hang around my mind recently, during doing the shortest path algorithms such as Floyd, Bellman,.. If I do proper DP without space optimization, I see that I have DAG and the relaxation will follow the topological sort from smaller states to bigger states. But if I do space optimization, we will not have DAG anymore in terms of relaxation order. But I also know that even if the relaxation order is not correct and not follow the DAG, the answer is still correct because the relaxations that not follow the correct topo order will not make the answer more worse but always equal or better. Somehow, I can not proof the correctness of it. And I do not know the keyword to "describe" this phenomenal, can you help me to point the term out. Thank in advance!

    • »
      »
      »
      6 дней назад, # ^ |
      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.

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

        I think your mindset is the problem

        • »
          »
          »
          »
          »
          5 дней назад, # ^ |
          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.

  • »
    »
    12 дней назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Hi, I have this arbitrary code from my peer and with the same idea, he said that if I run Floyd 2 times, the logic will work. Can you help to explain this..

    code