EK算法 68pts #8 9 10 11WA求调
查看原帖
EK算法 68pts #8 9 10 11WA求调
1357737
xialingn楼主2024/8/18 00:07
#include<bits/stdc++.h>
using namespace std;
const int maxn = 205;
const int maxm = 5005;
const int flow_max = 0x7fffffff;
int capa[maxn][maxn], flow[maxn];  //总的,记录残留网络的容量,标记从源点到当前节点实际还剩多少流量可用
int n, m, s, t, pre[maxn], maxflow=0, increase=0;
queue<int> q;

int bfs(int src, int des) {
    while(!q.empty()) q.pop();  //队列清空
    for (int i=1; i <= n; i++) pre[i] = -1;  
    flow[src] = flow_max;
    pre[src] = 0;  
    q.push(src);

    while (!q.empty()) {
        int v = q.front();
        q.pop();
        if (v == des) break;  
        for (int i=1; i <= n; i ++) { 
            if(capa[v][i] > 0 && i != src && pre[i] == -1) { //存在路径且该点之前未探索过
                q.push(i);
                pre[i] = v;
                
                flow[i] = min(capa[v][i], flow[v]); 
            }
        }
    }
    if (pre[des] == -1) return -1;  //残留图中不再存在增广路
    else return flow[des]; 

}

int maxFlow(int src, int des ) { //开始实施
    int increase = 0;
    int sumflow = 0;
    while ((increase = bfs(src, des)) != -1) {
        int k = des;
        while(k != src) {
            int last = pre[k];
            capa[last][k] -= increase;
            capa[k][last] += increase;
            k = last;
        }
        sumflow += increase;
    }
    return sumflow;
}

int main(void) {
    cin >> n >> m >> s >> t;
    memset(capa, 0, sizeof(capa));
    memset(flow, 0, sizeof(flow));
    //memset(pre, -1, sizeof(pre));  //不能在主函数里面更新,因为这样就只更新了一次
    int x, y;
    int c;
    for (int i=0; i < m; i ++) {
        cin >> x >> y >> c;
        capa[x][y] += c;
    }
  
    cout << maxFlow(s, t);
    return 0;
}

2024/8/18 00:07
加载中...