poj 1273 maxflow 模版题

连连模版把
自己对反向边的理解还不深刻!用vector比用二维数组要好!

#include<iostream>
using namespace std;
#include<vector>
#include<cstdio>
#include<algorithm>
#include<cstring>

const int maxn=205;
const int inf=0x3f3f3f3f;
typedef struct notes{
    int aim,f,rev,st;
}note;
vector<note> G[maxn];
int rev_[maxn];
bool used[maxn];///
int n,m;
void Add_E(int u,int v,int c){
    note temp;
    temp.aim=v;
    temp.f=c;
    temp.rev=G[v].size();
    G[u].push_back(temp);
    temp.aim=u;
    temp.f=0;///
    temp.rev=G[u].size()-1;
    G[v].push_back(temp);
}

int min_(int aa,int bb){
    return aa>bb?bb:aa;
}
int dfs(int s,int e,int c_flow){
// printf("s=%d e=%d\n",s,e);
    if(s==e)return c_flow;
    int size_=G[s].size(),fl;
    used[s]=true;
    for(int i=0;i<size_;i++){
        note &num=G[s][i];
// printf("e=%d\n",G[s][i].aim);
        if(num.f<=0)continue;
        if(used[G[s][i].aim])continue;
        used[num.aim]=true;
        fl=dfs(G[s][i].aim,e,min_(num.f,c_flow));
        if(fl>0){
            num.f-=fl;
            G[num.aim][num.rev].f+=fl;
            return fl;
        }
    }
    return 0;
}
int maxflow(int s,int e){
    int ans=0,flag;
// memset(rev_,-1,sizeof(rev_));
    memset(used,false,sizeof(used));
    while(flag=dfs(s,e,inf)){
// printf("flag=%d\n",flag);
        if(flag==inf||flag==0)break;
        ans+=flag;
        memset(used,false,sizeof(used));
    }
    return ans;
}
int main(){
    #ifdef ONLINE_JUDGE
    #else
    freopen("1273.txt","r",stdin);
    #endif // ONLINE_JUDGE
    while(scanf("%d%d",&n,&m)!=-1){
        for(int i=0;i<maxn;i++){
            G[i].clear();
        }
        for(int i=0;i<n;i++){
            int u,v,c;
            scanf("%d%d%d",&u,&v,&c);
            Add_E(u,v,c);
        }
        printf("%d\n",maxflow(1,m));
    }
    return 0;
}

你可能感兴趣的:(poj 1273 maxflow 模版题)