洛谷P2196 挖地雷(dp)

题意

题目链接

Sol

早年NOIP的题锅好多啊。。

这题连有向边还是无向边都没说(害的我wa了一遍)

直接\(f[i]\)表示到第\(i\)个点的贡献

转移的时候枚举从哪个点转移而来

然后我就用一个\(n^2\)的算法过了一道\(n \leqslant 20\)的题??。。

#include
#include
#include
using namespace std;
const int MAXN = 101;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, a[MAXN], ca[MAXN][MAXN], f[MAXN], Pre[MAXN], ed;
int main() {
    N = read();
    for(int i = 1; i <= N; i++) a[i] = read();
    for(int i = 1; i <= N; i++) 
        for(int j = i + 1; j <= N; j++) 
            ca[i][j] = ca[j][i] = read();
    int ans = 0;
    for(int i = 1; i <= N; i++) {
        for(int j = 1; j <= i - 1; j++)
            if(ca[i][j] && f[j] > f[i]) f[i] = f[j], Pre[i] = j;
        f[i] += a[i]; 
        if(f[i] > ans) ans = f[i], ed = i;
    }
    vector v;
    while(ed) v.push_back(ed), ed = Pre[ed]; 
    for(int i = v.size() - 1; i >= 0; i--) printf("%d ", v[i]); puts("");
    printf("%d", ans);
    return 0;
}

你可能感兴趣的:(洛谷P2196 挖地雷(dp))