经典题不解释。。(貌似代码写的很挫)
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INTMAX 2000000000
using namespace std;
int f[2000][3],b[2000][2000],c[2000],inf[2000];
int dp(int i,int j)
{
int ans=0;
if(f[i][j]>0) return f[i][j];
if(j==1)
{
if(b[i][0]==0) return c[i];
for(int k=1;k<=b[i][0];k++)
ans+=min(dp(b[i][k],1),min(dp(b[i][k],2),dp(b[i][k],3)));
ans+=c[i];
f[i][j]=ans;
return ans;
}
if(j==2)
{
int sum=0; ans=INTMAX;
if(b[i][0]==0) return INTMAX;
for(int k=1;k<=b[i][0];k++)
{
ans=min(ans,dp(b[i][k],1)-min(dp(b[i][k],1),dp(b[i][k],2)));
sum+=min(dp(b[i][k],1),dp(b[i][k],2));
}
ans+=sum;
f[i][j]=ans;
return ans;
}
if(j==3)
{
if(b[i][0]==0) return 0;
for(int k=1;k<=b[i][0];k++)
ans+=min(dp(b[i][k],1),dp(b[i][k],2));
f[i][j]=ans;
return ans;
}
}
int main()
{
int n,root=INTMAX,t=0,x;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
scanf("%d%d",&c[x],&b[x][0]);
for(int j=1;j<=b[x][0];j++)
{
scanf("%d",&b[x][j]); inf[b[x][j]]++;
}
}
for(int i=1;i<=n;i++)
if(inf[i]==0){root=i;break;}
printf("%d",min(dp(root,1),dp(root,2)));
return 0;
}