lvisbl_'s blog

By lvisbl_, history, 6 weeks ago, In English

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

Full text and comments »

  • Vote: I like it
  • +10
  • Vote: I do not like it

By lvisbl_, history, 7 weeks ago, In English

Recently, I have encounter this dynamic programming problem and I can not debug where I have problem, I used forward DP style and I have checked the transition and base case. Please help me to look at it, I'm so frustrating. Thank in advance.

Problem link: leetcode

My code:

code

Updated fix my problem: my for loop only run until i = n, but the final dp state which is dp[n + 1][m + 1] can be reached from dp[n + 1][m] => My for loop should run till n + 1 so this relaxation can happen.

fixed

Full text and comments »

  • Vote: I like it
  • -2
  • Vote: I do not like it

By lvisbl_, history, 2 months ago, In English

Recently I have done 711C - Coloring Trees, and I have found an overflow when I use min(a,b) function. More specifically, if I use:

min(a, b + c) when a, b and c are long long and sum of b + c large than 32-bit integer then the sum b + c will be treated as 32-bits integer and hence the overflow.

but let say, if I use a temporary variable:

long long tmp = b + c

then

min(a, tmp) will not overflow

What kind of behavior is this?

This is two submissions, one is overflow and the another one use a temporary variable will not overflow:

Overflow: 285989412

Not overflow: 286024512

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By lvisbl_, history, 6 months ago, In English

Recently, I encounter this 1741E - Sending a Sequence Over the Network that combine both forward and backward DP style, that make me feel surprised. I thought that we could only use either forward and backward DP in a problem to solve it. But in this problem we use both? Let call dp[i] is can we separate array from [1,i] into valid segments. Then:

The backward DP: if from range [1,i] we can not separate array into valid segments then we try to make the current number at index i as the length of the last segment => Check backward the range [1,i — b[i] — 1].

The forward DP: if we can separate array into valid segments in range [1,i-1], then the current number at index i can possibly length of the next segments => Update dp[i + b[i]] = true.

The doubt is can I combine both forward and backward DP style in all DP problems? If not, in what case/what type of DP properties the combination of forward and backward style will work? Do we have any related problems that require to combine both styles like this?

Thank in advance.

Full text and comments »

  • Vote: I like it
  • -1
  • Vote: I do not like it

By lvisbl_, history, 6 months ago, In English

Hello mates, I'm trying to solve this DP question Coin Combinations II from CSES: https://cses.fi/problemset/task/1636/

Initially, I define an array dp[i][j]: meaning number of ways to form sum i, starting choosing coins from j-th index. And this solution using this DP array get me TLE.

But if I switch the two dimension, array dp[i][j]: number of ways to form sum j, starting choosing coins from i-th index. This solution give me accepted. But why?

Note:

  • Two solution is 99% the same the only different is that I switch two dimensions.

  • Sum i at most 10^6 and there are at most 10^2 coins.

Thank in advance.

Accepted code
TLE code

Full text and comments »

  • Vote: I like it
  • +3
  • Vote: I do not like it