Pagini recente » Cod sursa (job #1074795) | Cod sursa (job #2007317) | Cod sursa (job #1674507) | Istoria paginii runda/winners28 | Cod sursa (job #1214820)
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <unordered_map>
using namespace std;
const char infile[] = "maxflow.in";
const char outfile[] = "maxflow.out";
const int MAXN = 100005;
const int oo = 0x3f3f3f3f;
ifstream fin(infile);
ofstream fout(outfile);
typedef vector<int> ggg[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
class Graph {
public:
Graph() {
}
Graph(int N) {
g.resize(N);
father.resize(N);
#ifdef debug
used.resize(N);
#endif
n = N;
}
void build(int N) {
g.resize(N);
father.resize(N);
#ifdef debug
used.resize(N);
#endif
n = N;
}
void addEdge(int x, int y, int capacity) {
g[x].push_back(y);
_capacity[x][y] += capacity;
}
bool bfs(int source, int sink) {
#ifdef debug
fill(used.begin(), used.end(), 0);// debug
#else
used.reset();
#endif
q.push(source);
used[source] = 1;
while(!q.empty()) {
int node = q.front();
q.pop();
if(node == sink)
continue;
for(It it = g[node].begin(), fin = g[node].end(); it != fin ; ++ it)
if(!used[*it] && _capacity[node][*it] > _flow[node][*it]) {
used[*it] = 1;
father[*it] = node;
q.push(*it);
}
}
return used[sink];
}
int getMaxFlow(int source, int sink) {
int maxFlow = 0;
while(bfs(source, sink)) {
for(It it = g[sink].begin(), fin = g[sink].end() ; it != fin ; ++ it) {
if(!used[*it] || _capacity[*it][sink] <= _flow[*it][sink])
continue;
int bottleneck = oo;
father[sink] = *it;
for(int i = sink ; i != source ; i = father[i])
bottleneck = min(bottleneck, _capacity[father[i]][i] - _flow[father[i]][i]);
if(!bottleneck)
continue;
for(int i = sink ; i != source ; i = father[i]) {
_flow[father[i]][i] += bottleneck;
_flow[i][father[i]] -= bottleneck;
}
maxFlow += bottleneck;
}
}
return maxFlow;
}
private:
queue <int> q;
#ifdef debug
vector <bool> used;
#else
bitset <MAXN> used;
#endif
int n;
vector <vector <int> > g;
vector <int> father;
map <int, map<int, int> > _capacity, _flow;
} G;
int main() {
int n, m;
fin >> n >> m;
G.build(n);
while(m -- ) {
int x, y, c;
fin >> x >> y >> c;
G.addEdge(x - 1, y - 1, c);
G.addEdge(y - 1, x - 1, 0);
}
fout << G.getMaxFlow(0, n - 1) << '\n';
fin.close();
fout.close();
return 0;
}