首页 > 代码库 > SDOI 2006 - 保安站岗

SDOI 2006 - 保安站岗

最小点费用覆盖,即选中费用最小的i个点把所有的边覆盖。

#include <cstdio>
using namespace std;

#define MAXV    1505
#define MAXE    (MAXV - 1)
int Vefw[MAXE], Veh[MAXV], Vet[MAXE], Vc[MAXV], Veptr;
int dp[MAXV][3],dp2[MAXV];

#define min(a,b) ((a)<(b)?(a):(b))

#define addedge(s,t)    do{\
    Vefw[Veptr] = Veh[s], Vet[Veptr] = t;        Veh[s] = ++Veptr;}while(0)

void solve(int s)
{
    dp[s][2] = 0, dp[s][1] = Vc[s];
    for(int e = Veh[s]; e; e = Vefw[e]) {
        int t = Vet[--e];
        solve(t);
        dp[s][1] += min(min(dp[t][0], dp[t][1]), dp[t][2]);
        dp[s][2] += min(dp[t][0], dp[t][1]);
    }
    int k;
    dp2[k=s] = 0x3f3f3f3f;
    for(int e1 = Veh[s]; e1; e1 = Vefw[e1]) {
        int t1 = Vet[--e1];
        dp2[t1] = dp[t1][1];
        for(int e2 = Veh[s]; e2; e2 = Vefw[e2]) {
            int t2 = Vet[--e2];
            if (t2 != t1) dp2[t1] += min(dp[t2][0], dp[t2][1]);
        }
        if (dp2[k] > dp2[t1]) k = t1;
    }
    dp[s][0] = dp2[k];
}

int N;

int main(void)
{
//    freopen("sdoi2006_guard.txt", "r", stdin);
    scanf("%d", &N);
    int root = 0;
    for(int i=0; i<N; ++i) {
        int s, k, c;
        scanf("%d%d%d", &s, &k, &c);
        Vc[--s] = k;
        while(c) {
            int t;
            scanf("%d", &t); --t;
            if (t == root) root = s;
            addedge(s, t);
            --c;
        }
    }
    solve(root);
    printf("%d\n", min(dp[root][0], dp[root][1]));
    return 0;
}

 

1046
Accepted
988
0
C++ 1421 B 2014-05-20 14:36:41