Difficulty: β +
Recommended Time: Min
Actual Time: 5 min
Topic:??
Code:??
This is a classic and classic.
I used the method of the network stream to do one (according to the question is the network flow 24).
However, the problem is simple (cough) solution.
Simplicity means short code, but it takes time to think about what's inside.
The main idea is that some villages want to buy and sell wine. Some only sell, some only buy. What is the minimum cost of transporting wine? It costs 1 yuan to ship a bottle of wine from a village to an adjoining village.
I really don't know how to deal with this problem at first glance (except for the network flow Brute force solution).
But after seeing the great God's practice, I thought of something.
Instead of looking at how each bottle is transported (so it is not a good idea to find the inner link), it is to see the number of each village changing hands.
It is found that in every method of transporting wine, each village has a regular number of hands.
If there are two bottles of wine passing through a village (including those that continue to be transported and parked in the village), then the number of the village turnover is 2.
If a bottle of wine passes through two villages, it adds one to each of the two villages.
So this gives us another idea of how to solve problems.
If you change the angle, look at the number of each village changing hands, you can also count the cost.
So we just have to find the minimum value of the sum of each village's turnover.
What is the minimum value?
It is not difficult to find that if a village X is the dividing line, the demand for the left side of X is q1,x to the right of demand plus the demand for x itself is Q2, then there is q1 + q2 = 0.
Obviously, the wine that handles village x must meet the demand of the left village at least |q1|. This is the minimum value we want.
The answer is ∑ (|qi|), Qi =∑ai, i = 1, 2, 3, ..., I. (The q here differs from the Q1,Q2 above)
2018-02-03
Analysis of the Trading in Gergovia and algorithm of UVa 11054 Wine