题目请戳这里

题目大意:给一张有向图,现在要选择一些点,删掉图中的所有边。具体操作为:选择点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. poj 1724 ROADS 最短路

    题目链接 n个节点, m条边, 一开始有K这么多的钱, 每条边有len, cost两个属性, 求1到n的最短距离, 花费要小于k. dis数组开成二维的, dis[u][cost]表示到达u花费为co ...

  2. 用python 10min手写一个简易的实时内存监控系统

    简易的内存监控系统 本文需要有一定的python和前端基础,如果没基础的,请关注我后续的基础教程系列博客 文章github源地址,还可以看到具体的代码,喜欢请在原链接右上角加个star 腾讯视频链接 ...

  3. tomcat链接mysql时超时报错java.io.EOFException: Can not read response from server. Expected to read 4 bytes,

    需要在配置文件里加上下面就ok了 <property name=”minEvictableIdleTimeMillis” value=”1800000″ /> <property n ...

  4. Hyperworks、Nastran、Abaqus与ansys的区别

    hypermesh不过是前处理,radioos就是hm的求解器,也是非常强大的可以处理很多非线性问题,最重要的是hm的优化功能强大.比那几个好一些.abaqus适合非线性分析,尤其是接触分析.nast ...

  5. 老男孩python第六期

    01 python s6 day7 上节回顾02 python s6 day7 SNMP使用03 python s6 day7 大型监控架构讲解04 python s6 day7 Redis使用05 ...

  6. zoj 1366 Cash Machine

    01背包加变形 动态规划的时候就犯浑了,每个状态都要记录的,我却只记录了当前状态的!! #include<stdio.h> #include<string.h> int max ...

  7. C# Best Practices - Define Proper Classes

    Application Architecture Define the components appropriately for the application and create project ...

  8. UVa1586 Molar mass

    #include <stdio.h> int GetQuantity(char* q, char** p){    int quantity = 0;    while (*q & ...

  9. ie6,ie7下设置overflow:auto下滚动条不起作用

    今天遇到一个比较特殊的情况:ie6,ie7下设置overflow:auto下滚动条出来了但是滚动条不起任何作用,但在火狐,ie8,ie9,谷歌等浏览器下正常显示,解决方案:只需要加一个position ...

  10. SQL中 and or优先级问题

    资源来源:http://www.linuxidc.com/Linux/2012-03/56267.htm 刚刚在项目中遇到这样一个问题,SQL语句如下: select * from LOAN_BACK ...