[Ahoi2009]Mincut 最小割


1797: [Ahoi2009]Mincut 最小割

Time Limit: 10 Sec   Memory Limit: 162 MB
Submit: 2457   Solved: 1058
[ 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

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


Ø题解:在残余网络上跑tarjan求出所有SCC,记id[u]为点u所在SCC的编号。显然有id[s]!=id[t](否则s到t有通路,能继续增广)。
1.对于任意一条满流边(u,v),(u,v)能够出现在某个最小割集中,当且仅当id[u]!=id[v];
2.对于任意一条满流边(u,v),(u,v)必定出现在最小割集中,当且仅当id[u]==id[s]且id[v]==id[t]。
①将每个SCC缩成一个点,得到的新图就只含有满流边了。那么新图的任一s-t割都对应原图的某个最小割,从中任取一个把id[u]和id[v]割开的割即可证明。
②假设将(u,v)的边权增大,那么残余网络中会出现s->u->v->t的通路,从而能继续增广,于是最大流流量(也就是最小割容量)会增大。这即说明(u,v)是最小割集中必须出现的边。(在 残网上)

#include<iostream>

#include<cmath>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<cstdlib>
#include<algorithm>
#define V 4605
#define mod 1000000007
#define LL long long
using namespace std;
int n,m,sd,T,S;//a[V][V],s[V][V],g[V][V],
int pre[V],dep[V],q[V*2];
int top,scc,cnt,G[V*2],dfn[V],low[V],vis[V],bl[V];
struct da
{
 int to,next,dis;       
}Edge[120305];
int head[V],tot;
inline void add(int x,int y,int zz)
{
  Edge[tot].to=y;
  Edge[tot].dis=zz;
  Edge[tot].next=head[x];
  head[x]=tot++;  
  Edge[tot].to=x;
  Edge[tot].dis=0;
  Edge[tot].next=head[y];
  head[y]=tot++;     
}
bool Bfs() {
    memset(dep, 0, sizeof(dep));
    int hd,tl;
    hd = tl = 0;
    q[++ tl] = S, dep[S] = 1;
    while(hd<tl) {
        int op = q[++hd];
        for(int i = head[op] ; i != -1 ; i = Edge[i].next) {
            if(Edge[i].dis&&(!dep[Edge[i].to])) {
                dep[Edge[i].to] = dep[op]+1;
                q[++ tl] = Edge[i].to;
                if(Edge[i].to==T) return true;
            }
        }
    }
    return false;
}
int Dfs(int op, int fw) {
    if(op==T) return fw;           
    int tmp =fw,k;
    for(int i = head[op] ; i != -1 ; i = Edge[i].next) {
        if(Edge[i].dis&&tmp&&dep[Edge[i].to]==dep[op]+1) {
            k = Dfs(Edge[i].to, min(Edge[i].dis, tmp));
            if(!k) { 
                dep[Edge[i].to] = 0;
                continue;
            }
            Edge[i].dis-= k, Edge[i^1].dis+= k,tmp-= k;
        }
    }
    return fw-tmp;
}
int solve()
{
   int i,flow=0;
   int z;
   while(Bfs())
   {
  // z=Dfs(S,mod);
    //while(z)
    //{
    flow+=Dfs(S,mod); //z;//add();///           
    //z=Dfs(S,mod);
    //}
   }
   return flow;
}
void tar(int x)
{
  int v;
  dfn[x]=low[x]=++cnt;
  G[top++]=x;
  vis[x]=1;
  for(int i=head[x];i!=-1;i=Edge[i].next)
  {
      v=Edge[i].to;
      if(!Edge[i].dis)continue;
      if(!dfn[v])
      {
        tar(v);
        low[x]=min(low[v],low[x]);          
      }
      else if(vis[v])
      {
        low[x]=min(low[x],dfn[v]);     
      }
  }
  if(low[x]==dfn[x])
  {
    scc++;
    while(1)
    {
      v=G[--top];
      bl[v]=scc;
      vis[v]=0;
      if(v==x)break;        
            
    }                  
  }


}
inline int haha()
{
    //freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
    // freopen("gro.in","r",stdin); freopen("gro.out","w",stdout);
    memset(head,-1,sizeof(head));
    cin>>n>>m>>S>>T;
    //cout<<S<<" ## "<<T<<endl;
    int x,y,z;
    for(int i=1;i<=m;i++)
    {
      //cin>>x>>y>>z;
      scanf("%d%d%d",&x,&y,&z); 
      add(x,y,z);        
    }
    int sb=solve();
    for(int i=1;i<=n;i++)
     if(!dfn[i])tar(i);
    for(int i=0;i<tot;i+=2)
    {
       if(bl[Edge[i].to]!=bl[Edge[i^1].to]&&!Edge[i].dis)
       printf("1 ");
       else printf("0 ");
       //cout<<0;
       if(bl[Edge[i].to]!=bl[Edge[i^1].to]&&!Edge[i].dis&&bl[Edge[i].to]==bl[T]&&bl[Edge[i^1].to]==bl[S])
       printf("1\n");//cout<<" "<<1<<endl;
       else
       printf("0\n");//cout<<" "<<0<<endl;        
    }
   // printf("%d",solve());
    return 0;
}
int gg=haha();
int main()
{;}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值