Pagini recente » Cod sursa (job #475053) | Cod sursa (job #1563566) | Cod sursa (job #385958) | Cod sursa (job #2912161) | Cod sursa (job #1750893)
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
vector<int> graph[1010];
int parent[1010];
queue<int> q;
bool vis[1010];
int cap[1010][1010];
int rez[1010][1010];
int n, m;
int x, y, c;
int max_flow;
bool BFS()
{
memset(vis, 0, sizeof(vis));
vis[1] = true;
q.push(1);
while(!q.empty()){
int node = q.front();
for(int i=0; i<graph[node].size(); i++){
if(!vis[graph[node][i]] && cap[node][graph[node][i]] != rez[node][graph[node][i]]){
vis[graph[node][i]] = true;
parent[graph[node][i]] = node;
if(graph[node][i] != n)
q.push(graph[node][i]);
}
}
q.pop();
}
return vis[n];
}
int main()
{
in >> n >> m;
for(int i=0; i<m; i++){
in >> x >> y >> c;
cap[x][y] = c;
graph[x].push_back(y);
graph[y].push_back(x);
}
while(BFS()){
for(int i=0; i<graph[n].size(); i++){
int flow = 0xfffff;
int x = graph[n][i];
if(vis[x] && cap[x][n] != rez[x][n]){
parent[n] = x;
for(x=n; x != 1; x = parent[x]){
flow = min(flow, cap[parent[x]][x] - rez[parent[x]][x]);
}
if(!flow) continue;
for(x=n; x != 1; x = parent[x]){
rez[parent[x]][x] += flow;
rez[x][parent[x]] -= flow;
}
max_flow += flow;
}
}
}
out << max_flow << '\n';
return 0;
}