Description
Input
Output
输出文件包含一个整数,为你破译得到的最大价值
Sample Input
7
0 2 2 7
1 0
1 0
-1 3 1 6 7
1 2 1 6
1 2 2 7
-1 0
Sample Output
3
思路:
附代码实现:
#include<cstdio>
#include<cstring>
using namespace std;
const int N=505;
const int M=900005;
int n,tot=1,one,cur[N],final[N],to[M],nt[M],c[M];
int d[N],q[M];
void link(int x,int y,int f) {
tot++;
to[tot]=y;
nt[tot]=final[x];
c[tot]=f;
final[x]=tot;
}
bool bfs() {
int head=1,last=1;
for(int i=0;i<=n+1;i++) cur[i]=final[i];
memset(d,0,sizeof(d));
q[1]=0;
d[0]=1;
while(head<=last) {
int x=q[head];
for(int i=cur[x];i;i=nt[i]) {
int y=to[i];
if(c[i]&&!d[y]) {
d[y]=d[x]+1;
q[++last]=y;
}
}
head++;
}
return d[n+1];
}
int min(int a,int b) {
return a<b?a:b;
}
int dfs(int x,int f) {
if(x==n+1||f==0) return f;
int f1=f;
for(int i=cur[x];i&&f1;i=nt[i]) {
int y=to[i];
cur[x]=i;
if(c[i]&&d[y]==d[x]+1) {
int canf=min(c[i],f1);
int flow=dfs(y,canf);
if(flow==0) d[y]=0;
f1-=flow;
c[i]-=flow;
c[i^1]+=flow;
}
}
return f-f1;
}
int dinic() {
int s=0,nw;
while(bfs()) while(nw=dfs(0,1e9)) s+=nw;
return s;
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++) {
int x,v,n1;
scanf("%d%d",&v,&n1);
if(v==1) {
link(0,i,1);
link(i,0,0);
one++;
}
if(v==-1) {
link(i,n+1,1);
link(n+1,i,0);
}
for(int j=1;j<=n1;j++) {
scanf("%d",&x);
link(i,x,1e9);
link(x,i,0);
}
}
printf("%d",one-dinic());
}
红妆素裹,分外妖娆