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.

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.

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 ;
}

POJ2125 Destroying The Graph (最小点权覆盖集)(网络流最小割)的更多相关文章

  1. POJ 2125 Destroying The Graph (二分图最小点权覆盖集+输出最小割方案)

    题意 有一个图, 两种操作,一种是删除某点的所有出边,一种是删除某点的所有入边,各个点的不同操作分别有一个花费,现在我们想把这个图的边都删除掉,需要的最小花费是多少. 思路 很明显的二分图最小点权覆盖 ...

  2. 最小点权覆盖集&最大点权独立集

    最小点权覆盖集 二分图最小点权覆盖集解决的是这样一个问题: 在二分图中,对于每条边,两个端点至少选一个,求所选取的点最小权值和. 方法: 1.先对图二分染色,对于每条边两端点的颜色不同 2.然后建立源 ...

  3. POJ2125 Destroying The Graph(二分图最小点权覆盖集)

    最小点权覆盖就是,对于有点权的有向图,选出权值和最少的点的集合覆盖所有的边. 解二分图最小点权覆盖集可以用最小割: vs-X-Y-vt这样连边,vs和X部点的连边容量为X部点的权值,Y部和vt连边容量 ...

  4. HDU 1569 - 方格取数(2) - [最大点权独立集与最小点权覆盖集]

    嗯,这是关于最大点权独立集与最小点权覆盖集的姿势,很简单对吧,然后开始看题. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1569 Time Limi ...

  5. hdu1569 方格取数(2) 最大点权独立集=总权和-最小点权覆盖集 (最小点权覆盖集=最小割=最大流)

    /** 转自:http://blog.csdn.net/u011498819/article/details/20772147 题目:hdu1569 方格取数(2) 链接:https://vjudge ...

  6. POJ 2125 最小点权覆盖集(输出方案)

    题意:给一个图(有自回路,重边),要去掉所有边,规则:对某个点,可以有2种操作:去掉进入该点 的所有边,也可以去掉出该点所有边,(第一种代价为w+,第二种代价为w-).求最小代价去除所有边. 己思:点 ...

  7. poj2125 最小点权覆盖集

    题意:有一张图,对于每个点,有出边和入边,现在目的是删除改图的所有边,对于每个点,删除出边的花费Wi-,删除入边的花费Wi+,现在的目的求删去所有边后的花费最小. 建图方法:对于每个点i,拆点为i,i ...

  8. HDU 1565 - 方格取数(1) - [状压DP][网络流 - 最大点权独立集和最小点权覆盖集]

    题目链接:https://cn.vjudge.net/problem/HDU-1565 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32 ...

  9. POJ 2125 Destroying the Graph 二分图最小点权覆盖

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8198   Accepted: 2 ...

随机推荐

  1. CodeForces 468A Program F

    Description Little X used to play a card game called "24 Game", but recently he has found ...

  2. C++中两块内存重叠的string的copy方法

    如果两段内存重叠,用memcpy函数可能会导致行为未定义. 而memmove函数能够避免这种问题,下面是一种实现方式: #include <iostream> using namespac ...

  3. java基础之hashmap

    Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.网上关于hashmap的文章很多,但到底是自己学习的总结,就发出来跟大家一起分享,一起讨论. 1.hashma ...

  4. [转][C/C++]函数名字修饰(Decorated Name)方式

    1.C/C++函数修饰名: 对于我们的C/C++源程序而言,函数名只是函数的一小部分,函数还有调用方式(参数入栈方式).返回值类型.参数个数和各参数类型等信息,对于C++类成员函数,还有更多信息.这些 ...

  5. sql 语句中使用条件判断case then else end

    sql 语句中使用条件判断case then else end范例: SELECT les.[nLessonNo] FROM BS_Lesson AS les WHERE les.[sClassCod ...

  6. python——周边

    Pythonic的禅意 import this python是用c语言写的.传说python不止有C语言实现,还有java实现,还有python实现的python,甚至还有js实现的python. p ...

  7. 更新安装xcode7插件

    mkdir -p ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-inscurl -fsSL https://raw.github ...

  8. About View

    View Geometry Frame & Bounds Graphically, a view can be regarded as a framed canvas. The frame l ...

  9. 谈谈你对http的理解

    一.先看一张图: 二.要client与sever能沟通 1.需要一样的规则,遵循一定的规范-----------协议 好比:不同国家的人,需要一门通用的语言. 2.谈到协议------http---- ...

  10. 爬虫再探实战(五)———爬取APP数据——超级课程表【四】——情感分析

    仔细看的话,会发现之前的词频分析并没有什么卵用...文本分析真正的大哥是NLP,不过,这个坑太大,小白不大敢跳...不过还是忍不住在坑边上往下瞅瞅2333. 言归正传,今天刚了解到boson公司有py ...