Pagini recente » Cod sursa (job #970486) | Cod sursa (job #1711061) | Cod sursa (job #3290408) | Cod sursa (job #1848033) | Cod sursa (job #1333868)
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>
#define Nmax 1010
#define oo (1 << 30)
#define neighbour Graph[node][i]
using namespace std;
vector <int> Graph[Nmax];
int N, maxFlow, Source, Destination, Father[Nmax], Capacity[Nmax][Nmax];
bool used[Nmax];
bool Bfs() {
int i, node;
queue <int> Queue;
memset(used, 0, sizeof(used));
Queue.push(Source);
used[Source] = true;
while(!Queue.empty()) {
node = Queue.front();
Queue.pop();
for(i = 0; i < Graph[node].size(); i++)
if(!used[neighbour] && Capacity[node][neighbour] > 0) {
Queue.push(neighbour);
used[neighbour] = true;
Father[neighbour] = node;
if(neighbour == Destination)
return true;
}
}
return false;
}
void Solve() {
int i, node, maxCapacity;
Source = 1;
Destination = N;
while(Bfs())
for(i = 0; i < Graph[Destination].size(); i++)
if(used[node = Graph[Destination][i]] && Capacity[node][Destination] > 0) {
maxCapacity = oo;
Father[Destination] = node;
for(node = Destination; node != Source; node = Father[node])
maxCapacity = min(maxCapacity, Capacity[Father[node]][node]);
for(node = Destination; node != Source; node = Father[node]) {
Capacity[Father[node]][node] -= maxCapacity;
Capacity[node][Father[node]] += maxCapacity;
}
maxFlow += maxCapacity;
}
}
void Read() {
int x, y, c, M;
ifstream in("maxflow.in");
in >> N >> M;
while(M--) {
in >> x >> y >> c;
Graph[x].push_back(y);
Graph[y].push_back(x);
Capacity[x][y] = c;
}
in.close();
}
void Write() {
ofstream out("maxflow.out");
out << maxFlow << '\n';
out.close();
}
int main() {
Read();
Solve();
Write();
return 0;
}