AcWing 1077. 皇宫看守
原题链接
中等
作者:
最后五分钟
,
2024-04-01 23:38:08
,
所有人可见
,
阅读 4
#include<bits/stdc++.h>
#define int long long
#define x first
#define y second
#define de(x) cout<<#x<<" = "<<x<<" "
#define deg(x) cout<<#x<<" = "<<x<<endl
using namespace std;
const int N=1510,M=2*N;
typedef pair<int,int> PII;
int n;
int w[N];
int h[N],e[M],ne[M],idx;
int f[N][3];
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u,int fa)
{
f[u][1]=w[u];
int sum=0;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==fa)continue;
dfs(j,u);
sum+=min(f[j][0],f[j][1]);
}
f[u][0]=1e13;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==fa)continue;
f[u][1]+=min(min(f[j][0],f[j][1]),f[j][2]);
f[u][0]=min(f[u][0],sum-min(f[j][1],f[j][0])+f[j][1]);
f[u][2]+=f[j][0];
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
memset(h,-1,sizeof h);
for(int i=1;i<=n;i++)
{
int a;
cin>>a;
cin>>w[a];
int k;
cin>>k;
for(int j=1;j<=k;j++)
{
int b;
cin>>b;
add(a,b),add(b,a);
}
}
dfs(1,-1);
cout<<min(f[1][0],f[1][1])<<endl;
return 0;
}