# POJ 2125 Destroying The Graph 题解【最小点权覆盖集】【网络流】【最小割方案】

## 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\le N\le 100, 1\le M\le 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 $10^6$ . 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.

## Sample Input

3 6
1 2 3
4 2 1
1 2
1 1
3 2
1 2
3 1
2 3


## Sample Output

5
3
1 +
2 -
2 +


## Source

Northeastern Europe 2003, Northern Subregion

## Code：

#include<cstdio>
#include<cstring>
int Min(int x,int y)
{
return x<y?x:y;
}
struct edge
{
int n,nxt,v;
edge(int n,int nxt,int v)
{
this->n=n;
this->nxt=nxt;
this->v=v;
}
edge(){}
}e[20000];
{
}
int d[250],q[250];
bool bfs()
{
int l=0,r=0;
memset(d,0,sizeof(d));
q[++r]=233;
d[233]=1;
while(l<r)
{
int x=q[++l];
if(e[i^1].v&&!d[e[i].n])
{
d[e[i].n]=d[x]+1;
q[++r]=e[i].n;
}
}
return d[0]>0;
}
int dinic(int x,int in)
{
if(x==233)
return in;
int flow=in;
if(e[i].v&&d[e[i].n]==d[x]-1)
{
int tmp=dinic(e[i].n,Min(e[i].v,flow));
if(!tmp)
d[e[i].n]=0;
e[i].v-=tmp;
e[i^1].v+=tmp;
flow-=tmp;
}
return in-flow;
}
bool vis[250],used[250];
void dfs(int x)
{
vis[x]=1;
if(e[i].v&&!vis[e[i].n])
dfs(e[i].n);
}
int main()
{
int n,m,u,v;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
{
scanf("%d",&u);
}
for(int i=1;i<=n;++i)
{
scanf("%d",&u);
}
for(int i=1;i<=m;++i)
{
scanf("%d%d",&u,&v);
}
int ans=0;
while(bfs())
{
int tmp;
do
{
tmp=dinic(0,0x3fffffff);
ans+=tmp;
}while(tmp);
}
printf("%d\n",ans);
dfs(0);
ans=0;
if(!vis[e[i].n]&&!used[e[i].n])
++ans,used[e[i].n]=1;
if(vis[e[i].n]&&!used[e[i].n])
++ans,used[e[i].n]=1;
printf("%d\n",ans);
memset(used,0,sizeof(used));
if(!vis[e[i].n]&&!used[e[i].n])
printf("%d -\n",e[i].n),used[e[i].n]=1;