最小割模型。
我们把点i拆成两个点i1和i2,把连入i点的边连到点i1,把从i点连出的边连到点i2,再在点i1和i2中间连流量为1的边,问题就转化成了最小割,割掉i1和i2中间的边就是踩坏第i台电脑。
然后跑最大流就好了。
Code:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
const int INF=0x7fffffff;
struct node{int x,y,c,next,other;}a[5010];
int n,m,s,t,len(0),ans(0),first[210],h[210],q[210];
void ins(int x,int y,int c)
{
int k1,k2;
len++;k1=len;
a[len].x=x;a[len].y=y;a[len].c=c;
a[len].next=first[x];first[x]=len;
len++;k2=len;
a[len].x=y;a[len].y=x;a[len].c=0;
a[len].next=first[y];first[y]=len;
a[k1].other=k2;a[k2].other=k1;
}
bool bfs()
{
int head=1,tail=2;q[1]=s;
memset(h,0,sizeof(h));h[s]=1;
while(head!=tail)
{
int x=q[head];
for(int i=first[x];i;i=a[i].next)
{
int y=a[i].y;
if(h[y]==0 && a[i].c>0)
{
q[tail++]=y;
h[y]=h[x]+1;
}
}
head++;
}
if(h[t]==0) return false;
return true;
}
int dfs(int x,int flow)
{
if(x==t) return flow;
int tt=0,minf=0;
for(int i=first[x];i;i=a[i].next)
{
int y=a[i].y;
if(a[i].c>0 && h[y]==(h[x]+1) && tt<flow)
{
minf=dfs(y,min(flow-tt,a[i].c));
tt+=minf;
a[i].c-=minf;a[a[i].other].c+=minf;
}
}
if(tt==0) h[x]=0;
return tt;
}
void dinic()
{
while(bfs())
ans+=dfs(s,INF);
}
int main()
{
scanf("%d %d %d %d",&n,&m,&s,&t);t+=n;
memset(first,0,sizeof(first));
for(int i=1;i<=n;i++)
ins(i+n,i,1);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d %d",&x,&y);
ins(x,y+n,INF);
ins(y,x+n,INF);
}
dinic();
printf("%d",ans);
}