Destroying The Graph
Time Limit: 2000MS   Memory Limit: 65536K
         

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 +
题目大意:
n个点m条边的有向图
需要移走这张图里所有的边
每次可以选择移走点i的所有入边或所有出边
每步操作都有对应的代价
求最小代价移走所有的边
注:边有自环和平行边 最小点权覆盖集
=最小割
拆点
源点向每个点连一条流量为outgoing pay的边
每个点向汇点连一条流量为incoming pay的边
原图中的边i,j,由i向拆出的j连inf边
跑最小割
方案的输出:
从源点遍历残量网络,边还有流量就遍历,记录所有遍历到的点
原本就有的点,如果没有被遍历到,就说明它被割了
拆出的点,如果被遍历到,说明它被割了
#include<cstdio>
#include<queue>
using namespace std;
int n,m,tot=,ans;
int front[],to[],nextt[],cap[];
int lev[],cur[];
int src,decc;
bool g[];
queue<int>q;
void add(int u,int v,int w)
{
to[++tot]=v;nextt[tot]=front[u];front[u]=tot;cap[tot]=w;
to[++tot]=u;nextt[tot]=front[v];front[v]=tot;cap[tot]=;
}
bool bfs()
{
for(int i=;i<=decc;i++) {lev[i]=-;cur[i]=front[i];}
while(!q.empty()) q.pop();
q.push(src);lev[src]=;
while(!q.empty())
{
int now=q.front();q.pop();
for(int i=front[now];i;i=nextt[i])
{
int t=to[i];
if(cap[i]>&&lev[t]==-)
{
lev[t]=lev[now]+;
q.push(t);
if(t==decc) return true;
}
}
}
return false;
}
int dinic(int now,int flow)
{
if(now==decc) return flow;
int rest=,delta;
for(int & i=cur[now];i;i=nextt[i])
{
int t=to[i];
if(lev[t]>lev[now]&&cap[i]>)
{
delta=dinic(t,min(flow-rest,cap[i]));
if(delta)
{
cap[i]-=delta;cap[i^]+=delta;
rest+=delta;if(rest==flow) break;
}
}
}
if(rest!=flow) lev[now]=-;
return rest;
}
void cut(int now)
{
g[now]=true;
for(int i=front[now];i;i=nextt[i])
{
if(cap[i]==||g[to[i]]) continue;
cut(to[i]);
}
}
int main()
{
scanf("%d%d",&n,&m);
decc=n+<<;
int x,y;
for(int i=;i<=n;i++)
{
scanf("%d",&x);
add(i<<|,decc,x);
}
for(int i=;i<=n;i++)
{
scanf("%d",&x);
add(src,i<<,x);
}
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
add(x<<,y<<|,2e9);
}
while(bfs()) ans+=dinic(src,2e9);
printf("%d\n",ans);
int sum=;
cut(src);
for(int i=;i<=n;i++)
{
if(g[i<<|]) sum++;
if(!g[i<<]) sum++;
}
printf("%d\n",sum);
for(int i=;i<=n;i++)
{
if(!g[i<<]) printf("%d -\n",i);
if(g[i<<|]) printf("%d +\n",i); }
}

错误:

1、

应该是

源点向每个点连一条流量为outgoing pay的边

每个点向汇点连一条流量为incoming pay的边

连反了

与源点相连的点,连出去的边是点打出的,所以源点与点之间的边控制的是出边的流量

汇点同理

2、方案输出方法错误

错误方法:

在残量网络中,如果与源点相连的边流量为0,说明这个点被割了

如果汇点连出去的边的流量 为这条边指 向的点的原流量,说明这个点被割了

前半部分是正确的,但后半部分是错的

因为跑最大流过程中,增光路上所有边流量都减,

比如有一条边由1指向2,所有花费都是1

跑完最大流后,源点——1 残量为0

2——汇点 残量为0

最终判断的是割掉2个点,但实际割其中一个就行

/*for(int i=front[src];i;i=nextt[i])
{
if(cap[i]==0)
{
sum++;
a[sum][0]=to[i]/2;a[sum][1]='+';
}
}
for(int i=front[decc];i;i=nextt[i])
{
if(cap[i]==out[to[i]/2])
{
sum++;
a[sum][0]=to[i]/2;a[sum][1]='-';
}
}*/

错误代码

3、题目中说有自环,做的时候把它特判去掉了,错

poj 2125 Destroying The Graph (最小点权覆盖)的更多相关文章

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

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

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

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

  3. POJ 2125 Destroying The Graph [最小割 打印方案]

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

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

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

  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 (二分图最小点权覆盖集+输出最小割方案)

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

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

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

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

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

  9. poj 3308 Paratroopers(二分图最小点权覆盖)

    Paratroopers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8954   Accepted: 2702 Desc ...

随机推荐

  1. 2018软工实践—Beta冲刺(6)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Beta 冲鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调组内工作 最终测试文稿编写 展示GitHub当日代码/文档签入记录 ...

  2. Leetcode题库——31.下一个排列

    @author: ZZQ @software: PyCharm @file: nextPermutation.py @time: 2018/11/12 15:32 要求: 实现获取下一个排列的函数,算 ...

  3. Daily target小队介绍(刘畅,陈杰,杨有存,唐祎琳,王晓哲,邵汝佳)

    一.团队介绍 1.团队构成: 2.队名: Daily target,我们的口号是Target your day! 3.团队项目描述: 我们计划写一个用于老师发布任务,学生接受任务的安卓app.教师安排 ...

  4. C# 通过http post 请求上传图片和参数

    一.C# Winform或控制台 /// <summary> /// 通过http上传图片及传参数 /// </summary> /// <param name=&quo ...

  5. vue2.0 keep-alive 最佳实战(转载)

    1.基本用法 vue2.0提供了一个keep-alive组件用来缓存组件,避免多次加载相应的组件,减少性能消耗 <keep-alive> <component> <!-- ...

  6. 内网内使用https 和 使用http 建立连接的速度对比

    文字版 使用https httpstat https://10.24.101.14/cwbase/web/Login.aspx --insecure Connected to HTTP/ OK Ser ...

  7. 惭愧, eclipse 之 build path

    算下来大学到现在已近用了很久的 eclipse 了, 包括 myeclipse, 但是今天碰到的问题让我很惭愧, 一个老项目的编译都搞了好久. 环境: Myeclipse 6.X Struts 1.X ...

  8. angular 神坑 ,回调函数无法被监视

    原方法,使用一个confirm 点ok然后回调,结果 界面无法刷新,搜索了下 是因为$scope没有监视model,必须使用apply方法 $scope.SelectedRow=row; negAle ...

  9. 传说中的WCF:消息拦截与篡改

    我们知道,在WCF中,客户端对服务操作方法的每一次调用,都可以被看作是一条消息,而且,可能我们还会有一个疑问:如何知道客户端与服务器通讯过程中,期间发送和接收的SOAP是什么样子.当然,也有人是通过借 ...

  10. SD/MMC相关寄存器的介绍

    1.SD卡内部架构 在熟悉SD/MMC相关寄存器之前,我们先来看看SD卡的内部架构是怎么样的,如下图所示: 2.SD/MMC相关寄存器的介绍 从上图中总结出:SD卡内部有7个寄存器. 一.OCR,CI ...