Cod sursa(job #1750556)

Utilizator HuskyezTarnu Cristian Huskyez Data 30 august 2016 14:48:30
Problema Flux maxim Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include <fstream>
#include <vector>
#include <queue>
#include <bits/stdc++.h>
#include <cstring>

using namespace std;

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

vector<int> graph[1005];
int parent[1005];
queue<int> q;

bool vis[1005];

int cap[1005][1005];

int n, m;
int x, y, c;

int max_flow;


bool BFS()
{
    memset(vis, 0, sizeof(vis));
    memset(parent, 0, sizeof(parent));
    vis[1] = true;
    q.push(1);
    while(!q.empty()){
        int node = q.front();
        vis[node] = true;
        for(int i=0; i<graph[node].size(); i++){
            if(!vis[graph[node][i]] && cap[node][graph[node][i]]){
                q.push(graph[node][i]);
                parent[graph[node][i]] = node;
            }
        }
        q.pop();
    }
    return vis[n];
}

int main()
{
    in >> n >> m;
    for(int i=0; i<m; i++){
        in >> x >> y >> c;
        graph[x].push_back(y);
        graph[y].push_back(x);
        cap[x][y] = c;
    }

    while(BFS()){
        int flow = 0xfffff;
        for(int x=parent[n]; x != 1; x = parent[x]){
            if(cap[parent[x]][x] < flow){
                flow = cap[parent[x]][x];
            }
        }
        for(int x=parent[n]; x != 1; x = parent[x]){
            cap[parent[x]][x] -= flow;
            cap[x][parent[x]] += flow;
        }
        max_flow += flow;
    }

    out << max_flow << '\n';

    return 0;
}