bzoj1797 [Ahoi2009]Mincut 最小割 最小割输出方案

17 篇文章 0 订阅

Description


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

Solution


直到现在才会输出方案。。以前只会删边跑m次最大流

跑一次最大流得到残量网络,我们把不满流的边抠出来跑强连通分量,那么缩点后剩余的边都是满流边了。
首先不满流的边肯定不可能

  1. 一条边(u,v)可能出现在最小割集中,那么scc[u]!=scc[v]
  2. 一条边(u,v)一定出现在最小割集中,那么(scc[u]=scc[s])且(scc[v]=scc[t])

2比较显然
对于1而言,因为剩余边都是割,那么我们只需要选一个包含(scc[u],scc[v])的集合割掉就可以了

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define fill(x,t) memset(x,t,sizeof(x))

const int INF=0x3f3f3f3f;
const int N=200005;
const int E=500005;

struct edge {int x,y,w,next;} e[E];

int dis[N],stack[N],dfn[N],low[N],top;
int ls[N],scc[N],edCnt=1;

bool vis[N];

int read() {
	int x=0,v=1; char ch=getchar();
	for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
	for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
	return x*v;
}

void add_edge(int x,int y,int w) {
	e[++edCnt]=(edge) {x,y,w,ls[x]}; ls[x]=edCnt;
	e[++edCnt]=(edge) {y,x,0,ls[y]}; ls[y]=edCnt;
}

bool bfs(int st,int ed) {
	std:: queue <int> que;
	fill(dis,0); dis[st]=1;
	for (que.push(st);!que.empty();) {
		int x=que.front(); que.pop();
		for (int i=ls[x];i;i=e[i].next) {
			if (e[i].w>0&&dis[e[i].y]==0) {
				dis[e[i].y]=dis[x]+1;
				if (e[i].y==ed) return true;
				que.push(e[i].y);
			}
		}
	}
	return false;
}

int find(int x,int ed,int mn) {
	if (x==ed||!mn) return mn;
	int ret=0;
	for(int i=ls[x];i;i=e[i].next) {
		if (e[i].w>0&&dis[x]+1==dis[e[i].y]) {
			int d=find(e[i].y,ed,std:: min(mn-ret,e[i].w));
			e[i].w-=d; e[i^1].w+=d; ret+=d;
			if (mn==ret) break;
		}
	}
	return ret;
}

int dinic(int st,int ed) {
	int res=0;
	for (;bfs(st,ed);) res+=find(st,ed,INF);
	return res;
}

void dfs(int x) {
	stack[++top]=x; vis[x]=1;
	dfn[x]=low[x]=++dfn[0];
	for (int i=ls[x];i;i=e[i].next) {
		if (!e[i].w) continue;
		if (!dfn[e[i].y]) {
			dfs(e[i].y);
			low[x]=std:: min(low[x],low[e[i].y]);
		} else if (vis[e[i].y]) {
			low[x]=std:: min(low[x],dfn[e[i].y]);
		}
	}
	if (low[x]==dfn[x]) {
		int y=0; scc[0]++;
		while (y!=x) {
			y=stack[top--];
			scc[y]=scc[0];
			vis[y]=0;
		}
	}
}

int main(void) {
	int n=read(),m=read(),s=read(),t=read();
	rep(i,1,m) {
		int x=read(),y=read(),w=read();
		add_edge(x,y,w);
	}
	dinic(s,t);
	rep(i,1,n) if (!dfn[i]) dfs(i);
	for (int i=2;i<=edCnt;i+=2) {
		if (e[i].w) {puts("0 0"); continue;}
		printf("%d ", (scc[e[i].x]!=scc[e[i].y]));
		printf("%d\n", (scc[e[i].x]==scc[s])&&(scc[e[i].y]==scc[t]));
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值