AcWing 1077. 皇宫看守
原题链接
中等
作者:
最后五分钟
,
2023-08-05 22:41:54
,
所有人可见
,
阅读 50
#include<bits/stdc++.h>
using namespace std;
const int N=1510;
int w[N],e[N],ne[N],h[N],idx,st[N],f[N][3];
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u)
{
f[u][2]=w[u];
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
dfs(j);
f[u][0]+=min(f[j][1],f[j][2]);
f[u][2]+=min(min(f[j][0],f[j][1]),f[j][2]);
}
f[u][1]=1e9;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
f[u][1]=min(f[u][1],f[u][0]-min(f[j][1],f[j][2])+f[j][2]);
}
}
int main()
{
int n;
memset(h,-1,sizeof h);
cin>>n;
for(int i=1;i<=n;i++)
{
int a;
cin>>a>>w[a];
int m;cin>>m;
while(m--)
{
int b;
cin>>b;
add(a,b);
st[b]=1;
}
}
int root=1;
while(st[root]==1)root++;
dfs(root);
//cout<<f[root][1]<<endl;
cout<<min(f[root][1],f[root][2])<<endl;
return 0;
}