1797: [Ahoi2009]Mincut 最小割
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2416 Solved: 1043
[ 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
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
1 0
0 0
1 0
0 0
1 0
1 0
题解:一道神奇的题。
首先,肯定得跑一遍最小割。
然后对于残余网络,我们求强连通。
对于(x,y)的这条边,如果它的流量是满的,同时x和y不属于一个强连通的话,那么这条边存在其中一个最小割中
如果它的流量是满的,同时x和s是一个强连通,y和t是一个强连通,那么它存在在所以最小割中。
证明?!呃呃呃,画一个图,应该就可以了。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<stack>
using namespace std;
const int N=60100;
struct node{
int x,y,z,next,other;
}sa[N*4];int len=1,first[N];
int n,m,st,ed;
void ins(int x,int y,int z)
{
len++;
sa[len].x=x;
sa[len].y=y;
sa[len].z=z;
sa[len].next=first[x];
sa[len].other=len+1;
first[x]=len;
len++;
sa[len].x=y;
sa[len].y=x;
sa[len].z=0;
sa[len].next=first[y];
sa[len].other=len-1;
first[y]=len;
}
int tail,head,q[N],dep[N];
bool build()
{
memset(dep,0,sizeof(dep));dep[st]=1;
tail=2;head=1;q[head]=st;
while(tail!=head)
{
int x=q[head];
for(int i=first[x];i!=-1;i=sa[i].next)
{
int y=sa[i].y;
if(sa[i].z&&dep[y]==0)
{
dep[y]=dep[x]+1;
q[tail++]=y;
}
}
head++;
}
if(dep[ed]==0) return false;
return true;
}
int get_ans(int x,int f)
{
if(x==ed) return f;
int s=0,t;
for(int i=first[x];i!=-1;i=sa[i].next)
{
int y=sa[i].y;
if(dep[y]==dep[x]+1&&sa[i].z&&f-s>0)
{
t=get_ans(y,min(f-s,sa[i].z));
s+=t;
sa[i].z-=t;
sa[sa[i].other].z+=t;
}
}
if(s==0) dep[x]=0;
return s;
}
int pre[N],low[N],belog[N],be=0,dfn=0;
stack<int> yu;
void tarjan(int x)
{
pre[x]=low[x]=++dfn;
yu.push(x);
for(int i=first[x];i!=-1;i=sa[i].next)
{
int y=sa[i].y;
if(sa[i].z)
{
if(!pre[y])
{
tarjan(y);
low[x]=min(low[x],low[y]);
}
else if(!belog[y])low[x]=min(low[x],pre[y]);
}
}
if(pre[x]==low[x])
{
be++;
while(1)
{
int xx=yu.top();yu.pop();
belog[xx]=be;
if(xx==x) break;
}
}
}
int main()
{
scanf("%d%d%d%d",&n,&m,&st,&ed);
memset(first,-1,sizeof(first));
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
ins(x,y,z);
}
int ans=0;
while(build()==true)
{
ans+=get_ans(st,99999999);
}
// printf("%d\n",ans);
for(int i=1;i<=n;i++)
if(!pre[i]) tarjan(i);
for(int i=1;i<=m;i++)
if(sa[i<<1].z) printf("0 0\n");
else
{
//printf("!");
int x=sa[i<<1].y,y=sa[i<<1|1].y;
printf("%d %d\n",belog[x]!=belog[y],belog[st]==belog[y]&&belog[ed]==belog[x]);
}
}