Atcoder Grand Contest 036 D - Negative Cycle

Atcoder Grand Contest 036 D - Negative Cycle

Problem-solving ideas

In some cases, to add or remove a map number of sides to make legitimate title FIG differential restraint system to take into account the shortest. This question may seem and the shortest nothing, but there is a not-so-classic inference, to a point \ (u \) is not a necessary and sufficient condition on the negative loop is
\ [\ forall _ {\ text {Edge} v \ rightarrow u} dis (S, v
) + weight (v, u) \ geq dis (S, u) \] where \ (S \) is a diagram with any \ (U \) Unicom point.

Create a random source point \ (S \) , we let \ (P_i DIS = (S, I) \) , considering only the original chain can be \ (P_i \ GEQ P_ {I}. 1 + \) . For any two \ (X, Y \ (X <Y) \) , plus new edges \ ((x, y), (y, x) \) needs are satisfied \ (p_x-1 \ geq p_y , p_y +1 \ geq p_x \) . Here, however seemingly not push down the subtle difference can be obtained readily apparent conclusion, so \ (P_i-Q_I P_ = {I}. 1 + \) , transposing available
\ [\ sum_ {i = x } ^ {y -1} q_i \ geq 1, \
sum_ {i = x} ^ {y-1} q_i \ leq 1 \] then we can prove that, \ (Q_I \ in \ {0,1 \} \) , where comparison easier if \ (q_i <0 \) differential constraint condition is not satisfied the original chain, if \ (q_i> 0 \) is the point \ (i + 1 \) there is an additional \ (- 1 \) into edge \ ( (V, I +. 1), V <I \) , this time \ (V \) to \ (I \)The worst case can go for a \ (0 \) chain updated, so \ (q_i \) can only as \ (1 \) .

Then we can consider \ (q_i \) Each bit of \ (0 \) or take \ (1 \) , and then delete the illegal side, this process can be \ (\ text {DP} \ ) to solve and does not meet the (q_i \ sum \ leq 1 \ ) \ situation, in which cross a second \ (1 \) when the statistics out for \ (\ sum q_i \ geq 1 \) in the case, each segment successive \ (0 \) statistics can, then you can make \ (dp [i] [j ] \) before taking into account the current \ (i \) bit and \ (i \) selected \ (1 \ ) , on a \ (1 \) in (j \) \ answer, transfer and optimize the use of the prefix can be.

code

/*program by mangoyang*/ 
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
    int ch = 0, f = 0; x = 0;
    for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
    for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
    if(f) x = -x;
}
const int N = 505;
#define int ll
int A[N][N], B[N][N], C[N][N], D[N][N], dp[N][N], n;
signed main(){
    read(n);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j < i; j++) read(A[j][i]);
        for(int j = i + 1; j <= n; j++) read(B[i][j]);
    }
    for(int i = 0; i <= n + 1; i++)
        for(int j = i; j <= n + 1; j++){
            if(i) C[i][j] += C[i-1][j];
            for(int k = j; k <= n + 1; k++) C[i][j] += A[i][k];
        }
    for(int i = n + 1; i >= 0; i--)
        for(int j = i; j <= n + 1; j++){
            D[i][j] += D[i+1][j];
            for(int k = i; k <= j; k++) D[i][j] += B[i][k];
        }
    memset(dp, 0x3f, sizeof(dp));
    dp[0][0] = 0;
    for(int i = 1; i <= n + 1; i++)
        for(int j = 0; j < i; j++){
            for(int k = 0; k <= j; k++) 
                dp[i][j] = min(dp[i][j], dp[j][k] + C[j][i+1] - C[k][i+1] + D[j+1][i]);
        }
    int ans = inf;
    for(int i = 0; i <= n; i++)
        ans = min(ans, dp[n+1][i]);
    cout << ans << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/mangoyang/p/11240437.html