Destroying The Graph
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 8158 | Accepted: 2620 | Special Judge |
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.
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.
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.
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 +
【分析】首先得拆点,一个点拆成in和out。很明显就是求最小点权覆盖集。最小点权覆盖集的求解可以借鉴二分图匹配的最大流解法。
再加上额外的源点S和汇点T后,将匹配以一条s-u-v-t形式的流路径串联起来。匹配的限制在顶点上,恰当的利用了流的容量限制。
而点覆盖集的限制在边上,最小割是最大流的对偶问题,对偶往往是将问题的性质从顶点转边,从边转顶点。可以尝试转最小割模型。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
#define inf 0x7fffffff
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int read() {int x=,f=;char c=getchar();while(c<''||c>'') {if(c=='-')f=-;c=getchar();}while(c>=''&&c<='') {x=x*+c-'';c=getchar();}return x*f;}
int n,m,cnt;
int win[N],wout[N];
bool flag;
int toto=;
struct Dinic {
int s,t;
struct Edge {
int nxt,to,cap,flow;
} edg[M];
bool vv[N];
bool vis[N];
int d[N];
int h[N];
int cur[N];
void init() {
met(h,-);
}
void AddEdge(int x,int y,int z) {
edg[toto].to=y;
edg[toto].nxt=h[x];
edg[toto].cap=z;
h[x]=toto++;
edg[toto].to=x;
edg[toto].nxt=h[y];
h[y]=toto++;
}
bool BFS() {
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=;
vis[s]=;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = h[x]; i!=-; i=edg[i].nxt) {
int v=edg[i].to;
if (!vis[v] && edg[i].cap > edg[i].flow) {
vis[v]=;
d[v] = d[x]+;
q.push(v);
if(flag)vv[v]=true;
}
}
}
return vis[t];
} int DFS(int x,int a) {
if (x==t || a==)
return a;
int flow = ,f;
for(int &i=cur[x]; i!=-; i=edg[i].nxt) {
int v=edg[i].to;
if (d[x]+ == d[v] && (f=DFS(v,min(a,edg[i].cap-edg[i].flow)))>) {
edg[i].flow+=f;
edg[i^].flow-=f;
flow+=f;
a-=f;
if (a==)
break;
}
}
return flow;
} int Maxflow(int s,int t) {
this->s=s;
this->t=t;
int flow = ;
while (BFS()) {
for(int i=; i<=t; i++)cur[i]=h[i];
flow+=DFS(s,inf);
}
return flow;
} } dc; int main() {
while (~scanf("%d%d",&n,&m)) {
dc.init();
met(wout,);
met(win,);
flag=false;
for(int i=; i<=n; i++)win[i]=read();
for(int i=; i<=n; i++)wout[i]=read();
while(m--) {
int u=read();
int v=read();
dc.AddEdge(u,v+n,inf);
}
for(int i=; i<=n; i++) {
dc.AddEdge(,i,wout[i]);
dc.AddEdge(i+n,*n+,win[i]);
}
printf("%d\n",dc.Maxflow(,*n+));
int sum=;
flag=true;
dc.BFS();
for(int i=; i<=n; i++) {
if(!dc.vv[i])sum++;
if(dc.vv[n+i])sum++;
}
printf("%d\n",sum);
for(int i=; i<=n; i++) {
if(!dc.vv[i])printf("%d -\n",i);
if(dc.vv[n+i])printf("%d +\n",i);
}
}
return ;
}