1 条题解

  • 0
    @ 2024-12-22 11:04:03

    C++ :

    //#include<bits/stdc++.h>
    #include<stdio.h>
    #include<vector>
    #include<string.h>
    #include<algorithm>
    #define Max_v 210
    #define INF 1<<30
    using namespace std;
    typedef struct edge
    {
        int to,cap,rev;
    };
    vector<edge>G[Max_v];
    bool used[Max_v];
    void add_edge(int from,int to,int cap)
    {
    	edge k;
    	k.to=to;k.cap=cap;k.rev=G[to].size();
        G[from].push_back(k);
    	k.to=from;k.cap=0;k.rev=G[from].size()-1;
    	G[to].push_back(k);
    }
    int dfs(int v,int t,int f)
    {
        if (v==t) return f;
        used[v]=true;
        for (int i=0;i<G[v].size();i++)
        {
            edge &e =G[v][i];
            if (!used[e.to] && e.cap > 0)  //之前走过的路,必定会填满,也是防止走循环
            {
                int d=dfs(e.to,t,min(e.cap,f));
                if (d>0)
                {
                    e.cap-=d;
                    G[e.to][e.rev].cap+=d;    //反向边增长
                    return d;
                }
            }
        }
        return 0;
    }
    int max_flow(int s,int t)
    {
        int flow=0;
        while(1)
        {
            memset(used,0,sizeof(used));
            int f = dfs(s,t,INF);
            if (f==0) return flow;
            flow+=f;
        }
    }
    void init(void)
    {
    	for (int i=0;i<Max_v;i++)
    		G[i].clear();
    }
    int main()
    {
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
    		init();
            while(n--)
            {
                int from,to,cap;
                scanf("%d%d%d",&from,&to,&cap);
                add_edge(from,to,cap);
            }
            printf("%d\n",max_flow(1,m));
        }
        return 0;
    }
    
    
    • 1

    信息

    ID
    290
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者