Pagini recente » Cod sursa (job #1644200) | Cod sursa (job #2755545) | Cod sursa (job #452913) | Cod sursa (job #2130779) | Cod sursa (job #1365933)
#include <iostream>
#include <fstream>
#include <bitset>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int maxn = 1005;
const int oo = 0x3f3f3f3f;
int n, m, c[maxn][maxn], father[maxn];
vector <int> g[maxn];
queue <int> q;
bitset <maxn> used;
inline bool bfs(int source, int sink) {
used.reset();
q.push(source);
used[source] = 1;
while(!q.empty()) {
int node = q.front();
q.pop();
if(node == sink)
continue;
for(vector <int> :: iterator it = g[node].begin() ; it != g[node].end() ; ++ it)
if(!used[*it] && c[node][*it] > 0) {
used[*it] = 1;
father[*it] = node;
q.push(*it);
}
}
return used[sink];
}
inline int getmaxflow(int source, int sink) {
int maxflow = 0;
while(bfs(source, sink))
for(vector <int> :: iterator it = g[sink].begin() ; it != g[sink].end() ; ++ it) {
if(!used[*it] || c[*it][sink] <= 0)
continue;
father[sink] = *it;
int bottleneck = oo;
for(int i = sink ; i != source ; i = father[i])
bottleneck = min(bottleneck, c[father[i]][i]);
for(int i = sink ; i != source ; i = father[i]) {
c[father[i]][i] -= bottleneck;
c[i][father[i]] += bottleneck;
}
maxflow += bottleneck;
}
return maxflow;
}
int main() {
fin >> n >> m;
for(int i = 1 ; i <= m ; ++ i) {
int x, y, z;
fin >> x >> y >> z;
g[x].push_back(y);
g[y].push_back(x);
c[x][y] = z;
}
fout << getmaxflow(1, n) << '\n';
}