Pagini recente » Cod sursa (job #1629453) | Cod sursa (job #2946197) | Cod sursa (job #926202) | Cod sursa (job #2138050) | Cod sursa (job #1250215)
#include <fstream>
#include <vector>
#include <queue>
#define Nmax 1010
#define oo (1 << 30)
#define Neighbour Graph[Node][i]
#define lastFather Graph[Destination][i]
using namespace std;
vector <int> Graph[Nmax];
int paint, N, Source, Destination, totalFlow, used[Nmax], Father[Nmax], Flow[Nmax][Nmax];
bool Bfs() {
int i, Node;
queue <int> Queue;
Queue.push(Source);
used[Source] = ++paint;
while(!Queue.empty()) {
Node = Queue.front();
Queue.pop();
for(int i = 0; i < Graph[Node].size(); i++)
if(used[Neighbour] != paint && Flow[Node][Neighbour] > 0) {
Queue.push(Neighbour);
used[Neighbour] = paint;
Father[Neighbour] = Node;
if(Neighbour == Destination)
return true;
}
}
return false;
}
void Solve() {
int i, minFlow, Node;
Source = 1;
Destination = N;
while(Bfs())
for(i = 0; i < Graph[Destination].size(); i++)
if(used[lastFather] == paint && Flow[lastFather][Destination] > 0) {
minFlow = oo;
Father[Destination] = lastFather;
for(Node = Destination; Node != Source; Node = Father[Node])
minFlow = min(minFlow, Flow[Father[Node]][Node]);
if(minFlow > 0)
for(Node = Destination; Node != Source; Node = Father[Node]) {
Flow[Node][Father[Node]] += minFlow;
Flow[Father[Node]][Node] -= minFlow;
}
totalFlow += minFlow;
}
}
void Read() {
int x, y, flow, M;
ifstream in("maxflow.in");
in >> N >> M;
while(M--) {
in >> x >> y >> flow;
Flow[x][y] = flow;
Graph[x].push_back(y);
Graph[y].push_back(x);
}
in.close();
}
void Write() {
ofstream out("maxflow.out");
out << totalFlow << '\n';
out.close();
}
int main() {
Read();
Solve();
Write();
return 0;
}