@atcoder - [email protected] Negative Cycle


@[email protected]

Given N points of a directed weighted graph, numbered from 0 to N - 1. This graph has a start N - 1 edges, connecting edges i th point i and the point i + 1, the right side is zero.

Next to this FIG Bordered: For each pair (i, j) (i ≠ j), even i -> j, when i <j -1 when the right side; the right side is otherwise 1.

We would delete some of the edge (i, j) (i ≠ j), makes this picture negative loop does not exist. Deletion cost edge (i, j) is A (i, j).
Please find the minimum cost puncturing edge, so that the ring is not present in FIG negative. The only plus side after deletion.

The Constraints
3≤N≤500
1 ≦ a (I, J) ≦ 10. 9 ^
all numbers are integers.

Input
format of the following forms:
N
A0,1 A0,2 A0,3 ⋯ A0,. 1 N-
A1,0 A1,2 A1,3 ⋯ A1,. 1 N-
A2,0 A2,1 A2,3 A2 ⋯, N -1

the AN the AN-1,1-1,0-1,2 ⋯ the AN-the AN. 1, N-2

Output
Output the smallest Erase costs.

Sample Input 1
3
2 1
1 4
3 3
Sample Output 1
2

Sample Input 2
4
1 1 1
1 1 1
1 1 1
1 1 1
Sample Output 2
2

@[email protected]

"I feel that happened to think of this practice, I feel a little sense then reverse out of a question." - By lsk.

If there are no negative loop, meaning that shortest existence. We have to think from the shortest start.
Shortest have a very classic conclusion: the triangle inequality. I.e., for any w is a right side edge (u, v) there is always a dis [u] + w> = dis [v].
So we start again from the triangle inequality, further analysis of this question.

FIG distance so that the final point 0 to the i-th point of dis [i]. Since the right side edge of the presence of 0, it is dis [i]> = dis [ i + 1]. Wherein there are dis [0] = 0.
For i-> j (i <j) of the edge, there is dis [i] - 1> = dis [j]; for i-> j (i> j) side, there are dis [i] + 1> = dis [j].
Since the maximum dis should satisfy the triangle inequality, there is dis [I] in the absence of a negative ring - 1 <= dis [i + 1].

Noted the above inequality are in dis [i] and dis [i + 1] established between, we can consider the difference: Let p [i] = dis [i ] - dis [i + 1] ( Note that not dis [i + 1] - dis [ i]), there are 0 <= p [i] < = 1.
For i-> j (i <j) of the edge, should satisfy \ (\ sum_ {I} = K-J. 1} ^ {P [K] \ GE. 1 \) ; for i-> j (i> j) side, should satisfy \ (\ sum_ {J} = K-I. 1} ^ {P [K] \ Le. 1 \) . This can be deduced from the above inequality triangle.
Namely: the need to contain a range of at least 1 1, 1 1 needs to include a maximum of the interval.

Then we can be a dp on this p. Set DP [i] [j] [k] denotes bits i has been processed before, the first forward number 1 at a position j, k at a second position.
Enumeration of the i + 1 bits are provided as a 0 or a 1 to fill the time shift. The first dimension is clearly can roll out.
Time complexity of O (n ^ 3).

@accepted [email protected]

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 500;
const ll INF = ll(1E18);
int A[MAXN + 5][MAXN + 5], B[MAXN + 5][MAXN + 5], n;
// A[i][j] -> (p[i] + p[i+1] + ... p[j] >= 1) (i <= j)
// B[i][j] -> (p[i] + p[i+1] + ... p[j] <= 1) (i <= j)
ll SA[MAXN + 5][MAXN + 5], SB[MAXN + 5][MAXN + 5];
// SA[i][j] -> A[1][j] + A[2][j] + ... + A[i][j]
// SB[i][j] -> B[1][j] + B[2][j] + ... + B[i][j]
ll dp[2][MAXN + 5][MAXN + 5], f[2][MAXN + 5], g[2];
int main() {
    scanf("%d", &n);
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=i-1;j++) scanf("%d", &B[j][i-1]); // i -> j (i > j)
        for(int j=i+1;j<=n;j++) scanf("%d", &A[i][j-1]);
    }
    for(int j=1;j<=n;j++)
        for(int i=1;i<=n;i++)
            SA[i][j] = A[i][j] + SA[i-1][j], SB[i][j] = B[i][j] + SB[i-1][j];
    n--;
    for(int j=1;j<=n;j++) {
        for(int k=j+1;k<=n;k++)
            dp[0][j][k] = INF;
        f[0][j] = INF;
    }
    g[0] = 0;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<i;j++) {
            for(int k=j+1;k<i;k++)
                dp[1][j][k] = dp[0][j][k], dp[0][j][k] = INF;
            f[1][j] = f[0][j], f[0][j] = INF;
        }
        g[1] = g[0], g[0] = INF;
        for(int j=1;j<i;j++) {
            for(int k=j+1;k<i;k++) {
                dp[0][j][k] = min(dp[0][j][k], dp[1][j][k] + SA[i][i] - SA[k][i] + SB[j][i]);
                dp[0][k][i] = min(dp[0][k][i], dp[1][j][k] + SB[k][i]);
            }
            f[0][j] = min(f[0][j], f[1][j] + SA[i][i] - SA[j][i]);
            dp[0][j][i] = min(dp[0][j][i], f[1][j] + SB[j][i]);
        }
        g[0] = min(g[0], g[1] + SA[i][i]);
        f[0][i] = min(f[0][i], g[1]);
    }
    ll mn = INF;
    for(int j=1;j<=n;j++) {
        for(int k=j+1;k<=n;k++)
            mn = min(mn, dp[0][j][k]);
        mn = min(mn, f[0][j]);
    }
    mn = min(mn, g[0]);
    printf("%lld\n", mn);
}

@[email protected]

In order to facilitate transfer, multi-set with only a few 1/1 does not contain any state.

I next time if we do not open long long I will. . .

Guess you like

Origin www.cnblogs.com/Tiw-Air-OAO/p/11713144.html