Description Alice and Bob play the following game. First, Alice draws
some directed graph with N vertices and M arcs. After that Bob tries
to destroy it. In a move he may take any vertex of the graph and
remove either all arcs incoming into this vertex, or all arcs outgoing
from this vertex. Alice assigns two costs to each vertex: Wi+ and Wi-.
If Bob removes all arcs incoming into the i-th vertex he pays Wi+
dollars to Alice, and if he removes outgoing arcs he pays Wi- dollars.
Find out what minimal sum Bob needs to remove all arcs from the graph.Input Input file describes the graph Alice has drawn. The first line
of the input file contains N and M (1 <= N <= 100, 1 <= M <= 5000).
The second line contains N integer numbers specifying Wi+. The third
line defines Wi- in a similar way. All costs are positive and do not
exceed 106 . Each of the following M lines contains two integers
describing the corresponding arc of the graph. Graph may contain loops
and parallel arcs.Output On the first line of the output file print W — the minimal
sum Bob must have to remove all arcs from the graph. On the second
line print K — the number of moves Bob needs to do it. After that
print K lines that describe Bob’s moves. Each line must first contain
the number of the vertex and then ‘+’ or ‘-’ character, separated by
one space. Character ‘+’ means that Bob removes all arcs incoming into
the specified vertex and ‘-’ that Bob removes all arcs outgoing from
the specified vertex.
把每个点的入点和出点分成两部中间连的边就是原图中的边,求最小割。
输出方案可以从原点出发dfs一遍得到割成的两个集合,集合之间的边就是要割掉的边。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int s=205,t=206,oo=0x3f3f3f3f;
int fir[210],que[210],vis[210],dep[210],ne[50010],to[50010],w[50010],f[210],
n,m,num;
void add(int u,int v,int x)
{
num++;
ne[num*2]=fir[u];
fir[u]=num*2;
to[num*2]=v;
w[num*2]=x;
ne[num*2+1]=fir[v];
fir[v]=num*2+1;
to[num*2+1]=u;
w[num*2+1]=0;
}
bool bfs()
{
int hd,tl,u,v;
memset(dep,0,sizeof(dep));
dep[s]=1;
que[hd=tl=1]=s;
while (hd<=tl)
{
u=que[hd++];
for (int i=fir[u];i;i=ne[i])
if (w[i]&&!dep[v=to[i]])
{
dep[v]=dep[u]+1;
que[++tl]=v;
}
}
return dep[t];
}
int dfs(int u,int lim)
{
if (u==t) return lim;
int ret=0,v,x;
for (int i=fir[u];i&&ret<lim;i=ne[i])
if (w[i]&&dep[v=to[i]]==dep[u]+1)
{
x=dfs(v,min(lim-ret,w[i]));
w[i]-=x;
w[i^1]+=x;
ret+=x;
}
if (!ret) dep[u]=0;
return ret;
}
void dfs(int u)
{
int v;
vis[u]=1;
for (int i=fir[u];i;i=ne[i])
if (w[i]&&!vis[v=to[i]])
dfs(v);
}
int main()
{
int u,v,x,ans=0,tot=0;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
scanf("%d",&x);
add(i*2+1,t,x);
}
for (int i=1;i<=n;i++)
{
scanf("%d",&x);
add(s,i*2,x);
}
for (int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
add(u*2,v*2+1,oo);
}
while (bfs()) ans+=dfs(s,oo);
printf("%d\n",ans);
dfs(s);
for (int i=fir[s];i;i=ne[i])
if (!w[i]&&!vis[v=to[i]])
f[++tot]=v;
for (int i=fir[t];i;i=ne[i])
if (!w[i^1]&&vis[v=to[i]])
f[++tot]=v;
printf("%d\n",tot);
for (int i=1;i<=tot;i++)
printf("%d %c\n",f[i]/2,(f[i]&1)?'+':'-');
}