zoukankan      html  css  js  c++  java
  • 【洛谷P2936】全流

    本人今天刚刚学会最大流,此题感觉完全没有提高+的难度,仅仅是一个模板最大流,我用了Dinic算法,而且本题数据很小,邻接矩阵存图即可。

    注意:本题大小写字母均包括在内!!

    被卡了一次10分

    Dinic模板的代码 (AC本题)

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <queue>
     5 using namespace std;
     6 const int INF=99999999;
     7 queue<int>q;
     8 int n,w[257][257],d[257];
     9 inline bool bfs()
    10 {
    11     memset(d,0,sizeof(d));
    12     d['A']=1;
    13     q.push('A');
    14     while(!q.empty())
    15     {
    16         int now=q.front();
    17         q.pop();
    18         for(int i='A';i<='z';i++)   //一定要到小写z
    19             if(w[now][i]>0&&d[i]==0)
    20             {
    21                 d[i]=d[now]+1;
    22                 q.push(i);
    23             }
    24     }
    25     return d['Z']!=0;
    26 }
    27 int dfs(int now,int dist)
    28 {
    29     if(now=='Z') return dist;
    30     for(int i='A';i<='z';i++)  //同上
    31         if(d[i]==d[now]+1&&w[now][i]>0)
    32         {
    33             int x=dfs(i,min(dist,w[now][i]));
    34             if(x>0)
    35             {
    36                 w[now][i]-=x;
    37                 w[i][now]+=x;
    38                 return x;
    39             }
    40         }
    41     return 0;
    42 }
    43 int main()
    44 {
    45     cin>>n;
    46     for(int i=1;i<=n;i++)
    47     {
    48         char x,y;
    49         int z;
    50         cin>>x>>y>>z;
    51         w[x][y]+=z;
    52     }
    53     int ans=0;
    54     while(bfs())
    55     {
    56         while(int di=dfs('A',INF))
    57             ans+=di;
    58     }
    59     cout<<ans;
    60     return 0;
    61 }
  • 相关阅读:
    hadoop集群部署入门(传智Hadoop学习)
    遇到问题了!
    MD5和TreeView的学习
    今天晚上完成了一个登录功能
    第一次来到博客园!
    单元测试--测?
    单元测试-公司实习1
    Mariadb数据库小结
    [奋斗的人生] 学习,总结,感恩,回馈
    将博客搬至CSDN
  • 原文地址:https://www.cnblogs.com/shl-blog/p/10500790.html
Copyright © 2011-2022 走看看