Cod sursa(job #2763958)

Utilizator BossBobsterRobert Alexandru Costin BossBobster Data 18 iulie 2021 09:06:46
Problema Flux maxim Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.29 kb
#include <iostream>
#include <string.h>
#include <random>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
typedef pair<string, string> pss;
typedef pair<int, char> pic;
typedef pair<pii, int> piii;
typedef pair<double, double> pdd;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ull, ull> pull;
typedef complex<double> cd;
//#define max(n, m) ((n>m)?n:m)
//#define min(n, m) ((n<m)?n:m)
#define f first
#define s second
#define input() ios_base::sync_with_stdio(0);cin.tie(0);

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

struct edge
{
    int nx, cap, cur;
};

vector<edge> adj[1010];
queue<pii> nextInLine;
bitset<1010> visited;
vector<pii> ord;
int main()
{
    int n, m, a, b, c, curN, curF, ans = 0;
    fin >> n >> m;
    for(int i = 0; i < m; i ++)
    {
        fin >> a >> b >> c; a--; b--;
        adj[a].push_back({b, c, 0}); //adj[b].push_back({a, -c, 0});
    }
    for(int i = 0; i < m; i ++)
    {
        nextInLine.push({0, 2000000000});
        visited.reset(); ord.clear();
        while(!nextInLine.empty())
        {
            curN = nextInLine.front().f; curF = nextInLine.front().s;
            nextInLine.pop();
            if(visited[curN]) continue;
            visited[curN] = true;
            for(int i = 0; i < adj[curN].size(); i ++)
            {
                edge it = adj[curN][i];
                if(it.cur < it.cap)
                {
                    nextInLine.push({it.nx, min(curF, it.cap-it.cur)});
                    ord.push_back({curN, i});
                    break;
                }
            }
        }
        for(int i = 0; i < ord.size(); i ++)
            adj[ord[i].f][ord[i].s].cur += curF;
    }
    for(int i = 0; i < n; i++)
        for(auto it : adj[i])
            if(it.nx == n-1)
                ans += it.cur;
    fout << ans << "\n";
}