Pagini recente » Cod sursa (job #1640089) | Profil Ryuzaki | Cod sursa (job #2421114) | Cod sursa (job #992097) | Cod sursa (job #3136709)
#include <bits/stdc++.h>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
/*
struct edge{
int node;
int cap;
int flow;
edge(int node, int cap, int flow) : node(node), cap(cap), flow(flow) {}
};
*/
pair < int , int > flow[1005][1005];
deque < int > q;
int path[1005], n_path;
bool inpath[1005];
int max_flow;
int n,m,x,y;
int source,sink;
bool ok;
void read() {
f >> n >> m;
source = 1; sink = n;
for (int i=1;i<=m;i++) {
f >> x >> y;
f >> flow[x][y].second;
}
}
void dfs(int nod) {
path[++n_path] = nod;
inpath[nod] = 1;
if (nod == sink) ok=1;
for (int i=1;i<=n && !ok;i++) {
if (flow[nod][i].second!=flow[nod][i].first && !inpath[i]) {
dfs(i);
if (!ok) {
inpath[i]=0;
--n_path;
}
}
}
}
void ford_fulk() {
ok = 1;
int total = 0;
while (ok) {
ok = 0;
n_path = 0;
dfs(source);
if (ok) {
int bottleneck = INT_MAX;
for (int i=1;i<n_path;i++) {
bottleneck = min(bottleneck, flow[path[i]][path[i+1]].second-flow[path[i]][path[i+1]].first);
inpath[path[i+1]]=0;
}
for (int i=1;i<n_path;i++) {
flow[path[i]][path[i+1]].first += bottleneck;
flow[path[i+1]][path[i]].first -= bottleneck;
}
max_flow += bottleneck;
}
}
g << max_flow;
}
int main()
{
read();
ford_fulk();
return 0;
}