Pagini recente » Cod sursa (job #1312739) | Cod sursa (job #2606620) | Cod sursa (job #2529963) | Cod sursa (job #46901) | Cod sursa (job #1669248)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#include <algorithm>
using namespace std;
ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");
const int nmax = 1005;
const int oo = 2e9;
vector <int> g[nmax];
bitset <nmax> viz;
int dady[nmax], c[nmax][nmax], f[nmax][nmax], n, m;
inline bool bfs(int source, int dest) {
vector <int> :: iterator son;
queue <int> q;
int dad;
viz=0;
q.push(source);
viz[source]=true;
while(!q.empty()) {
dad=q.front();
q.pop();
for(son=g[dad].begin(); son!=g[dad].end(); son++)
if(viz[*son]==false && c[dad][*son] > f[dad][*son]) {
viz[*son]=true;
dady[*son]=dad;
q.push(*son);
}
}
return viz[dest];
}
void Edmunson_Karp(int source, int dest) {
vector <int> :: iterator son;
int maxflow=0, flow, node;
while(bfs(source, dest)) {
for(son=g[dest].begin(); son!=g[dest].end(); son++) {
if(viz[*son]==false) continue;
if(c[*son][dest] <= f[*son][dest]) continue;
flow=oo;
for(node=*son; node!=source; node=dady[node]) {
flow=min(flow, c[dady[node]][node]-f[dady[node]][node]);
}
if(flow==0) continue;
maxflow+=flow;
for(node=*son; node!=source; node=dady[node]){
f[dady[node]][node]+=flow;
f[node][dady[node]]-=flow;
}
}
}
fout << maxflow;
}
int main() {
ios_base::sync_with_stdio(false);
int i, x, y, z;
fin >> n >> m;
for(i=1; i<=m; i++)
{
fin >> x >> y >> z;
c[x][y]+=z;
g[x].push_back(y);
g[y].push_back(x);
}
Edmunson_Karp(1, n);
fin.close();
fout.close();
return 0;
}