Destroying The Graph
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8503   Accepted: 2753   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 + 主要是找割边。
有构造出来的图知道这个是个二部图加两个源点汇点,二部图之间的连边不可能是割边(INF),所以就dfs(S)然后用vis标记,那么vis[S]一定是1,并且vis[T]一定是0.因为S,T不可能在一个集合里
那么从源点处找一下和它相连的边,看vis[]是不是0,是的话就是割边。
然后从汇点处找一下和它相连的边,看vis[]是不是1,是的话就是割边。
因为是个二部图所以不用dfs直接找一次就可以了
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
const int M=;
const int INF=1e9+;
int head[N],tot,S,T;
int q[N],dis[N],n,m,Q;
bool vis[N];
struct node
{
int next,v,w;
} e[M<<];
void add(int u,int v,int w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
}
bool bfs()
{
memset(dis,-,sizeof(dis));
dis[S]=;
int l=,r=;
q[r++]=S;
while(l<r)
{
int u=q[l++];
for(int i=head[u]; ~i; i=e[i].next)
{
int v=e[i].v;
if(dis[v]==-&&e[i].w>)
{
q[r++]=v;
dis[v]=dis[u]+;
if(v==T) return true;
}
}
}
return false;
}
int dfs(int s,int low)
{
if(s==T||!low) return low;
int ans=low,a;
for(int i=head[s]; ~i; i=e[i].next)
{
if(e[i].w>&&dis[e[i].v]==dis[s]+&&(a=dfs(e[i].v,min(e[i].w,ans))))
{
e[i].w-=a;
e[i^].w+=a;
ans-=a;
if(!ans) return low;
}
}
if(low==ans) dis[s]=-;
return low-ans;
}
void dfs(int u){
vis[u]=;
for(int i=head[u];~i;i=e[i].next) if(!vis[e[i].v]&&e[i].w) dfs(e[i].v);
}
int a[N],b[N];
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
S=,T=*n+;
int x,f,t;
memset(head,-,sizeof(head));
tot=;
for(int i=; i<=n; ++i)
{
scanf("%d",&x);
add(S,i,x);
add(i,S,);
}
for(int i=; i<=n; ++i)
{
scanf("%d",&x);
add(i+n,T,x);
add(T,i+n,);
}
while(m--)
{
scanf("%d%d",&f,&t);
add(t,f+n,INF);
add(f+n,t,);
}
int ans=;
while(bfs()) ans+=dfs(S,INF);
printf("%d\n",ans);
dfs(S);
int ct1=,ct2=;
for(int i=head[S];~i;i=e[i].next) if(!vis[e[i].v]) a[ct1++]=e[i].v;
for(int i=head[T];~i;i=e[i].next) if(vis[e[i].v]) b[ct2++]=e[i].v-n;
printf("%d\n",ct1+ct2);
for(int i=;i<ct1;++i) printf("%d +\n",a[i]);
for(int i=;i<ct2;++i) printf("%d -\n",b[i]); }
}

poj2125最小点权覆盖+找一个割集的更多相关文章

  1. poj2125 最小点权覆盖集

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

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

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

  3. POJ2125 Destroying The Graph (最小点权覆盖集)(网络流最小割)

                                                          Destroying The Graph Time Limit: 2000MS   Memo ...

  4. POJ2125 Destroying The Graph 二分图 + 最小点权覆盖 + 最小割

    思路来源:http://blog.csdn.net/lenleaves/article/details/7873441 求最小点权覆盖,同样求一个最小割,但是要求出割去了那些边, 只要用最终的剩余网络 ...

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

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

  6. [学习笔记]最小割之最小点权覆盖&&最大点权独立集

    最小点权覆盖 给出一个二分图,每个点有一个非负点权 要求选出一些点构成一个覆盖,问点权最小是多少 建模: S到左部点,容量为点权 右部点到T,容量为点权 左部点到右部点的边,容量inf 求最小割即可. ...

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

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

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

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

  9. POJ 3308 Paratroopers (对数转换+最小点权覆盖)

    题意 敌人侵略r*c的地图.为了消灭敌人,可以在某一行或者某一列安置超级大炮.每一个大炮可以瞬间消灭这一行(或者列)的敌人.安装消灭第i行的大炮消费是ri.安装消灭第j行的大炮消费是ci现在有n个敌人 ...

随机推荐

  1. Python3 字符串格式化(%操作符)

    格式符 格式符为真实值预留位置,并控制显示的格式.格式符可以包含有一个类型码,用以控制显示的类型,如下: %s    字符串 (采用str()的显示) %r    字符串 (采用repr()的显示) ...

  2. Leetcode PHP题解--D75 706. Design HashMap

    2019独角兽企业重金招聘Python工程师标准>>> D75 706. Design HashMap 题目链接 706. Design HashMap 题目分析 自行设计一个has ...

  3. React全家桶+AntD 共享单车后台管理系统开发

    第1章 课程导学对课程整体进行介绍,并且说明学习的必要性.第2章 React基础知识React基础知识以及生命周期的介绍,并使用React官方脚手架初始化基础项目,同时介绍了新一代打包工具Yarn.第 ...

  4. TensorFlow框架 入门笔记

    背景 略 基础 介绍 略 TensorFlow安装 link TensorFlow 主要概念 使用图(graph)来表示计算任务(执行流程). 在被称之为会话(session)的上下文(context ...

  5. dlopen failed: empty/missing DT_HASH in "libx.so" (built with --hash-style=gnu?)

    崩溃日志内容: java.lang.UnsatisfiedLinkError: dlopen failed: empty/missing DT_HASH in "libxxxx.so&quo ...

  6. CSS开发技巧(二):表格合并边框后的单元格宽度计算

    前言: 分离边框模型和合并边框模型是表格的两种模型,它通过以下属性确定: border-collapse:separate(默认值) | collapse | inherit 当采用分离边框模型时,表 ...

  7. 最大公约数gcd、最小公倍数lcm

    最大公约数(辗转相除法) 循环: int gcd(int a,int b) { int r; ) { r=b%a; b=a; a=r; } return b; } 递归: int gcd(int a, ...

  8. 2019 ICPC 银川网络赛 F-Moving On (卡Cache)

    Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn. Each city has a ...

  9. Vxlan L3

    拓扑图: CE1 <CE1>display current-configuration !Software Version V800R013C00SPC560B560 !Last conf ...

  10. javascript阻止子元素继承父元素事件

    $('.box').on('click', function (e) { if(e.target == this) { console.log(e.target) } })