BZOJ 1797: [Ahoi2009]Mincut 最小割 图的联通 最小割 tarjan

1797: [Ahoi2009]Mincut 最小割

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2441  Solved: 1053
[Submit][Status][Discuss]

Description

A,B两个国家正在交战,其中A国的物资运输网中有N个中转站,M条单向道路。设其中第i (1≤i≤M)条道路连接了vi,ui两个中转站,那么中转站vi可以通过该道路到达ui中转站,如果切断这条道路,需要代价ci。现在B国想找出一个路径切断方案,使中转站s不能到达中转站t,并且切断路径的代价之和最小。 小可可一眼就看出,这是一个求最小割的问题。但爱思考的小可可并不局限于此。现在他对每条单向道路提出两个问题: 问题一:是否存在一个最小代价路径切断方案,其中该道路被切断? 问题二:是否对任何一个最小代价路径切断方案,都有该道路被切断? 现在请你回答这两个问题。

Input

第一行有4个正整数,依次为N,M,s和t。第2行到第(M+1)行每行3个正 整数v,u,c表示v中转站到u中转站之间有单向道路相连,单向道路的起点是v, 终点是u,切断它的代价是c(1≤c≤100000)。 注意:两个中转站之间可能有多条道路直接相连。 同一行相邻两数之间可能有一个或多个空格。

Output

对每条单向边,按输入顺序,依次输出一行,包含两个非0即1的整数,分 别表示对问题一和问题二的回答(其中输出1表示是,输出0表示否)。 同一行相邻两数之间用一个空格隔开,每行开头和末尾没有多余空格。

Sample Input

6 7 1 6
1 2 3
1 3 2
2 4 4
2 5 1
3 5 5
4 6 2
5 6 3

Sample Output

1 0
1 0
0 0
1 0
0 0
1 0
1 0

这是一道结论题。

在最小割求完之后 残联网络跑tarjan

没有跑满的就不可能出现在最小割集中

对于一条边(u,v)

能出现在最小割集中当且仅当(u,v)为满流且在残量网络里u,v不属于同一个强连通分量

必定出现在最小割集中当且仅当(u,v)为满流且在残量网络中u与s在同一强连通分量且v与t在同一强连通分量。


#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=40010,inf=0X3f3f3f3f;

int n,S,T;

int ecnt=1,last[N];
struct EDGE{int fr,to,nt,val;}e[N<<3];
inline void readd(int u,int v,int val)
{e[++ecnt]=(EDGE){u,v,last[u],val};last[u]=ecnt;}
inline void add(int u,int v,int val)
{readd(u,v,val);readd(v,u,0);}

int d[N],q[N];

bool bfs()
{
	memset(d+1,0,sizeof(int)*n);
	register int head=0,tail=1,u,i;
	d[S]=1;q[head]=S;
	while(head<tail)
	{
		u=q[head++];
		for(i=last[u];i;i=e[i].nt)
		if(e[i].val&&!d[e[i].to])
		{
			d[e[i].to]=d[u]+1;
			q[tail++]=e[i].to;
		}
	}
	return d[T];
}

int dfs(int u,int lim)
{
	if(u==T||!lim)return lim;
	register int res=0,i,tmp;
	for(i=last[u];i;i=e[i].nt)
	if(d[e[i].to]==d[u]+1&&e[i].val)
	{
		tmp=dfs(e[i].to,min(lim,e[i].val));
		e[i].val-=tmp;e[i^1].val+=tmp;res+=tmp;lim-=tmp;
		if(!tmp)d[e[i].to]=-1;
		if(!lim)break;
	}
	return res;
}

void dinic()
{while(bfs())dfs(S,inf);}

int dfn[N],bel[N],low[N],cnt,s[N],top,scc;
bool ins[N];

void tarjan(int u)
{
	dfn[u]=low[u]=++cnt;s[++top]=u;ins[u]=1;
	register int i;
	for(i=last[u];i;i=e[i].nt)
	if(e[i].val)
	{
		if(!dfn[e[i].to])
		{tarjan(e[i].to);low[u]=min(low[u],low[e[i].to]);}
		else if(ins[e[i].to]&&low[u]>dfn[e[i].to])low[u]=dfn[e[i].to];
	}
	if(dfn[u]==low[u])
	{
		int tmp;scc++;
		do
		{
			tmp=s[top--];ins[tmp]=0;bel[tmp]=scc;ins[tmp]=0;
		}while(u^tmp);
	}
}

int main()
{
	n=read();int m=read();S=read();T=read();
	register int i,u,v,val;
	for(i=1;i<=m;++i){u=read();v=read();val=read();add(u,v,val);}
	dinic();
	for(i=1;i<=n;++i)if(!dfn[i])tarjan(i);
	for(i=2;i<=m<<1;i+=2)
	{
		if(e[i].val)puts("0 0");
		else 
		{
			if(bel[e[i].fr]==bel[e[i].to]){puts("0 0");continue;}
			putchar('1');putchar(' ');
			if((bel[e[i].fr]==bel[S]&&bel[e[i].to]==bel[T])||(bel[e[i].fr]==bel[T]&&bel[e[i].to]==bel[S]))puts("1");else puts("0");
		}
	}
	return 0;
}
/*
6 7 1 6
1 2 3
1 3 2
2 4 4
2 5 1
3 5 5
4 6 2
5 6 3

1 0
1 0
0 0
1 0
0 0
1 0
1 0
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值