Cod sursa(job #2617174)

Utilizator drknss_Hehe hehe drknss_ Data 20 mai 2020 21:27:42
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.92 kb
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define forn(i,a,b) for (int i = a; i <= b; i++)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rc(s) return cout<<s,0
#define pi pair <int, int>
#define sz(x) (int)((x).size())
#define int long long

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const ll inf = 0x3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const double lil = 0.0000000000001;
const ll INF64 = 3e18 + 1;
const int N = 1e3 + 11;

ifstream in("maxflow.in");
ofstream out("maxflow.out");

int c[N][N],flow[N][N],p[N],n,m,ans;

vector<int>v[N];

bool viz[N];

bool bfs(){

    memset(viz, 0, sizeof(viz));

    queue<int>q;
    q.push(1);

    viz[1] = 1;

    while(!q.empty()){

        int nod = q.front(); q.pop();

        if(nod == n)continue;

        for(auto it : v[nod]){

            if(c[nod][it] == flow[nod][it] || viz[it])continue;

            viz[it] = 1;
            p[it] = nod;
            q.push(it);
        }
    }

    return viz[n];
}



int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0); cerr.tie(0); cout.tie(0);

    in >> n >> m;

    while(m--){

        int x, y, e;

        in >> x >> y >> e;

        v[x].push_back(y);
        v[y].push_back(x);

        c[x][y] = e;

    }

    bfs();

    while(bfs()){

        for(auto it : v[n]){

            if(c[it][n] == flow[it][n] || !viz[it])continue;

            int mnflow = INF64;

            for(int i = n; i != 1; i = p[i])mnflow = min(mnflow , c[p[i]][i] - flow[p[i]][i]);

            if(mnflow == 0)continue;

            for(int i = n; i != 1; i = p[i]){

                flow[p[i]][i] += mnflow;
                flow[i][p[i]] -= mnflow;
            }

            ans += mnflow;
        }

    }

    out << ans << '\n';

return 0;
}