Destroying The Graph
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8311   Accepted: 2677   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 +

Source

Northeastern Europe 2003, Northern Subregion

题意from mhy12345:给一张有向图,现在要选择一些点,删掉图中的所有边。具体操作为:选择点 i,可
以选择删除从 i 出发的所有有向边或者进入 i 的所有有向边,分别有个代价 ini 和
outi,求最小的代价删掉所有边。并输出删除方案。


建图 S-- w+ -->in-->out-- w- -->T

显然要拆成入点和出点,对于一条边,出点或者入点的花费花一个就可以了
 
打印方案的话,从S做BFS,对于一条边一个顶点vis一个顶点没vis就是割集里的了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=,M=,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m,u,v,s,t,w[N];
struct edge{
int v,c,f,ne;
}e[M<<];
int cnt,h[N];
inline void ins(int u,int v,int c){
cnt++;
e[cnt].v=v;e[cnt].c=c;e[cnt].f=;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].c=;e[cnt].f=;e[cnt].ne=h[v];h[v]=cnt;
}
int cur[N],d[N],vis[N];
int q[N],head,tail;
bool bfs(){
head=tail=;
memset(vis,,sizeof(vis));
d[s]=;vis[s]=;q[tail++]=s;
while(head!=tail){
int u=q[head++];
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(!vis[v]&&e[i].c>e[i].f){
vis[v]=;d[v]=d[u]+;
q[tail++]=v;
if(v==t) return true;
}
}
}
return false;
}
int dfs(int u,int a){
if(u==t||a==) return a;
int flow=,f;
for(int &i=cur[u];i;i=e[i].ne){
int v=e[i].v;
if(d[v]==d[u]+&&(f=dfs(v,min(e[i].c-e[i].f,a)))>){
flow+=f;
e[i].f+=f;
e[((i-)^)+].f-=f;
a-=f;
if(a==) break;
}
}
if(a) d[u]=-;
return flow;
}
int dinic(){
int flow=;
while(bfs()){
for(int i=s;i<=t;i++) cur[i]=h[i];
flow+=dfs(s,INF);
}
return flow;
} void bfsSol(){
head=tail=;
memset(vis,,sizeof(vis));
q[tail++]=s;vis[s]=;
while(head!=tail){
int u=q[head++];
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(!vis[v]&&e[i].c>e[i].f){
vis[v]=;
q[tail++]=v;
}
}
}
}
int cut[M];
void solve(){
int flow=dinic();
printf("%d\n",flow);
bfsSol();
int num=;
for(int i=;i<=n;i++){
if(!vis[i]) num++;
if(vis[i+n]) num++;
}
printf("%d\n",num);
for(int i=;i<=n;i++){
if(!vis[i]) printf("%d +\n",i);
if(vis[i+n]) printf("%d -\n",i);
}
}
int main(){
//freopen("in.txt","r",stdin);
n=read();m=read();s=;t=n+n+;
for(int i=;i<=n;i++) w[i]=read(),ins(s,i,w[i]);
for(int i=;i<=n;i++) w[i+n]=read(),ins(i+n,t,w[i+n]);
for(int i=;i<=m;i++){
u=read();v=read();
ins(v,u+n,INF);
}
solve();
}
 
 
 

