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 ...
随机推荐
- 通过代码自定义cell 新浪微博页面显示
通过代码自定义cell(cell的高度不一致)(如果高度一致的cell 用xib实现) 1.新建一个集成自UItableVIewCell的类 2.重写initWithStle :方法 - (insta ...
- powershell 判断操作系统版本 命令
powershell 传教士 原创文章.始于 2015-12-15 允许转载,但必须保留名字和出处,否则追究法律责任 一 前言 判断操作系统版本,是个老话题,bat.vbs中都有例子,这本不是重要问题 ...
- Android 读写SD卡的文件
今天介绍一下Android 读写SD卡的文件,要读写SD卡上的文件,首先需要判断是否存在SD卡,方法: Environment.getExternalStorageState().equals(Env ...
- linux exec用法总结
Linux中exec的用法总结 先总结一个表: exec命令 作用 exec ls 在shell中执行ls,ls结果显示结束后不返回原来的的目录中,而是/(根目录) exec <file 将fi ...
- Android-LogCat日志工具(一)
LogCat : Android中一个命令行工具,可以用于得到程序的log信息. 就像你知道一个人的日志.航程,你可以无时无刻知道一个人在干什么. 而LogCat , 就是程序的日志.通过日志,你可以 ...
- hibernate缓存和提高效率
1.使用二级缓存,多把大批量的.短期多次的查询数据存到二级缓存中,避免和数据库的多次交互,增加负担.二级缓存加在那些增删改少的,查询多的类中.二级缓存的是对象,如果查出来的不是对象,不会放到缓存中去. ...
- 团队开发——冲刺1.a
冲刺阶段一(第一天) 1.今天准备做什么? 在了解C#的基础上,深入熟悉Windows窗体应用程序,熟练掌握基本功能. 2.明天做什么:简单设计界面.
- 文件IO和标准IO
2015.2.26 星期四,阴天 今天的内容主要是文件IO man 手册的分册: man -f open 查看那些分册中有openman 1 -- 普通的命令程序man 2 -- 系统调用man 3 ...
- Asp.Net请求管道中的19个事件
请求管道中的19个事件.(1)BeginRequest: 开始处理请求(2)AuthenticateRequest授权验证请求,获取用户授权信息(3):PostAuthenticateRequest获 ...
- 【大胃王】2013暴食女王巅峰战(安吉拉x三宅x正司x木下)熟肉+高能
一边说着“不可以还没试就先放弃”,一边认真吃饭的女生真的让人抵抗不了啊. http://www.bilibili.com/video/av2395980/