Pagini recente » Cod sursa (job #2450455) | Cod sursa (job #1329808) | Cod sursa (job #2443362) | Cod sursa (job #1322586) | Cod sursa (job #2776132)
#include <fstream>
#include <string.h>
#include <queue>
#include <vector>
#define NMAX 1005
using namespace std;
/************************************/
/// INPUT / OUTPUT
ifstream f("maxflow.in");
ofstream g("maxflow.out");
/************************************/
/// GLOBAL DECLARATIONS
int N, M, S, D;
int maxFlow, startingNode;
int flow[NMAX][NMAX], parent[NMAX], cap[NMAX][NMAX];
vector < int > adj[NMAX];
queue < int > q;
/************************************/
/// FUNCTIONS
void ReadInput();
void Solution();
void Output();
/************************************/
///-------------------------------------------------
inline void ReadInput()
{
f >> N >> M;
S = 1;
D = N;
for (int i = 1 ; i <= M ; ++ i)
{
int a, b, flux;
f >> a >> b >> flux;
cap[a][b] = flux;
adj[a].push_back(b);
adj[b].push_back(a);
}
}
///-------------------------------------------------
bool BFS()
{
memset(parent, 0, sizeof(parent));
startingNode = 0;
q.push(S);
while (!q.empty())
{
int node = q.front();
q.pop();
for (auto u: adj[node])
{
if (!parent[u] && flow[node][u] < cap[node][u])
{
parent[u] = node;
if (u == D)
{
startingNode = node;
continue;
}
q.push(u);
}
}
}
return startingNode;
}
///-------------------------------------------------
inline void Solution()
{
while (BFS())
{
int flux = 1e8;
int node = D;
while (node != S)
{
flux = min(flux, (cap[parent[node]][node] - flow[parent[node]][node]));
node = parent[node];
}
maxFlow += flux;
node = D;
while (node != S)
{
int par = parent[node];
flow[par][node] += flux;
flow[node][par] -= flux;
node = par;
}
}
}
///-------------------------------------------------
inline void Output()
{
g << maxFlow;
}
///-------------------------------------------------
int main()
{
ReadInput();
Solution();
Output();
return 0;
}