1797: [Ahoi2009]Mincut 最小割
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1973 Solved: 852
[ 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
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
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
2015.4.16新加数据一组,可能会卡掉从前可以过的程序。
Source
题解:跑一遍最大流。然后在残余网络的流量非0的边中跑tarjan缩点。一边的两个端点分属不同联通块的边是可能出现的边,如果两个端点分别属于起点和终点所在的联通块,则是一定存在最小割中的边。注意判断的时候要除去根本没跑的边。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<queue>
#define N 120003
#define inf 1000000000
using namespace std;
int n,m;
int point[N],next[N],v[N],remain[N],last[N],deep[N],num[N],u[N];
int cur[N],ins[N],dfsn[N],belong[N],st[N],top,s1,t1;
int mark[N],mark1[N],tot,sz,cnt,low[N];
int xx[N],yy[N];
void add(int x,int y,int z,int k)
{
tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z; u[tot]=k;
tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0; u[tot]=k;
}
int addflow(int s,int t)
{
int now=t,ans=inf;
while (now!=s)
{
ans=min(ans,remain[last[now]]);
now=v[last[now]^1];
}
now=t;
while (now!=s)
{
remain[last[now]]-=ans;
remain[last[now]^1]+=ans;
now=v[last[now]^1];
}
return ans;
}
void bfs(int s,int t)
{
for (int i=1;i<=n;i++) deep[i]=n;
queue<int> p; p.push(t); deep[t]=0;
while (!p.empty())
{
int now=p.front(); p.pop();
for (int i=point[now];i!=-1;i=next[i])
if (deep[v[i]]==n&&remain[i^1])
deep[v[i]]=deep[now]+1,p.push(v[i]);
}
}
int isap(int s,int t)
{
bfs(s,t);
for (int i=1;i<=n;i++) cur[i]=point[i];
for (int i=1;i<=n;i++) num[deep[i]]++;
int now=s; int ans=0;
while (deep[s]<n)
{
if (now==t)
{
ans+=addflow(s,t);
now=s;
}
bool f=false;
for (int i=cur[now];i!=-1;i=next[i])
if (deep[v[i]]+1==deep[now]&&remain[i])
{
cur[now]=i;
last[v[i]]=i;
f=true;
now=v[i];
break;
}
if (!f)
{
int minn=n;
for (int i=point[now];i!=-1;i=next[i])
if (remain[i]) minn=min(minn,deep[v[i]]);
if (!--num[deep[now]]) break;
deep[now]=minn+1;
num[deep[now]]++;
cur[now]=point[now];
if (now!=s)
now=v[last[now]^1];
}
}
return ans;
}
void tarjan(int x)
{
ins[x]=1; st[++top]=x;
dfsn[x]=low[x]=++sz;
for (int i=point[x];i!=-1;i=next[i])
{
if (!remain[i]) continue;
if (!dfsn[v[i]]) tarjan(v[i]),low[x]=min(low[v[i]],low[x]);
else if (ins[v[i]]) low[x]=min(low[x],dfsn[v[i]]);
}
if (low[x]==dfsn[x])
{
++cnt; int t;
do
{
t=st[top--];
belong[t]=cnt;
ins[t]=0;
}while (t!=x);
}
}
int main()
{
freopen("a.in","r",stdin);
freopen("my.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&s1,&t1);
tot=-1;
memset(point,-1,sizeof(point));
memset(next,-1,sizeof(next));
for (int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z,i);
xx[i]=x; yy[i]=y;
}
int t2=isap(s1,t1); //cout<<t2<<endl;
//for (int i=1;i<=n;i++)
//for (int j=point[i];j!=-1;j=next[j])
// if (remain[i]) cout<<i<<" "<<v[j]<<endl;
for (int i=1;i<=n;i++)
if (!dfsn[i]) tarjan(i);
int s=belong[s1]; int t=belong[t1];
for (int i=1;i<=n;i++)
{
for (int j=point[i];j!=-1;j=next[j])
if (remain[j]!=0)
if (belong[i]!=belong[v[j]]&&j%2==1)
{
mark[u[j]]=1;
if ((belong[i]==s&&belong[v[j]]==t||belong[i]==t&&belong[v[j]]==s)&&j%2==1)
mark1[u[j]]=1;
}
}
for (int i=1;i<=m;i++)
printf("%d %d\n",mark[i],mark1[i]);
}