Difference constraint system personal understanding, constraint personal understanding
Today, we are exposed to a mysterious thing:
Difference Constraint
Personal Understanding: The difference constraint is to give some restrictions to find the optimal solution that meets the conditions, or determine whether the conditions are true.
Practices/ideas:
1. First, write the corresponding inequality based on the conditions of the question.
2. Convert the inequality into a-B <= c
3. Create an edge with a weight of c, pointing from B to
4. Link an edge from 0 to another vertex with the right of 1
5. Run the deep search SPFA to check whether the answer is updated.
After this is done, the shortest path is obtained! The maximum value that meets the condition is obtained!
Of course, you can follow the opposite idea above,
The method and result are the opposite of the above, but both can be AC!
There must be a lot of doubts here. I will explain it based on my understanding.
1. Why do we need to build an edge from B to a with the weight of c?
Because a-B <= c, the edge weight between B and a is c, and the answer is
2. Why is the shortest path not the longest path?
Because every time we create the largest edge weight, we need to obtain the value that satisfies all the inequalities.
Then, when this value is the smallest value in all inequalities, the conditions can be met,
So we need the shortest path.
3. Why does SPFA need to perform deep search instead of wide search?
Because deep search is easy to judge negative loops!
4. Why do we need to build an edge from 0 to all vertices?
Personal feeling: to facilitate the updating of answers
But it may be incorrect. Please wait and ask your teacher...
We recommend several good articles:
Http://972169909-qq-com.iteye.com/blog/1185527
Http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html