POJ 2125 Destroying The Graph [最小割 打印方案]的更多相关文章

  1. poj 2125 Destroying The Graph 最小割+方案输出

    构图思路: 1.将所有顶点v拆成两个点, v1,v2 2.源点S与v1连边,容量为 W- 3.v2与汇点连边,容量为 W+ 4.对图中原边( a, b ), 连边 (a1,b2),容量为正无穷大 则该 ...

  2. POJ - 2125 Destroying The Graph (最小点权覆盖)

    题意:给一张图,现在要删去所有的边,删去一个点的所有入边和所有出边都有其对应\(W_{i+}\)和\(W_{i-}\).求删去该图的最小花费,并输出解 分析:简而言之就是用最小权值的点集去覆盖所有的边 ...

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

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

  4. poj 2125 Destroying The Graph (最小点权覆盖)

    Destroying The Graph http://poj.org/problem?id=2125 Time Limit: 2000MS   Memory Limit: 65536K       ...

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

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

  6. 图论(网络流,二分图最小点权覆盖):POJ 2125 Destroying The Graph

    Destroying The Graph   Description Alice and Bob play the following game. First, Alice draws some di ...

  7. POJ 2125 Destroying The Graph 二分图 最小点权覆盖

    POJ2125 题意简述:给定一个有向图,要通过某些操作删除所有的边,每一次操作可以选择任意一个节点删除由其出发的所有边或者通向它的所有边,两个方向有不同的权值.问最小权值和的解决方案,要输出操作. ...

  8. ●POJ 2125 Destroying The Graph

    题链: http://poj.org/problem?id=2125 题解: 最小割 + 输出割方案.建图:拆点,每个题拆为 i 和 i'分别表示其的入点和出点建立超源 S和超汇 T.S -> ...

  9. poj 3469 Dual Core CPU——最小割

    题目:http://poj.org/problem?id=3469 最小割裸题. 那个限制就是在 i.j 之间连双向边. 根据本题能引出网络流中二元关系的种种. 别忘了写 if ( x==n+1 ) ...

随机推荐

  1. 关于React Native的那些坑

    好久没写博客了,特地把之前接触React Native时遇到的坑总结一下. 初始化一个React Native项目时,可能会遇到以下这些坑: 1.项目版本号与安卓模拟器中安装的 compileSdkV ...

  2. 读懂_countof,可以懂得什么

    在c++开发中数组是我们经常使用存储结构,而于此同时"数组越界"是每个c++程序员不能不提防陷阱. 还好,我们有预定义宏_countof. 一.在visual c++开发环境下,它 ...

  3. hbase伪分布式安装(单节点安装)

    hbase伪分布式安装(单节点安装) http://hbase.apache.org/book.html#quickstart   1.    前提配置好java,环境java变量     上传jdk ...

  4. Harris角点检测原理分析

    看到一篇从数学意义上讲解Harris角点检测很透彻的文章,转载自:http://blog.csdn.net/newthinker_wei/article/details/45603583 主要参考了: ...

  5. 【问题解决】java中为什么不建议使用DataInputStream 的readLine()方法

    常用方法 int read(byte[] b) 从包含的输入流中读取一定数量的字节,并将它们存储到缓冲区数组 b 中. int read(byte[] b, int off, int len) 从包含 ...

  6. MYBATIS异常:INVALID BOUND STATEMENT

    1.mapper.xml中namespaces错误(***) 2.方法不存在 3.方法返回值错误

  7. apache日志管理【转】

    web服务器日志轮循比较好的方式有三种:第一种方法是利用Linux系统自身的日志文件轮循机制:logrotate:第二种方法是利用apache自带的日志轮循程序rotatelogs:第三种是使用在ap ...

  8. linux_磁盘分区

    分区并没有数据内容只是改变分区表,保存在0磁头,0磁道1扇区除MBR引导后64bytes中,只能有4个组分区,4个以上要一个扩展分区 引导MBR,保存在446字节中 磁盘想要存放数据,首先要分区,可以 ...

  9. TCP/IP详解 卷1 第二十章 TCP的成块数据流

    先补充一个知识: 1.停止等待协议:是tcp保证传输可靠的重要途径,"停止等待"就是指发送完一个分组就停止发送,等待对方确认之后,才能继续发送下一个分组 停止等待协议的优点是简单, ...

  10. Android内核三大核心功能之一AMS内部原理

    上面类是AmS的全称,另外两大核心功能是WindowManagerService.java和View.java AmS提供的主要功能: 统一调度各应用程序 内存管理 进程管理 AmS中定义了几个重要的 ...