题目请戳这里

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

题目分析:最小割。因为每次删除的是一个点的所有入边或者所有出边。那么就很明显了,拆点,将i拆成i和I+n2个点,分别表示第i个点的入度点和出度点。源点到每个i连边,表示从i点的出边,边权为outi,i+n表示第i个点的入度点,那么i+n-->汇点建边,边权为ini,对于每对给定的有向边i->j,建边i->j + n,边权为无穷大。原理其实和这题是十分类似的。因为要求一个最小割,要保证源点和汇点被分在2个集合中,i->j + n边权无穷大后,保证割边集不可能包含i->j + n这类边,那么割边集只能包含s->i和j + n->t,这样就保证求出的最小割将源点和汇点分开。建好图跑一遍最大流即可。

不过这题还要给一个最小割的方案,输出割点。搞了好久。。。

其实也不是很复杂,对求完最大流的残余网络进行一次dfs即可。从源点开始dfs,只对边权残余容量为0的边遍历。所有遍历到的点标记上。然后检查1-2n的所有点,分2类:

1:i<=n的点,根据前面建图可知,这类点是表示第i个点的出边的,如果从源点无法通过残余容量为0的边遍历到,那么说明这个点的出边是属于割集的,即所求点。反之,对于能遍历到的点,肯定不是割点。

2:i>n的点,这类点是表示第i个点的入边,如果被遍历到了,肯定是属于割点的。为什么呢,因为从源点开始遍历,肯定要先通过1-n的点到达n+1~n + n的点,假设到达了i+n这个点,并且假设是从j到达i+n的点的,前面已经说了,j肯定不属于割点,那么j的出边肯定就没有删除,要求要删掉所有的边,既然从j不能删掉从j出发的边,那么只能删掉j所到达的边的入边了。既然能从j->i+n,那么j->i肯定右边,相对i来说,这是条入边,i一定要属于割点才能保证删掉所有的边。

详情请见代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 105;
const int M = 10005;
const int inf = 0x3f3f3f3f; int m,n,num;
struct node
{
int to,c,f,next,pre;
}arc[M];
int head[N],que[N],sta[N],cnt[N],dis[N],rpath[N];
bool flag[N][N];
void build(int s,int e,int cap)
{
arc[num].to = e;
arc[num].c = cap;
arc[num].f = 0;
arc[num].next = head[s];
head[s] = num ++;
arc[num - 1].pre = num;
arc[num].pre = num - 1;
arc[num].to = s;
arc[num].c = arc[num].f = 0;
arc[num].next = head[e];
head[e] = num ++;
}
void init()
{
memset(head,-1,sizeof(head));
memset(flag,false,sizeof(flag));
num = 0;
int i,a,b,d;
scanf("%d",&m);
for(i = 1;i <= n;i ++)
{
scanf("%d",&d);
build(i + n,n + n + 1,d);
}
for(i = 1;i <= n;i ++)
{
scanf("%d",&d);
build(0,i,d);
}
while(m --)
{
scanf("%d%d",&a,&b);
if(flag[a][b + n])
continue;
flag[a][b + n] = true;
build(a,b + n,inf);
}
}
void re_Bfs()
{
int i,front,rear;
for(i = 0;i <= n + n + 1;i ++)
{
dis[i] = n + n + 2;
cnt[i] = 0;
}
front = rear = 0;
dis[n + n + 1] = 0;
cnt[0] = 1;
que[rear ++] = n + n + 1;
while(front != rear)
{
int u = que[front ++];
for(i = head[u];i != -1;i = arc[i].next)
{
if(arc[arc[i].pre].c == 0 || dis[arc[i].to] < n + n + 2)
continue;
dis[arc[i].to] = dis[u] + 1;
cnt[dis[arc[i].to]] ++;
que[rear ++] = arc[i].to;
}
}
}
void dfs(int u)
{
if(cnt[u])
return;
cnt[u] = 1;
for(int i = head[u];i != -1;i = arc[i].next)
if(arc[i].c > 0 && cnt[arc[i].to] == 0)
dfs(arc[i].to);
}
void show(int maxflow)
{
memset(cnt,0,sizeof(cnt));
dfs(0);
int i,rear = 0;
for(i = 1;i <= n;i ++)
{
if(cnt[i] == 0)
que[rear ++] = i;
if(cnt[i + n])
que[rear ++] = i + n;
}
printf("%d\n",rear);
for(i = 0;i < rear;i ++)
{
if(que[i] <= n)
printf("%d -\n",que[i]);
else
printf("%d +\n",que[i] - n);
}
}
void ISAP()
{
re_Bfs();
int i,u,maxflow = 0;
for(i = 0;i <= n + n + 1;i ++)
sta[i] = head[i];
u = 0;
while(dis[0] < n + n + 2)
{
if(u == n + n + 1)
{
int curflow = inf;
for(i = 0;i != n + n + 1;i = arc[sta[i]].to)
curflow = min(curflow,arc[sta[i]].c);
for(i = 0;i != n + n + 1;i = arc[sta[i]].to)
{
arc[sta[i]].c -= curflow;
arc[sta[i]].f += curflow;
arc[arc[sta[i]].pre].c += curflow;
arc[arc[sta[i]].pre].f -= curflow;
}
maxflow += curflow;
u = 0;
}
for(i = sta[u];i != -1;i = arc[i].next)
if(arc[i].c > 0 && dis[u] == dis[arc[i].to] + 1)
break;
if(i != -1)
{
sta[u] = i;
rpath[arc[i].to] = arc[i].pre;
u = arc[i].to;
}
else
{
if((-- cnt[dis[u]]) == 0)
break;
int Min = n + n + 2;
sta[u] = head[u];
for(i = sta[u];i != -1;i = arc[i].next)
if(arc[i].c > 0)
Min = min(Min,dis[arc[i].to]);
dis[u] = Min + 1;
cnt[dis[u]] ++;
if(u != 0)
u = arc[rpath[u]].to;
}
}
printf("%d\n",maxflow);
show(maxflow);
}
int main()
{
while(scanf("%d",&n) != EOF)
{
init();
ISAP();
}
return 0;
}
//1380K 63MS
/*
3 6
1 2 3
4 2 1
1 2
1 1
3 2
1 2
3 1
2 3 3 5
1 2 3
4 2 1
1 2
3 2
1 2
3 1
2 3
*/

