[AGC036D] Negative Cycle

https://atcoder.jp/contests/agc036/tasks/agc036_d

你有一个n个点的带权有向图,点编号为0,...,n−1。初始时这个图有n−1条边,对于每个0≤i≤n−2,点i到点i+1有一条边权为0的边。

现在图上新加入了一些边:对于每个0≤i,j≤n−1,如果i<j,那么加入一条i到j的边权为−1的边,否则加入一条i到j的边权为1的边。

每条新加入的边有一个代价(在输入中给出),你希望花费尽量小的代价(最小化删掉的边的代价之和)删掉一些新加入的边(你不能删初始时就有的边),使得这个图上不存在负环。3≤n≤500。

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : \(1200\) points

Problem Statement

We have a weighted directed graph with \(N\) vertices numbered \(0\) to \(N-1\).

The graph initially has \(N-1\) edges. The \(i\)-th edge (\(0 \leq i \leq N-2\)) is directed from Vertex \(i\) to Vertex \(i+1\) and has a weight of \(0\).

Snuke will now add a new edge \((i → j)\) for every pair \(i, j\) (\(0 \leq i,j \leq N-1,\ i \neq j\)). The weight of the edge will be \(-1\) if \(i < j\), and \(1\) otherwise.

Ringo is a boy. A negative cycle (a cycle whose total weight is negative) in a graph makes him sad. He will delete some of the edges added by Snuke so that the graph will no longer contain a negative cycle. The cost of deleting the edge \((i → j)\) is \(A_{i,j}\). He cannot delete edges that have been present from the beginning.

Find the minimum total cost required to achieve Ringo's objective.

Constraints

  • \(3 \leq N \leq 500\)
  • \(1 \leq A_{i,j} \leq 10^9\)
  • All values in input are integers.

Editorial

https://img.atcoder.jp/agc036/editorial.pdf

Let us consider the situation where we have deleted some edges and have no negative cycle. Now, we can assign an integer pi to each vertex so that the following condition is satisfied:

  • For each edge e = (i → j), pj ≤ pi + weight(e) holds.

This can be achieved by, for example, letting pi = (the shortest distance from Vertex 0 to Vertex i). On the other hand, we can see that, if we can assign pi to the vertices so that this condition is satisfied, the graph contains no negative cycle. (If we assume there is a negative cycle, adding pj - pi ≤ +weight(e) along it would lead to contradiction.)

Let us confirm what conditions pi must satisfy. First, for each i (0 ≤ i ≤ N -2), pi ≥ pi+1 must hold, which corresponds to the originally present edge (i → i+1) of weight 0. Here, let us define qi = pi-pi+1, which is a non-negative integer since pi ≥ pi+1.

Then, considering the edge (i # j) of weight 1, pj ≤ pi + 1 must hold, which can be represented as qj + qj+1 + ...+ qi-1 ≤ 1 with q.

Similarly, considering the edge (i → j) of weight -1, qi + qi+1 + ...+ qj-1 ≥ 1 must hold.

Let us consider the problem of whether we can set the values of q so that the condition is satisfied.

Then, we can see that we only need to consider 0 and 1 as the elements of q.

Thus, what we have to do is to solve the following problem:

<<<
We will set up a sequence q consisting of 0 and 1, and delete the edges whose conditions are not satisfied for this q. Minimize the total cost of deleting these edges.
<<<

We can solve it with the DP as follows:

  • dp[i][j] = the minimum total cost of the edges that are confirmed to be deleted when we have chosen the values from q0 through qi and the positions of the last 1 and the second last 1 are i and j, respectively.

Let us consider the transition \(dp[i][j] \to dp[x][i]\). The additional edges of weight 1 we additionally need to delete are the edges \(b \to a\) such that \(j < a ≤ i, x < b\), and the additional edges of weight -1 we additionally need to delete are the edges \(a \to b\) such that i < a < b ≤ x. With prefix sums, this transition can be calculated faster in \(O(N^3)\) time, which is fast enough.

Link to sample code

注:无负环=源点到所有点的最短路存在;有\(i\to i+1\)\(0\)边=最短路递减。可DP。

2020-02-19@zbw

\(d_i\)\(u,v, w\)\(d_u + w \ge d_v\)\(d_0 = 0\)

\(d_i \ge d_{i+1}\)

对于一个点 \(i\),设它 \(d_i = x\)\(j < i\)\(d_j - 1 \ge d_i,d_j \ge d_i + 1\)\(d_i + 1 \ge d_j,d_j\le d_i + 1\)

\(dp_{i,j,k}\) 表示点 \(i\)\(0\)\(i-j+1, \ldots, i\) 的距离为 \(x\)\(0\)\(i-j-k+1 \ldots i-j\) 的距离为 \(x+1\)\(0\)\(i-j-k\) 的距离为 \(x+2\)

转移:考虑 \(0\) 到点 \(i\) 与点 \(i-1\) 的距离是相等的还是不相等的。

如果相等 \(dp_{i,j,k} \leftarrow dp_{i-1,j-1,k}\),那么 \(i-j+1 \ldots i-1\) 到点 \(i\) 的边都要删掉,\(i\)\(i-j-k\) 及之前的边都要删掉

如果 \(d_i = d_{i-1}-1\),~,\(i\)\(d_j > d_{i-1}\) 的点 \(j\) 的边都要删掉。

解答

最草的是我这题看过题解,还硬是愣了一会……(发现连C都不会做了……)

注意前缀和数组也要开long long!!!

张爷爷的做法

Detail

Detail

DP题。需要先建个模。

若没有负环,考虑用\(dis[i]\)表示\(0\)\(i\)的距离,显然有\(dis[i]\ge dis[i+1]\ge dis[i]-1\)。为方便分析,将\(dis[i]\)差分,设\(d_i=d_{i-1}-d_{i}\)

\(sum_{range1\to range2}\)表示\(range1\)\(range2\)所有边的权值,\(dp_{i,j,k}\)表示现在已经DP到了\(i\),且\(d_j=d_k=1\)\(i\)之前最近的两个等于\(1\)\(d\)\(j>k\)),有:

\[dp_{0,0,0}=0\\ dp_{i,j,k}+sum_{[j,i]\to \{i+1\}}+sum_{\{i+1\}\to [0,k)}\to dp_{i+1,j,k} \\ dp_{i,j,k}+sum_{\{i+1\}\to [0,j)}\to dp_{i+1,i+1,j} \]

直接DP(500^3*8/1024/1024=953/1024MB,内存恰好够……然而,510^3*8/1024/1024=1012就会MLE……)也可,也可以滚动数组优化。

题解的做法

Detail (码风太毒瘤了……)

建模与上类似,但是DP的状态只有两维。

\(dp_{j,k}\)表示已经考虑的所有\(d_i\)中,最后的两个,则有以下转移:

\[dp_{0,0}=0\\ dp_{j,k}+sum_{w=-1,[j,i)\to[j,i)}+sum_{[i,n)\to[0,j)}\to dp_{i,j} \]

则答案为\(\min dp\)

posted @ 2021-06-22 07:48  frank3215  阅读(147)  评论(0编辑  收藏  举报