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 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.
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.
3 6
1 2 3
4 2 1
1 2
1 1
3 2
1 2
3 1
2 3
5
3
1 +
2 -
2 +
Northeastern Europe 2003, Northern Subregion
每条边有被+操作破坏和被-操作破坏两种情况,因为每条边都要被破坏,所以一条边的+和-操作至少要执行一次,这类似于点覆盖。
把操作当成点,每条边就对应两个操作,求最小点权覆盖集就行了。
建边的地方好长时间没想明白……把每个点拆成入点(+)和出点(-),s向出点(-)连容量为权值的边,入点(+)向e连容量为权值的边,然后出点向入点连原图的边,容量是INF。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 1000000010;
const int SZ = 1000010;
int head[SZ],nxt[SZ],tot = 1,n,m;
struct edge{
int t,d;
}l[SZ];
void build(int f,int t,int d)
{
l[++ tot].t = t;
l[tot].d = d;
nxt[tot] = head[f];
head[f] = tot;
}
void insert(int f,int t,int d)
{
build(f,t,d); build(t,f,0);
}
int deep[SZ];
queue<int> q;
bool bfs(int s,int e)
{
memset(deep,0,sizeof(deep));
deep[s] = 1;
while(q.size()) q.pop();
q.push(s);
while(q.size())
{
int u = q.front(); q.pop();
for(int i = head[u];i;i = nxt[i])
{
int v = l[i].t;
if(!deep[v] && l[i].d)
{
deep[v] = deep[u] + 1;
q.push(v);
if(v == e) return true;
}
}
}
return false;
}
int dfs(int u,int flow,int e)
{
if(u == e || flow == 0) return flow;
int rest = flow;
for(int i = head[u];i;i = nxt[i])
{
int v = l[i].t;
if(deep[v] == deep[u] + 1 && l[i].d)
{
int f = dfs(v,min(rest,l[i].d),e);
if(f > 0)
{
l[i].d -= f;
l[i ^ 1].d += f;
rest -= f;
if(rest == 0) break;
}
else deep[v] = 0;
}
}
return flow - rest;
}
int dinic(int s,int e)
{
int ans = 0;
while(bfs(s,e)) ans += dfs(s,INF,e);
return ans;
}
bool vis[SZ];
void dfs(int u)
{
vis[u] = 1;
for(int i = head[u];i;i = nxt[i])
if(!vis[l[i].t] && l[i].d)
dfs(l[i].t);
}
int ff[SZ],tt[SZ],ans[SZ];
int main()
{
scanf("%d%d",&n,&m);
int s = n + n + 1;
int e = n + n + 2;
for(int i = 1;i <= n;i ++) //入边
{
int x;
scanf("%d",&x);
insert(i + n,e,x);
}
for(int i = 1;i <= n;i ++) //出边
{
int x;
scanf("%d",&x);
insert(s,i,x);
}
int sum = 0;
for(int i = 1;i <= m;i ++)
{
int a,b;
scanf("%d%d",&a,&b);
insert(a,b + n,INF);
ff[i] = a;tt[i] = b + n;
}
printf("%d\n",dinic(s,e));
dfs(s);
for(int i = 1;i <= 2 * n;i ++)
{
if(vis[i] && i > n)
ans[++ ans[0]] = i;
if(!vis[i] && i <= n)
ans[++ ans[0]] = i;
}
printf("%d\n",ans[0]);
for(int i = 1;i <= 2 * n;i ++)
{
if(vis[i] && i > n)
printf("%d +\n",i - n);
if(!vis[i] && i <= n)
printf("%d -\n",i);
}
return 0;
}