Difference Constraint Learning Blog

Inadvertently saw the difference constraint from the introduction to the algorithm and spent an hour simply learning

Reference blog: Blog 1

It probably means that there are generally two models for this type of problem:

1. Give you n intervals to indicate the maximum number of intervals, then find the total maximum?

 

 

2. Give you n intervals to indicate the minimum number of each interval, and then find the total minimum

 

 

Example: P1250 tree planting

Topic:

Give you m intervals bet means that at least t trees are planted from b to e, and then find the minimum total trees.

 

Analysis formula:

Because at least t trees are planted, it must be written in a form greater than or equal to

s [e] -s [b-1]> = t // Only lower bound, no upper bound

Each position i can be planted or not: 0 <= s [i] -s [i-1] <= 1

s[i]-s[i-1]>=0

s[i-1]-s[i]>=1

 

Building plans:

b-1 to e Directed edges with edge weight t

i-1 to i Directed edges with edge weight 0

As for why i to i-1 build a directed edge of -1

Personal understanding is that the above two sides are established by the minimum value of this side, then i to i-1 should also be the minimum weight (the value is 0,1)

 

 

 

Another question: P3084 Spotted Cow

Topic:

Give you m intervals (l, r) means there is only one cow in interval l, r. How many cows are there in total?

 

Analysis formula:

s[r]-s[l-1]=1

Each position i has a cow and no cow: 0 <= s [i] -s [i-1] <= 1

Written in the form of the last edition only

s[i]-s[i-1]<=1

s[i-1]-s[i]<=0

 

Building plans:

Directed graph with weights of 1 from 1 to r

r directed graph with l-1 construction weight of 1

Directed graph with i-1 to 1 building weight value of 1

Directed graph with i to i-1 building weight value of 0

Here is the same, each side is to save the maximum value, and then run the shortest path on the graph.

 

 

Because of the negative weights in the graph, Bellman-Ford has only learned SPFA solutions.

Published 519 original articles · praised 69 · 50,000+ views

Guess you like

Origin blog.csdn.net/qq_41286356/article/details/105527799