You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
949 B

#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=1510;
int n;
int h[N],e[N],ne[N],idx;
int w[N];
int f[N][3];
int in[N];
void add(int a,int b){
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u){
f[u][0]=0;
f[u][1]=INF ;
f[u][2]=w[u];
for(int i=h[u];~i;i=ne[i]){
int v=e[i];
dfs(v);
f[u][0]+=min(f[v][1],f[v][2]);
f[u][2]+=min({f[v][0],f[v][1],f[v][2]});
}
for(int i=h[u];~i;i=ne[i]){
int v=e[i];
f[u][1]=min(f[u][1],f[v][2]+f[u][0]-min(f[v][1],f[v][2]));
}
}
int main() {
memset(h,-1,sizeof h);
cin>>n;
for (int i = 1; i <= n; i++){
int x,m;
cin>>x>>w[x]>>m;
while(m--){
int y;
cin>>y;
}
}
int root=1;
while(in[root])root++;
dfs(root);
printf("%d\n",min(f[root][1],f[root][2]));
return 0;
}