zoukankan      html  css  js  c++  java
  • Drainage Ditches

    Drainage Ditches

    题目链接:http://poj.org/problem?id=1273

    网络流

    网络流模板题,学习网络流

    SAP时间复杂度为O(V*E^2),代码如下:

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <vector>
     5 #include <queue>
     6 #define N 205
     7 using namespace std;
     8 typedef long long LL;
     9 LL const inf=1000000000000;
    10 LL mp[N][N];
    11 LL n,m;
    12 bool vis[N];
    13 LL pre[N];
    14 void init(){
    15     memset(mp,0,sizeof(mp));
    16     for(LL i=0;i<n;++i){
    17         LL u,v,c;
    18         cin>>u>>v>>c;
    19         mp[u][v]+=c;
    20     }
    21 }
    22 LL bfs(LL s,LL t){
    23     memset(vis,0,sizeof(vis));
    24     memset(pre,-1,sizeof(pre));
    25     queue<LL>q;
    26     LL flow=inf;
    27     q.push(s);
    28     while(!q.empty()){
    29         LL p=q.front();q.pop();
    30         vis[p]=1;
    31         if(p==t)break;
    32         for(LL i=1;i<=m;++i){
    33             if(!vis[i]&&mp[p][i]!=0){
    34                 flow=min(flow,mp[p][i]);
    35                 pre[i]=p;
    36                 q.push(i);
    37             }
    38         }
    39     }
    40     if(pre[t]==-1)return 0;
    41     return flow;
    42 }
    43 void updata(LL t,LL flow){
    44     for(LL a=t;pre[a]!=-1;a=pre[a]){
    45         LL p=pre[a];
    46         mp[p][a]-=flow;
    47         mp[a][p]+=flow;
    48     }
    49 }
    50 LL getflow(LL s,LL t){
    51     LL maxflow=0,flow=0;
    52     while(1){
    53         flow=bfs(s,t);
    54         updata(t,flow);
    55         maxflow+=flow;
    56         if(flow==0)break;
    57     }
    58     return maxflow;
    59 }
    60 int main(void){
    61     while(cin>>n>>m){
    62         init();
    63         cout<<getflow(1,m)<<endl;
    64     }
    65 }
     1 struct Edge{
     2     int from,to,cap,flow;
     3     Edge(int _from=0,int _to=0,int _cap=0,int _flow=0){
     4         from=_from;to=_to;cap=_cap;flow=_flow;
     5     }
     6 };
     7 vector<Edge>e;
     8 vector<int>adj[207];
     9 int a[207],p[207];//a[i]为可增广的权值
    10 void addEdge(int from,int to,int cap){
    11     adj[from].push_back(e.size());
    12     adj[to].push_back(e.size()+1);
    13     e.push_back(Edge(from,to,cap,0));
    14     e.push_back(Edge(to,from,0,0));
    15 }
    16 int MaxFlow(int s,int t){
    17     int flow=0;
    18     while(1){
    19         queue<int>q;//findFlow
    20         q.push(s);
    21         memset(a,0,sizeof(a));
    22         a[1]=INF;
    23         while(!q.empty()){
    24             int u=q.front();q.pop();
    25             for(int i=0;i<adj[u].size();i++){
    26                 Edge ee=e[adj[u][i]];
    27                 int v=ee.to;
    28                 if(!a[v]&&ee.cap>ee.flow){
    29                     a[v]=min(a[u],ee.cap-ee.flow); 
    30                     p[v]=adj[u][i];//记录边的序号
    31                     q.push(v);
    32                 }
    33             }
    34             if(a[t])break;
    35         }
    36         if(!a[t])break;
    37         for(int i=t;i!=1;i=e[p[i]].from){//updata残余网络
    38             e[p[i]].flow+=a[t];
    39             e[p[i]^1].flow-=a[t];
    40         }
    41         flow+=a[t];
    42     }
    43     return flow;
    44 }
    另附邻接表版模板
  • 相关阅读:
    Lua ip转整数
    纯lua实现Base64加密与解密
    lua之base64的解码和编码(三种方案实现)
    Lua 5.1 位操作(与,或,异或操作)
    Lua打印Table对象
    Lua 截取字符串(截取utf-8格式字符串)
    lua 截取字符,以及取字符个数(非字符串长度)
    lua 加密解密
    Openwrt与贝壳物联平台通讯示例
    php socket编程:使用socket_recv而不是socket_read
  • 原文地址:https://www.cnblogs.com/barrier/p/6087097.html
Copyright © 2011-2022 走看看