[AHOI2009]最小割 最小割可行边&必须边
题解:
做这题的时候才知道有最小割可行边和必须边这种东西。。。。。
1,最小割可行边,
意思就是最小割中可能出现的边。
充要条件:
1,满流
2,在残余网络中找不到x ---> y的路径
解释:
如果在残余网络中还找得到x ---> y的路径的话,要割掉这条边就还需要割掉另一条路径,这显然是不够优的。
如果是满流的话显然不是割掉了这条边
2,最小割必须边
1,满流
2,在残余网络中s 可以到 x, y 可以到 t。
解释:
满流的原因和上面原因,同时必须边肯定也是可行边(显然可行边的范围就要大一些嘛)。
如果满流但s不能到x or y 不能到 t,因为这样的话说明在s 到 x(y 到 t)的路上就已经被割掉了,而不是在这里割的。
但是因为满流了,所以这是可行的,但是由于割在别的地方,说明不是必须的。
因此s 必须可以到 x, y 必须可以到s才能保证是必须边,而不是可行边
至于实现方法就比较妙了。
如果两个点在一个scc中则表示可以到。
因此可行边需要保证x和y不在一个scc中。
而必须边则还需要额外保证s 和 x 属于一个scc, y 和 t属于一个scc
#include<bits/stdc++.h>
using namespace std;
#define R register int
#define getchar() *o++
#define inf 2139062143
#define AC 5000
#define ac 150000
#define D printf("line in %d\n", __LINE__);
char READ[], *o = READ;
int n, m, s, t, ans, x, cnt, addflow;
int last[AC], c[AC], have[AC], good[AC];
int date[ac], Next[ac], belong[ac], haveflow[ac], Head[AC], tot = ;//前向星
int low[AC], dfn[AC], scc[AC], timer;//tarjan
int q[AC], head, tail;//队列
int Stack[AC], top;
bool z[AC];//用于tarjan inline int read()
{
int x = ; char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} inline void upmin(int &a, int b)
{
if(a > b) a = b;
} inline void add(int f, int w, int S)
{
date[++tot] = w, Next[tot] = Head[f], Head[f] = tot, haveflow[tot] = S, belong[tot] = f;
date[++tot] = f, Next[tot] = Head[w], Head[w] = tot;
} void pre()
{
int u, v, e;
n = read(), m = read(), s = read(), t = read();
for(R i = ; i <= m; i++)
{
u = read(), v = read(), e = read();
add(u, v, e);
}
} void bfs()
{
int x, now;
c[t] = , have[] = , q[++tail] = t;
while(head < tail)
{
x = q[++head];
for(R i = Head[x]; i ; i = Next[i])
{
now = date[i];
if(haveflow[i ^ ] && !c[now])
{
c[now] = c[x] + ;
++have[c[now]];
q[++tail] = now;
}
}
}
memcpy(good, Head, sizeof(good));
} void aru()
{
while(x != s)
{
haveflow[last[x]] -= addflow;
haveflow[last[x] ^ ] += addflow;
x = date[last[x] ^ ];
}
ans += addflow;
} void isap()
{
int now; bool done;
x = s, addflow = inf;
while(c[s] != )
{
if(x == t) aru(), addflow = inf;
done = false;
for(R i = good[x]; i ; i = Next[i])
{
now = date[i];
if(haveflow[i] && c[now] == c[x] - )
{
done = true;
upmin(addflow, haveflow[i]);
good[x] = last[now] = i;
x = now;
break;
}
}
if(!done)
{
int go = ;
for(R i = Head[x]; i ; i = Next[i])
{
now = date[i];
if(haveflow[i] && c[now]) upmin(go, c[now]);
}
good[x] = Head[x];//error!!!不要忘了重置
if(!(--have[c[x]])) break;
++have[c[x] = go + ];
if(x != s) x = date[last[x] ^ ];
}
}
} void tarjan(int x)
{
int now;
dfn[x] = low[x] = ++timer;
z[x] = true;
Stack[++top] = x;
for(R i = Head[x]; i ; i = Next[i])
{
if(!haveflow[i]) continue;//流满表示不联通
now = date[i];
if(!dfn[now])
{
tarjan(now);
upmin(low[x], low[now]);
}
else if(z[now]) upmin(low[x], low[now]);
}
if(dfn[x] == low[x])
{
++cnt;
while(Stack[top] != x)
{
now = Stack[top--];
scc[now] = cnt;
z[now] = false;
}
now = Stack[top--];
scc[now] = cnt;
z[now] = false;
}
} void work()
{
int x, y;
for(R i = ; i <= tot; i += )
{
x = belong[i], y = date[i];
// printf("%d %d\n", x, y);
if(!haveflow[i] && scc[x] != scc[y])
{
printf("1 ");
if(scc[x] == scc[s] && scc[y] == scc[t]) printf("1\n");
else printf("0\n");
}
else printf("0 0\n");
}
} int main()
{
// freopen("in.in", "r", stdin);
fread(READ, , , stdin);
pre();
bfs();
isap();
for(R i=;i<=n;i++)
if(!dfn[i]) tarjan(i);
// for(R i=1;i<=n;i++) printf("%d ", scc[i]);
// printf("\n");
// for(R i=2;i<=tot;i++) printf("%d ", haveflow[i]);
work();
// fclose(stdin);
return ;
}
[AHOI2009]最小割 最小割可行边&必须边的更多相关文章
- BZOJ 1797 网络流的可行边&必须边
求完网络流以后 tarjan一发 判一判 //By SiriusRen #include <queue> #include <bitset> #include <cstd ...
- 最小割求法&&可行边和必须边
最小割的可行边与必须边 就是在残量网络上跑tarjan 可行边: 满流并且残量网络上不能存在入点到出点的路径 必须边: 满流并且残量网络上入点能从源点到达,出点能到汇点. 任意一种最小割求法: 跑一边 ...
- scu - 3254 - Rain and Fgj(最小点权割)
题意:N个点.M条边(2 <= N <= 1000 , 0 <= M <= 10^5),每一个点有个权值W(0 <= W <= 10^5),现要去除一些点(不能去掉 ...
- 算法笔记--最大流和最小割 && 最小费用最大流 && 上下界网络流
最大流: 给定指定的一个有向图,其中有两个特殊的点源S(Sources)和汇T(Sinks),每条边有指定的容量(Capacity),求满足条件的从S到T的最大流(MaxFlow). 最小割: 割是网 ...
- 3532: [Sdoi2014]Lis 最小字典序最小割
3532: [Sdoi2014]Lis Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 865 Solved: 311[Submit][Status] ...
- 紫书 例题 11-2 UVa 1395(最大边减最小边最小的生成树)
思路:枚举所有可能的情况. 枚举最小边, 然后不断加边, 直到联通后, 这个时候有一个生成树.这个时候,在目前这个最小边的情况可以不往后枚举了, 可以直接更新答案后break. 因为题目求最大边减最小 ...
- bzoj1797: [Ahoi2009]Mincut 最小割(最小割+强联通tarjan)
1797: [Ahoi2009]Mincut 最小割 题目:传送门 题解: 感觉是一道肥肠好的题目. 第二问其实比第一问简单? 用残余网络跑强联通,流量大于0才访问. 那么如果两个点所属的联通分量分别 ...
- BZOJ1797:[AHOI2009]最小割(最小割)
Description A,B两个国家正在交战,其中A国的物资运输网中有N个中转站,M条单向道路.设其中第i (1≤i≤M)条道路连接了vi,ui两个中转站,那么中转站vi可以通过该道路到达ui中转站 ...
- POJ 3308 Paratroopers(最大流最小割の最小点权覆盖)
Description It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the ...
随机推荐
- linux-centos6②
- Python 发邮件例子
Python 发邮件例子 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-04-23 16:12:33 # @Autho ...
- Python入门编程中的变量、字符串以及数据类型
//2018.10.10 字符串与变量 1. 在输出语句中如果需要出现单引号或者双引号,可以使用转义符号\,它可以将其中的歧义错误解释化解,使得输出正常: 2. 对于python的任何变量都需要进行赋 ...
- Java开发工程师(Web方向) - 04.Spring框架 - 第1章.Spring概述
第1章.Spring概述 Spring概述 The Spring Framework is a lightweight solution and a potential one-stop-shop f ...
- 【springmvc+mybatis项目实战】杰信商贸-2.数据库配置
首先我们来了解项目的架构 我们分别使用了MySql和Oracle数据库,即是异构数据库.我们做到一个平台支持多个数据库.数据库建模我们使用Sybase公司的PowerDesigner(以后简称PD), ...
- 【转】VSstudio中的一些宏
说明 $(RemoteMachine) 设置为“调试”属性页上“远程计算机”属性的值.有关更多信息,请参见更改用于 C/C++ 调试配置的项目设置. $(References) 以分号分隔的引用列表被 ...
- 从Softmax回归到Logistic回归
Softmax回归是Logistic回归在多分类问题上的推广,是有监督的. 回归的假设函数(hypothesis function)为,我们将训练模型参数,使其能够最小化代价函数: 在Softmax回 ...
- [leetcode-744-Find Smallest Letter Greater Than Target]
Given a list of sorted characters letters containing only lowercase letters, and given a target lett ...
- Redis+Keepalived高可用方案详细分析
背景 目前,Redis集群的官方方案还处在开发测试中,未集成到稳定版中.且目前官方开发中的Redis Cluster提供的功能尚不完善(可参考官方网站或http://www.redisdoc.com/ ...
- Javascript闭包演示【转】
文章出自http://www.cnblogs.com/snandy/archive/2011/03/01/1967628.html 有个网友问了个问题,如下的html,为什么点击所有的段落p输出都是5 ...