# BZOJ 2502: 清理雪道

Time Limit: 10 Sec
Memory Limit: 128 MB

Description

Input

Output

   输出文件的第一行是一个整数k – 直升飞机的最少飞行次数。


Sample Input

8

1 3

1 7

2 4 5

1 8

1 8

0

2 6 5

0

Sample Output

4
HINT

Source

2011福建集训

#### Code####


#include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int read()
{
int s=0,f=1;char ch=getchar();
while(!('0'<=ch&&ch<='9')){if(ch=='-')f=-1;ch=getchar();}
while('0'<=ch&&ch<='9'){s=(s<<3)+(s<<1)+ch-'0';ch=getchar();}
return s*f;
}
int S,T,SS,TT,n,m,np;
int A[105],B[105],jr[10005],jc[10005];
int be[100005],bn[200005],bv[200005],bl[200005],bw=1;
void put(int u,int v,int l)
{bw++;bn[bw]=be[u];be[u]=bw;bv[bw]=v;bl[bw]=l;}
int d[100005];
bool spfa(int S,int T)
{
for(int i=1;i<=np;i++)
d[i]=10000000;
d[S]=1;
queue<int>q;
for(q.push(S);!q.empty();)
{int u=q.front();q.pop();
for(int i=be[u],v;i;i=bn[i])
if(d[v=bv[i]]>d[u]+1&&bl[i])
{d[v]=d[u]+1;
q.push(v);
}
}
return d[T]!=10000000;
}
int ans;
int dinic(int u,int mf,int T)
{
if(mf==0)return 0;
if(u==T)return mf;
int sum=0;
for(int i=be[u],v;i;i=bn[i])
if(d[v=bv[i]]==d[u]+1)
{int f=dinic(v,min(mf-sum,bl[i]),T);
bl[i]-=f;
bl[i^1]+=f;
sum+=f;
}
return sum;
}
bool p[101][101];
int main()
{
n=read();
S=++np,T=++np;
for(int i=1;i<=n;i++)
A[i]=++np,
put(S,A[i],1000000),
put(A[i],S,0),
put(A[i],T,1000000),
put(T,A[i],0);
for(int i=1;i<=n;i++)
{int m=read();
for(int j=1,v;j<=m;j++)
v=read(),
put(A[i],A[v],1e9),
put(A[v],A[i],0),
jc[A[i]]++,jr[A[v]]++;
}
SS=++np,TT=++np;
int sumr=0;
for(int i=1;i<=np;i++)
{if(jr[i]-jc[i]>0)put(SS,i,jr[i]-jc[i]),put(i,SS,0),sumr+=jr[i]-jc[i];
else put(i,TT,jc[i]-jr[i]),put(TT,i,0);
}
put(T,S,1e9);
put(S,T,0);
while(spfa(SS,TT))ans+=dinic(SS,1e9,TT);
if(ans<sumr)
{printf("-1\n");return 0;}
ans=bl[bw];
bl[bw]=bl[bw-1]=0;
while(spfa(T,S))ans-=dinic(T,1e9,S);
printf("%d\n",ans);
return 0;
}


posted on 2016-03-04 15:27  wuyuhan  阅读(176)  评论(0编辑  收藏  举报