POJ2125 Destroying The Graph (最小点权覆盖集)(网络流最小割)
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 8158 | Accepted: 2620 | Special Judge |
Description
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.
Output
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 (最小点权覆盖集)(网络流最小割)的更多相关文章
- POJ 2125 Destroying The Graph (二分图最小点权覆盖集+输出最小割方案)
题意 有一个图, 两种操作,一种是删除某点的所有出边,一种是删除某点的所有入边,各个点的不同操作分别有一个花费,现在我们想把这个图的边都删除掉,需要的最小花费是多少. 思路 很明显的二分图最小点权覆盖 ...
- 最小点权覆盖集&最大点权独立集
最小点权覆盖集 二分图最小点权覆盖集解决的是这样一个问题: 在二分图中,对于每条边,两个端点至少选一个,求所选取的点最小权值和. 方法: 1.先对图二分染色,对于每条边两端点的颜色不同 2.然后建立源 ...
- POJ2125 Destroying The Graph(二分图最小点权覆盖集)
最小点权覆盖就是,对于有点权的有向图,选出权值和最少的点的集合覆盖所有的边. 解二分图最小点权覆盖集可以用最小割: vs-X-Y-vt这样连边,vs和X部点的连边容量为X部点的权值,Y部和vt连边容量 ...
- HDU 1569 - 方格取数(2) - [最大点权独立集与最小点权覆盖集]
嗯,这是关于最大点权独立集与最小点权覆盖集的姿势,很简单对吧,然后开始看题. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1569 Time Limi ...
- hdu1569 方格取数(2) 最大点权独立集=总权和-最小点权覆盖集 (最小点权覆盖集=最小割=最大流)
/** 转自:http://blog.csdn.net/u011498819/article/details/20772147 题目:hdu1569 方格取数(2) 链接:https://vjudge ...
- POJ 2125 最小点权覆盖集(输出方案)
题意:给一个图(有自回路,重边),要去掉所有边,规则:对某个点,可以有2种操作:去掉进入该点 的所有边,也可以去掉出该点所有边,(第一种代价为w+,第二种代价为w-).求最小代价去除所有边. 己思:点 ...
- poj2125 最小点权覆盖集
题意:有一张图,对于每个点,有出边和入边,现在目的是删除改图的所有边,对于每个点,删除出边的花费Wi-,删除入边的花费Wi+,现在的目的求删去所有边后的花费最小. 建图方法:对于每个点i,拆点为i,i ...
- HDU 1565 - 方格取数(1) - [状压DP][网络流 - 最大点权独立集和最小点权覆盖集]
题目链接:https://cn.vjudge.net/problem/HDU-1565 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32 ...
- POJ 2125 Destroying the Graph 二分图最小点权覆盖
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8198 Accepted: 2 ...
随机推荐
- POJ 1739
楼教主男人八题之一... 题目大意: 求从左下角经过所有非障碍点一次到达右下角的方案数 这里不是求回路,但是我们可以考虑,在最下面一行再增加一行,那么就可以当做求此时左下角到右下角的回路总数,那么就转 ...
- navtab方法参数以及事件
参数(options) DOM方式初始化navtab的,推荐使用集合属性data-options定义参数,如果使用data属性定义参数,注意转换成对应的名称. 名称 类型 默认值 描述 id stri ...
- 禁用gridview默认点击效果
cf_gridview.setSelector(new ColorDrawable(Color.TRANSPARENT)); 然后自己给做一个按下的效果xml文件
- 从对SAE的一次授权安全评估浅谈云安全
EMail: jianxin#80sec.comSite: http://www.80sec.comDate: 2011-12-20From: http://www.80sec.com/ [ 目录 ...
- hdu 2083
ps:N个数中,中位数是最小距离...第一次WA是因为排序之后最小和最大相加除2...应该是找他们的中位数,而不是中间数. 代码: #include "stdio.h" #incl ...
- Why am I getting an error converting a Foo** → const Foo**?
Because converting Foo** → const Foo** would be invalid and dangerous. C++ allows the (safe) convers ...
- (spring-第5回【IoC基础篇】)spring容器从加载配置文件到实例化bean的内部工作机制
前面讲过,spring的生命周期为:实例化前奏-->实例化-->实例化后期-->初始化前期-->初始化-->初始化后期-->bean的具体调用-->销毁前-- ...
- NSURLSession概述
NSURLSession是iOS7中新的网络接口,它与咱们熟悉的NSURLConnection是并列的.在程序在前台时,NSURLSession与NSURLConnection可以互为替代工作.注意, ...
- 数据结构《14》----并查集 Union-Find
描述: 并查集是一种描述解决等价关系.能够方便地描述不相交的多个集合. 支持如下操作 1. 建立包含元素 x 的集合 MakeSet(x) 2. 查找给定元素所在的集合 Find(x), 返回 ...
- 用python做些有意思的事——分析QQ聊天记录——私人订制
之前,写了这篇文章,用python提取全部群成员的发言时间,并简单做了下分析.先补充一下,针对特定单个群成员(这里以 小小白 为例)消息记录的获取. 代码比较简单,主要是正则表达式的书写.(附: ...