题目链接:http://poj.org/problem?id=2125
拆点成二分图后,可以看出问题有点类似于二分图最大点覆盖集,实际上它是最大点权覆盖集,根据对偶原理,把二分图转化,二分图原有的边变为inf,s->左部点,容量为点权,右部点->t,容量同样为点权,跑最大流,就是最大点权覆盖集。
关键是判断割边。这里的割边代表“割点”,即拆掉点的入度或点的出度,可以用一遍dfs实现。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N=500;
const int M=40000;
const int inf=0x3fffffff;
int head[N],d[N],edge[M],ver[M],next[M];
bool v[N];
int n,m,s,t,tot=1,maxflow=0;
void add (int u,int v,int cap) {
ver[++tot]=v;edge[tot]=cap;next[tot]=head[u];head[u]=tot;
ver[++tot]=u;edge[tot]=0; next[tot]=head[v];head[v]=tot;
}
bool bfs () {
memset(d,0,sizeof(d));
queue<int> q;
q.push(s); d[s]=1;
while (!q.empty()) {
int x=q.front(); q.pop();
for (int i=head[x];i;i=next[i])
if (edge[i] && d[ver[i]]==0) {
q.push(ver[i]);
d[ver[i]]=d[x]+1;
if (ver[i]==t) return true;
}
}
return false;
}
int dinic (int x,int f) {
if (x==t) return f;
int rest=f,now;
for (int i=head[x];i&&rest;i=next[i])
if (edge[i]&&d[ver[i]]==d[x]+1) {
now=dinic(ver[i],min(rest,edge[i]));
if (!now) d[ver[i]]=0;
edge[i]-=now;
edge[i^1]+=now;
rest-=now;
}
return f-rest;
}
void dfs (int u) {
v[u]=1;
for (int i=head[u];i;i=next[i])
if (!v[ver[i]]&&edge[i])
dfs(ver[i]);
}
int main () {
int x,y,w,ans=0;
while (scanf("%d%d",&n,&m)!=EOF) {
s=0;t=2*n+1;tot=1;maxflow=ans=0;
memset(head,0,sizeof(head));
memset(v,0,sizeof(v));
for (int i=1;i<=n;i++) {
scanf("%d",&x);
add(i+n,t,x);
}
for (int i=1;i<=n;i++) {
scanf("%d",&x);
add(s,i,x);
}
for (int i=1;i<=m;i++) {
scanf("%d%d",&x,&y);
add(x,y+n,inf);
}
while (bfs())
while (w=dinic(s,inf)) maxflow+=w;
printf("%d\n",maxflow);
dfs(s);
for (int i=1;i<=n;i++)
ans+=!(v[i])+(v[i+n]);
printf("%d\n",ans);
for (int i=1;i<=n;i++) {
if (v[i+n]) printf("%d +\n",i);
if (!v[i]) printf("%d -\n",i);
}
}
return 0;
}