图论(网络流,二分图最小点权覆盖):POJ 2125 Destroying The Graph
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 +
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn=;
const int maxm=;
const int INF=;
int cnt,tot,fir[maxn],fron[maxn],dis[maxn];
int to[maxm],nxt[maxm],gap[maxn],path[maxn];
int cap[maxm];queue<int>q; struct Max_Flow{
void Init(int tot_=){
tot=tot_;cnt=;
memset(fir,,sizeof(fir));
memset(dis,,sizeof(dis));
memset(gap,,sizeof(gap));
} void add(int a,int b,int c){
nxt[++cnt]=fir[a];
fir[a]=cnt;
cap[cnt]=c;
to[cnt]=b;
} void addedge(int a,int b,int c){
add(a,b,c);
add(b,a,);
} bool BFS(int s,int t){
dis[t]=;q.push(t);
while(!q.empty()){
int x=q.front();q.pop();
for(int i=fir[x];i;i=nxt[i])
if(!dis[to[i]]){
dis[to[i]]=dis[x]+;
q.push(to[i]);
}
}
return dis[s];
} int Aug(int s,int t,int &p){
int f=INF;
while(p!=s){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}p=t;
while(p!=s){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
return f;
} int ISAP(int s,int t){
if(!BFS(s,t))return ;
for(int i=s;i<=t;i++)fron[i]=fir[i];
for(int i=s;i<=t;i++)gap[dis[i]]+=;
int p=s,ret=;
while(dis[s]<=tot){
if(p==t)ret+=Aug(s,t,p); for(int &i=fron[p];i;i=nxt[i])
if(cap[i]&&dis[p]==dis[to[i]]+){
path[p=to[i]]=i;
break;
} if(!fron[p]){
if(--gap[dis[p]]==)
break;
int Min=tot;
for(int i=fir[p];i;i=nxt[i])
if(cap[i])Min=min(Min,dis[to[i]]);
gap[dis[p]=Min+]+=;fron[p]=fir[p];
if(p!=s)p=to[path[p]^];
}
}
return ret;
}
}isap; int n,m,top;
int tag[maxn],st[maxn];
void DFS(int x){
tag[x]=;
for(int i=fir[x];i;i=nxt[i])
if(cap[i]&&!tag[to[i]])DFS(to[i]);
} int main(){
scanf("%d%d",&n,&m);
int s=,t=*n+;
isap.Init(t+);
for(int i=,v;i<=n;i++){
scanf("%d",&v);
isap.addedge(s,i,v);
}
for(int i=,v;i<=n;i++){
scanf("%d",&v);
isap.addedge(i+n,t,v);
}
for(int i=,a,b;i<=m;i++){
scanf("%d%d",&a,&b);
isap.addedge(b,a+n,INF);
} printf("%d\n",isap.ISAP(s,t));
DFS();
for(int i=;i<=n;i++){
if(!tag[i])
st[++top]=i;
if(tag[i+n])
st[++top]=i+n;
}
printf("%d\n",top);
for(int i=;i<=top;i++){
if(st[i]<=n)
printf("%d +\n",st[i]);
else
printf("%d -\n",st[i]-n);
}
return ;
}
图论(网络流,二分图最小点权覆盖):POJ 2125 Destroying The Graph的更多相关文章
- POJ 2125 Destroying the Graph 二分图最小点权覆盖
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8198 Accepted: 2 ...
- POJ 2125 Destroying The Graph (二分图最小点权覆盖集+输出最小割方案)
题意 有一个图, 两种操作,一种是删除某点的所有出边,一种是删除某点的所有入边,各个点的不同操作分别有一个花费,现在我们想把这个图的边都删除掉,需要的最小花费是多少. 思路 很明显的二分图最小点权覆盖 ...
- POJ 2125 Destroying The Graph 二分图 最小点权覆盖
POJ2125 题意简述:给定一个有向图,要通过某些操作删除所有的边,每一次操作可以选择任意一个节点删除由其出发的所有边或者通向它的所有边,两个方向有不同的权值.问最小权值和的解决方案,要输出操作. ...
- POJ2125 Destroying The Graph(二分图最小点权覆盖集)
最小点权覆盖就是,对于有点权的有向图,选出权值和最少的点的集合覆盖所有的边. 解二分图最小点权覆盖集可以用最小割: vs-X-Y-vt这样连边,vs和X部点的连边容量为X部点的权值,Y部和vt连边容量 ...
- poj 3308 Paratroopers(二分图最小点权覆盖)
Paratroopers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8954 Accepted: 2702 Desc ...
- POJ2125 Destroying The Graph 二分图 + 最小点权覆盖 + 最小割
思路来源:http://blog.csdn.net/lenleaves/article/details/7873441 求最小点权覆盖,同样求一个最小割,但是要求出割去了那些边, 只要用最终的剩余网络 ...
- poj 2125 Destroying The Graph (最小点权覆盖)
Destroying The Graph http://poj.org/problem?id=2125 Time Limit: 2000MS Memory Limit: 65536K ...
- POJ - 2125 Destroying The Graph (最小点权覆盖)
题意:给一张图,现在要删去所有的边,删去一个点的所有入边和所有出边都有其对应\(W_{i+}\)和\(W_{i-}\).求删去该图的最小花费,并输出解 分析:简而言之就是用最小权值的点集去覆盖所有的边 ...
- POJ3308 Paratroopers(最小割/二分图最小点权覆盖)
把入侵者看作边,每一行每一列都是点,选取某一行某一列都有费用,这样问题就是选总权最小的点集覆盖所有边,就是最小点权覆盖. 此外,题目的总花费是所有费用的乘积,这时有个技巧,就是取对数,把乘法变为加法运 ...
- POJ 2125 Destroying The Graph [最小割 打印方案]
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8311 Accepted: 2 ...
随机推荐
- SQL Cursor 基本用法
1 table1结构如下 2 id int 3 name varchar(50) 4 5 declare @id int 6 declare @name varchar(50) ...
- 如果 @s int 把它转成字符,可以这样 cast(@s as varchar)
如果 @s int 把它转成字符,可以这样 cast(@s as varchar)
- 新装的mysql,直接安装板
Windows安装MySQL解压版 http://www.cnblogs.com/xiaoit/p/3932241.html my文件 如下: [mysql]# 设置mysql客户端默认字符集defa ...
- 用户组,AD域控简介
“自由”的工作组 工作组(WORK GROUP)就是将不同的电脑按功能分别列入不同的组中,以方便管理.比如在一个网络内,可能有成百上千台工作电脑,如果这些电脑不进行分组,都列在“网上邻居”内,可 ...
- oracle-同义词,又学到东西了
select col1 from tab1@db_link1; create or replace synonym test123 as se,ect col1 from tab1@db_link ...
- 【转】伟大的RAC和MVVM入门(一)
原文:http://www.sprynthesis.com/2014/12/06/reactivecocoa-mvvm-introduction/ 翻译自ReactiveCocoa and MVV ...
- OpenCart本地调试环境搭建
OpenCart简介: 免费开源网络版电子商务系统,是建立在线商务网站首选之一.有众多用户和开发基础,结合其丰富特性与模板插件,可最大化定制在线商店.(也就是用来方便开网店的) 本地调试准备: Fir ...
- SGU 194. Reactor Cooling(无源汇有上下界的网络流)
时间限制:0.5s 空间限制:6M 题意: 显然就是求一个无源汇有上下界的网络流的可行流的问题 Solution: 没什么好说的,直接判定可行流,输出就好了 code /* 无汇源有上下界的网络流 * ...
- spring 构造注入 异常 Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments
你可能在做项目的时候,需要在项目启动时初始化一个自定义的类,这个类中包含着一个有参的构造方法,这个构造方法中需要传入一些参数. spring提供的这个功能叫“构造注入”, applicationCon ...
- js 的其它运算符和优先级
三元运算符: 语法为 exp1? exp2:exp3 判断 exp1是true 和 false 如果true,则返回exp2 ,如果false ,则返回exp3 <script> if ...