Cod sursa(job #3336808)

Utilizator DinVinEmanuel DinVin Data 26 ianuarie 2026 00:54:17
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.71 kb
//edmonds karp
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");
int n,m,cap[1001][1001],flux[1001][1001];
vector<vector<int>> M;
vector<int> tata;
bool bfs(int s, int t) {
    tata.assign(n+1,0);
    tata[s] = -1;
    queue<int> Q;
    Q.push(s);
    while (!Q.empty()) {
        int u = Q.front(); Q.pop();
        for (auto v : M[u])
            if (tata[v] == 0 && cap[u][v] - flux[u][v] > 0) {
                tata[v] = u;
                if (v==t)return true;
                Q.push(v);
            }
    }
    return false;
}
void edmondskarp(int s, int t) {
    while (bfs(s,t)) {
        int capacitate_reziduala = 1e9;
        for (int v=t;v!=s;v=tata[v]) {
            int u = tata[v];
            capacitate_reziduala = min(capacitate_reziduala, cap[u][v] - flux[u][v]);
        }
        for (int v=t;v!=s;v=tata[v]) {
            int u = tata[v];
            flux[u][v] += capacitate_reziduala;
            flux[v][u] -= capacitate_reziduala;
        }
    }
}
int main() {
    fin>>n>>m;
    M.resize(n+1);
    for (int i=1;i<=m;i++) {
        int x,y,z;
        fin>>x>>y>>z;
        M[x].push_back(y);
        M[y].push_back(x);
        cap[x][y] = z;
    }
    edmondskarp(1,n);
    int fluxmaxim = 0;
    for (auto v : M[n]) {
        fluxmaxim += flux[v][n];
    }
    fout<<fluxmaxim;

    return 0;
}

// //flux cu ford-fulkerson
// #include <bits/stdc++.h>
// using namespace std;
// ifstream fin("date.in");
// ofstream fout ("maxflow.out");
// const int INF = 1e9;
// vector<vector<int>> M;
// int cap[1001][1001];
// int n,m;
// vector<bool> viz;
// int dfs (int u, int t, int flow) {
//     if (u == t) return flow;
//     viz[u] = true;
//     for (auto v : M[u])
//         if (!viz[v] && cap[u][v] > 0 ) {
//             int currflow = min(flow, cap[u][v]);
//             int tempflow = dfs(v, t, currflow);
//
//             if (tempflow > 0) {
//                 cap[u][v] -= tempflow;
//                 cap[v][u] += tempflow;
//                 return tempflow;
//             }
//         }
//     return 0;
// }
//
//
//
// int ford(int s, int t) {
//     int maxflow = 0;
//     while (true) {
//         int flux = dfs (s,t,INF);
//         if (flux == 0) break;
//         maxflow+=flux;
//         viz.assign(n+1,false);
//     }
//     return maxflow;
// }
// int main() {
//     fin>>n>>m;
//     M.resize(n+1);
//     viz.assign(n+1,false);
//     for (int i=1;i<=m;i++) {
//         int x,y,z;
//         fin>>x>>y>>z;
//         M[x].push_back(y);
//         M[y].push_back(x);
//         cap[x][y] = z;
//     }
//     cout<<ford(1,n);
//     return 0;
// }