[最大流][tarjan] Bzoj 1797 最小割

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

HINT

设第(i+1)行输入的边为i号边,那么{1,2},{6,7},{2,4,6}是仅有的三个最小代价切割方案。它们的并是{1,2,4,6,7},交是 。 【数据规模和约定】 测试数据规模如下表所示 数据编号 N M 数据编号 N M 1 10 50 6 1000 20000 2 20 200 7 1000 40000 3 200 2000 8 2000 50000 4 200 2000 9 3000 60000 5 1000 20000 10 4000 60000

题解

  • 我们首先对原图跑一遍最大流
  • 很容易得知若一条边u->v存在u->v的增广路,则这条边一定不是割边,反之亦然则可能是割边
  • 那么我们就把点分成了s集和t集还有一部分不确定
  • 若一条边连接了s集和t集,则这条边一定是必然存在最小割中的边
  • 那么我们就得到了这样一个算法: 把残余网络中每个SCC缩成一个点,这样图中剩下的边则必然是满流边,且图中的每一个割都对应了原图中的一个最小割
  • 设点i所在的强连通分量为id[i].那么一条边可能存在于割集中当且仅当id[u]!=id[v]
  • 一条边一定存在于割集中当且仅当id[u]==id[s]且id[v]==id[t]

代码

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #define inf 1000000000
 6 #define ll long long 
 7 using namespace std;
 8 int n,m,S,T,cnt=1,ind,top,scc,head[4005],Q[4005],h[4005],cur[4005],id[4005],dfn[4005],low[4005];
 9 bool inq[4005];
10 struct edge{ int from,to,pre,v; }e[120005];
11 void insert(int u,int v,int w)
12 {
13     e[++cnt].pre=u;e[cnt].to=v;e[cnt].from=head[u];head[u]=cnt;e[cnt].v=w;
14     e[++cnt].pre=v;e[cnt].to=u;e[cnt].from=head[v];head[v]=cnt;e[cnt].v=0;
15 }
16 bool bfs()
17 {
18     int p=0,q=1;
19     for (int i=1;i<=n;i++) h[i]=-1;
20     Q[0]=S,h[S]=0;
21     while (p!=q)
22     {
23         int x=Q[p++];
24         for (int i=head[x];i;i=e[i].from) if (h[e[i].to]==-1&&e[i].v) h[e[i].to]=h[x]+1,Q[q++]=e[i].to;
25     }
26     return h[T]!=-1;
27 }
28 int dfs(int x,int f)
29 {
30     if(x==T)return f;
31     int w,used=0;
32     for (int i=cur[x];i;i=e[i].from)
33         if (h[e[i].to]==h[x]+1)
34         {
35             w=dfs(e[i].to,min(e[i].v,f-used)),e[i].v-=w;e[i^1].v+=w;
36             if (e[i].v) cur[x]=i;
37             used+=w;
38             if(used==f)return f;
39         }
40     if (!used)h[x]=-1;
41     return used;
42 }
43 int dinic()
44 {
45     int ans=0;
46     while (bfs())
47     {
48         for( int i=1;i<=n;i++) cur[i]=head[i];
49         ans+=dfs(S,inf);
50     }
51     return ans;
52 }
53 void tarjan(int x)
54 {
55     dfn[x]=low[x]=++ind;
56     Q[++top]=x,inq[x]=1;
57     for (int i=head[x];i;i=e[i].from)
58         if (e[i].v)
59         {
60             if (!dfn[e[i].to]) tarjan(e[i].to),low[x]=min(low[e[i].to],low[x]);
61             else if (inq[e[i].to]) low[x]=min(dfn[e[i].to],low[x]);
62         }
63     int now=0;
64     if (dfn[x]==low[x])
65     {
66         scc++;
67         while(now!=x) now=Q[top--],id[now]=scc,inq[now]=0;
68     }
69 }
70 int main()
71 {
72     scanf("%d%d%d%d",&n,&m,&S,&T);
73     for (int i=1,u,v,w;i<=m;i++) scanf("%d%d%d",&u,&v,&w),insert(u,v,w);
74     dinic();
75     for (int i=1;i<=n;i++) if(!dfn[i])tarjan(i);
76     for (int i=2;i<=cnt;i+=2)
77         if (e[i].v)puts("0 0");
78         else 
79         {
80             if (id[e[i].pre]!=id[e[i].to]) printf("1 "); else printf("0 ");
81             if (id[e[i].pre]==id[S]&&id[e[i].to]==id[T]) printf("1
"); else printf("0
");
82         }
83 }
原文地址:https://www.cnblogs.com/Comfortable/p/11149921.html