皇宫看守

皇宫看守

#include<bits/stdc++.h>
#define re return
#define inc(i,l,r) for(int i=l;i<=r;++i)
using namespace std;
template<typename T>inline void rd(T&x)
{
    char c;bool f=0;
    while((c=getchar())<'0'||c>'9')if(c=='-')f=1;
    x=c^48;
    while((c=getchar())>='0'&&c<='9')x=x*10+(c^48);
    if(f)x=-x;
}

const int maxn=1505;
int n,m,rt,f[maxn][3],val[maxn],vis[maxn];
//f[x][0]可以被父亲看到,当前未安排
//f[x][1]可以被某个儿子看到,当前未安排
//f[x][2]当前安排

vector<int>G[maxn];

inline void dfs(int x)
{
    int d=2147483647;
    for(vector<int>::iterator it=G[x].begin();it!=G[x].end();++it)
    {
        int v=*it;
        dfs(v);
        f[x][0]+=min(f[v][1],f[v][2]);
        f[x][1]+=min(f[v][2],f[v][1]);
        d=min(d,f[v][2]-min(f[v][2],f[v][1]));
        //强制某个选f[v][2]
        f[x][2]+=min(f[v][0],min(f[v][1],f[v][2])); 
    } 
    
    f[x][2]+=val[x];
    f[x][1]+=d;
}

int main()
{
    
    //freopen("in.txt","r",stdin);
    rd(n);
    int x,y,cnt;
    inc(i,1,n)
    {
        rd(x);
        rd(val[x]);
        rd(cnt);
        inc(i,1,cnt)
        {
            rd(y);
            vis[y]=1;
            G[x].push_back(y);
        }
    } 
    
    inc(i,1,n)
    if(!vis[i])
    {
        rt=i;
        break;
    }
    dfs(rt);
    
    printf("%d",min(f[rt][1],f[rt][2]));
    re 0;
}

 

posted @ 2019-08-30 08:17  凉如水  阅读(216)  评论(0编辑  收藏  举报