poj2125Destroying 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. CodeForces1082G Petya and Graph 最小割

    网络流裸题 \(s\)向点连边\((s, i, a[i])\) 给每个边建一个点 边\((u, v, w)\)抽象成\((u, E, inf)\)和\((v, E, inf)\)以及边\((E, t, ...

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

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

  4. Petya and Graph(最小割,最大权闭合子图)

    Petya and Graph http://codeforces.com/contest/1082/problem/G time limit per test 2 seconds memory li ...

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

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

  6. Petya and Graph/最大权闭合子图、最小割

    原题地址:https://codeforces.com/contest/1082/problem/G G. Petya and Graph time limit per test 2 seconds ...

  7. Destroying The Graph 最小点权集--最小割--最大流

    Destroying The Graph 构图思路: 1.将所有顶点v拆成两个点, v1,v2 2.源点S与v1连边,容量为 W- 3.v2与汇点连边,容量为 W+ 4.对图中原边( a, b ), ...

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

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

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

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

随机推荐

  1. Django Web开发【1】Django简介

    前言 看完<Django Book>之后, 总想找个实例来实战开发下,无奈国内Django的书籍相当少,只能从英文书籍中吸取养料,偶然之后得到Learning Website Develo ...

  2. js基础参数获取

    1 获取浏览器中url中的参数,会自动把问号"?"去掉 function getParamsFromHref() { //调试用 var wyl_ = window.locatio ...

  3. 从零开始PHP学习 - 第二天

    写这个系列文章主要是为了督促自己  每天定时 定量消化一些知识! 同时也为了让需要的人 学到点啥~! 本人技术实在不高!本文中可能会有错误!希望大家发现后能提醒一下我和大家! 偷偷说下 本教程最后的目 ...

  4. hdu 3397 Sequence operation 线段树

    题目链接 给出n个数, 每个数是0或1, 给5种操作, 区间变为1, 区间变为0, 区间0,1翻转, 询问区间内1的个数, 询问区间内最长连续1的个数. 需要将数组开成二维的, 然后区间0, 1翻转只 ...

  5. Windows Phone 8.1 发送http 网络请求。

    在windows phone 8.1 中可以用 HttpClient 类来发送http 请求. 例子: try { Uri uri = new Uri(@"http://api.map.ba ...

  6. python总结

    环境:django,numpy,matplotlib, 解释语言:开发效率高,通用性强,内置方便的数据容器,易于扩展和嵌入. 语言:lua--嵌入式/网络/APP,erlang--嵌入式,python ...

  7. android之PackageManager简单介绍

    PackageManager相关 本类API是对全部基于载入信息的数据结构的封装,包含下面功能: 安装,卸载应用查询permission相关信息 查询Application相关信息(applicati ...

  8. Ubuntu 用 pptp 建立 vpn 服务

    1.下载pptp sudo apt-get install pptpd 2.配置pptp 须要改动配置下面的文件: pptpd.conf文件:配置链接后的主机ip和能够分配的内存范围 vi /etc/ ...

  9. cocos2d-x 3.1 集成 云风pbc

    cocos2d-x 3.x版本号变动比較大,从改用cmake管理整个项目,到使用python集成一体化的项目工具. 这些都是我喜欢的.我能够非常easy的在我的ubuntu上面搭建好开发环境,并且根本 ...

  10. 获取ubuntu 的root密码,告别sudo

    刚刚开始使用ubuntu的朋友可能知道一个提权命令    sudo 如果你接触过其他的Linux系统的话,你会知道  linux系统有一个最高权限   root.一般用su root,然后输入密码就可 ...