Pagini recente » Cod sursa (job #558941) | Cod sursa (job #547934) | Cod sursa (job #1524533) | Cod sursa (job #1919599) | Cod sursa (job #988764)
Cod sursa(job #988764)
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <algorithm>
#include <utility>
#include <cstring>
#include <string>
#include <stack>
#include <deque>
#include <iomanip>
#include <set>
#include <map>
#include <cassert>
#include <ctime>
#include <list>
#include <iomanip>
using namespace std;
string file = "maxflow";
ifstream cin( (file + ".in").c_str() );
ofstream cout( (file + ".out").c_str() );
const int MAXN = 1005;
const int oo = 0x3f3f3f3f;
typedef vector<int> Graph[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; }
struct ClassComp {
inline bool operator () (const int &a, const int &b) const {
return a > b;
}
};
Graph G;
int N, M, Flow[MAXN][MAXN], Capacity[MAXN][MAXN], Father[MAXN], MaxFlow;
bitset <MAXN> Used;
queue <int> Q;
const int So = 1; /// Source
int Si; /// Sink
inline bool BFs() {
while(!Q.empty())
Q.pop();
for(Q.push(So), Used.reset() ; !Q.empty() ; Q.pop()) {
int Node = Q.front();
Used[Node] = true;
for(It it = G[Node].begin(), fin = G[Node].end() ; it != fin ; ++ it)
if(!Used[*it] && Flow[Node][*it] < Capacity[Node][*it]) {
Q.push(*it);
Father[*it] = Node;
if(*it == Si)
return true;
}
}
return Used[Si];
}
int main() {
cin >> N >> M;
Si = N;
for(int i = 1 ; i <= M ; ++ i) {
int x, y, z;
cin >> x >> y >> z;
G[x].push_back(y);
G[y].push_back(x);
Capacity[x][y] = z;
}
for( ; BFs() ; ) {
for(It it = G[Si].begin(), fin = G[Si].end() ; it != fin ; ++ it) {
if(!Father[*it] || Capacity[*it][Si] <= Flow[*it][Si])
continue;
Father[Si] = *it;
int MinFlow = oo;
for(int i = Si ; i != So ; i = Father[i])
MinFlow = min(MinFlow, Capacity[Father[i]][i] - Flow[Father[i]][i]);
for(int i = Si ; i != So ; i = Father[i])
Flow[Father[i]][i] += MinFlow,
Flow[i][Father[i]] = -Flow[Father[i]][i];
MaxFlow += MinFlow;
}
}
cout << MaxFlow << '\n';
cin.close();
cout.close();
return 0;
}