• 【最小割】【Dinic】【强联通分量缩点】bzoj1797 [Ahoi2009]Mincut 最小割


    结论:

    满足条件一:当一条边的起点和终点不在 残量网络的 一个强联通分量中。且满流。

    满足条件二:当一条边的起点和终点分别在 S 和 T 的强联通分量中。且满流。、

    网上题解很多的。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<vector>
     4 #include<algorithm>
     5 #include<queue>
     6 using namespace std;
     7 #define INF 2147483647
     8 #define MAXN 4011
     9 #define MAXM 120101
    10 int v[MAXM],cap[MAXM],en,first[MAXN],next[MAXM];
    11 int d[MAXN],cur[MAXN],cmp[MAXN],sum;
    12 bool vis[MAXN];
    13 queue<int>q;
    14 vector<int>vs;
    15 int n,m,S,T,A,B,C;
    16 void Init_Dinic(){memset(first,-1,sizeof(first)); en=0;}
    17 void AddEdge(const int &U,const int &V,const int &W)
    18 {v[en]=V; cap[en]=W; next[en]=first[U]; first[U]=en++;
    19 v[en]=U; next[en]=first[V]; first[V]=en++;}
    20 bool bfs()
    21 {
    22     memset(d,-1,sizeof(d)); q.push(S); d[S]=0;
    23     while(!q.empty())
    24       {
    25         int U=q.front(); q.pop();
    26         for(int i=first[U];i!=-1;i=next[i])
    27           if(d[v[i]]==-1 && cap[i])
    28             {
    29               d[v[i]]=d[U]+1;
    30               q.push(v[i]);
    31             }
    32       }
    33     return d[T]!=-1;
    34 }
    35 int dfs(int U,int a)
    36 {
    37     if(U==T || !a) return a;
    38     int Flow=0,f;
    39     for(int &i=cur[U];i!=-1;i=next[i])
    40       if(d[U]+1==d[v[i]] && (f=dfs(v[i],min(a,cap[i]))))
    41         {
    42           cap[i]-=f; cap[i^1]+=f;
    43           Flow+=f; a-=f; if(!a) break;
    44         }
    45     if(!Flow) d[U]=-1;
    46     return Flow;
    47 }
    48 void max_flow()
    49 {
    50     while(bfs())
    51       {
    52         memcpy(cur,first,(n+5)*sizeof(int));
    53         while(dfs(S,INF));
    54       }
    55 }
    56 void dfs(int U)
    57 {
    58     vis[U]=1;
    59     for(int i=first[U];i!=-1;i=next[i]) if(cap[i]&&(!vis[v[i]])) dfs(v[i]);
    60     vs.push_back(U);
    61 }
    62 void dfs2(int U)
    63 {
    64     cmp[U]=sum;
    65     for(int i=first[U];i!=-1;i=next[i]) if(cap[i^1]&&(!cmp[v[i]])) dfs2(v[i]);
    66 }
    67 void scc()
    68 {
    69     for(int i=1;i<=n;i++) if(!vis[i]) dfs(i);
    70     vector<int>::iterator it=vs.end(); --it;
    71     for(;;--it)
    72       {
    73         if(!cmp[*it]) {++sum; dfs2(*it);}
    74         if(it==vs.begin()) break;
    75       }
    76 }
    77 int main()
    78 {
    79     scanf("%d%d%d%d",&n,&m,&S,&T); Init_Dinic();
    80     for(;m;--m)
    81       {
    82           scanf("%d%d%d",&A,&B,&C);
    83           AddEdge(A,B,C);
    84       }
    85     max_flow(); scc();
    86     for(int i=0;i<en;i+=2)
    87       printf("%d %d
    ",(!cap[i])&&cmp[v[i+1]]!=cmp[v[i]],(!cap[i])&&cmp[v[i+1]]==cmp[S]&&cmp[v[i]]==cmp[T]);
    88     return 0;
    89 }
  • 相关阅读:
    postgresql客户端连接错误的解决方法【转】
    在游戏开发中使用管理类的目的和作用
    Unity3D对象池
    yield的作用
    Unity延迟和重复调用方法
    Unity的Asset Store商店下载文件路径
    C#委托和事件详解
    C#一个关于委托和事件通俗易懂的例子
    C#委托和事件定义和使用
    C#委托和事件
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/4152713.html
Copyright © 2020-2023  润新知