Description
There are
n
cities located along the one-way road. Cities are numbered from
The
For each pair of cities
Determine the maximum number of produced goods that can be sold in total in all the cities after a sequence of transportations.
Input
The first line of the input contains two integers
The second line contains
n
integers
The third line of input contains n integers s i ( 0 ≤ s i ≤ 10 9 ) — the number of units of goods that can be sold in each city.
Output
Print the maximum total number of produced goods that can be sold in all cities after a sequence of transportations.
Solution
Build a network of the following form: The network will consist of
n + 2
vertices, the source located at the node with the number
n + 1
and the sink at the node with the number
n + 2
. For each node
i
such that
The value of the maximum flow in this network is the answer to the problem. Since the network contains O(n 2 ) arcs, the usual flow search algorithms is not applicable here.
Instead, we will find a capacity of the minimal cut in this network. The cut in this case is a partition of the set of all nodes from
1
to
Capacity of the minimal cut can be found using the following dynamic programming:
The answer to the problem can be obtained as min j = 0, n cut(n, j) .
The resulting complexity of this solution is O(n 2 ) . To fit in memory limit you should store only two consecutive rows during the computation of the dynamic.
翻译一下就是考虑网络流:
1.
S
to
2.
i
to
3.
i
to
最大流就是答案,但直接跑是
O(n 2 )
级别的边,肯定爆炸。考虑到这个网络的特殊性,同时考虑到最大流等于最小割。设计状态
f[i][j]
表示对于前
i
个点中有