Pagini recente » Cod sursa (job #2398117) | Cod sursa (job #2549808) | Cod sursa (job #1182531) | Cod sursa (job #643380) | Cod sursa (job #2913519)
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
struct arc
{
int neig, cap, flow, oppo;
};
vector <arc> v[1005];
int que[1005], parent[1005], vis[1005], first, last, OK, n, m;
int update()
{
int node = n;
int flow = 999999;
while(node != 1)
{
int pi = parent[node];
int port = vis[node]-1;
if(flow > v[pi][port].cap - v[pi][port].flow)
flow = v[pi][port].cap - v[pi][port].flow;
node = pi;
}
node = n;
while(node != 1)
{
int pi = parent[node];
int port = vis[node]-1;
v[pi][port].flow += flow;
int opposite = v[pi][port].oppo;
v[node][opposite].flow -= flow;
node = pi;
}
return flow;
}
int bfs()
{
while(first<last)
{
int node = que[first];
for(int i=0; i<v[node].size(); i++)
{
int neighbour = v[node][i].neig;
if(!vis[neighbour] && v[node][i].cap > v[node][i].flow)
{
vis[neighbour] = i+1;
que[last++] = neighbour;
parent[neighbour] = node;
if(neighbour == n)
{
OK = 1;
return update();
}
}
}
first++;
}
return 0;
}
int main()
{
int i, x, y, c, MaxFlow=0;
fin>>n>>m;
for(i=1; i<=m; i++)
{
fin>>x>>y>>c;
v[x].push_back({y,c,0,v[y].size()});
v[y].push_back({x,0,0,v[x].size()-1});
}
OK = 1;
while(OK)
{
OK = first = last = 0;
que[last++] = 1;
MaxFlow += bfs();
memset(vis, 0, sizeof(vis));
}
fout<<MaxFlow;
return 0;
}