Pagini recente » Cod sursa (job #1452125) | Cod sursa (job #2051536) | Cod sursa (job #1339789)
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#define DIM 1005
#define INF 2000000000
#define vint vector<int>::iterator
#define infile "maxflow.in"
#define outfile "maxflow.out"
using namespace std;
ifstream f(infile);
ofstream g(outfile);
bool ok[DIM];
int nodeCount, edgeCount;
int edgeCapacity[DIM][DIM], edgeFlow[DIM][DIM];
int nodeParent[DIM];
vector<int> Graph[DIM];
queue<int> Queue;
bool bfs(int source, int destination) {
memset(ok, false, sizeof(ok));
ok[source] = true;
Queue.push(source);
while (!Queue.empty()) {
int node = Queue.front();
Queue.pop();
if (node == destination)
continue;
for (vint it = Graph[node].begin(); it != Graph[node].end(); ++it)
if (!ok[*it] && edgeCapacity[node][*it] > edgeFlow[node][*it]) {
ok[*it] = true;
Queue.push(*it);
nodeParent[*it] = node;
}
}
return ok[destination];
}
int getMaxFlow() {
int flowMax = 0;
while ( bfs(1, nodeCount) ) {
for (vint it = Graph[nodeCount].begin(); it != Graph[nodeCount].end(); ++it) {
if (edgeCapacity[*it][nodeCount] <= edgeFlow[*it][nodeCount])
continue;
int flowToRaise = INF;
nodeParent[nodeCount] = *it;
for (int i = nodeCount; i != 1; i = nodeParent[i])
flowToRaise = std::min(flowToRaise, edgeCapacity[nodeParent[i]][i] - edgeFlow[nodeParent[i]][i]);
for (int i = nodeCount; i != 1; i = nodeParent[i]) {
edgeFlow[nodeParent[i]][i] += flowToRaise;
edgeFlow[i][nodeParent[i]] -= flowToRaise;
}
flowMax += flowToRaise;
}
}
return flowMax;
}
int main() {
f >> nodeCount >> edgeCount;
for (int i = 1; i <= edgeCount; ++i) {
int source, destination, curentEdgeCapacity;
f >> source >> destination >> curentEdgeCapacity;
Graph[source].push_back(destination);
Graph[destination].push_back(source);
edgeCapacity[source][destination] = curentEdgeCapacity;
}
g << getMaxFlow();
return 0;
}
//Trust me, I'm the Doctor!