Pagini recente » Cod sursa (job #2191304) | Cod sursa (job #269606) | Cod sursa (job #999606) | Cod sursa (job #300679) | Cod sursa (job #1161711)
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
vector<int> V[1001];
queue<int> Q;
int c[1001][1001];
int f[1001][1001];
int t[1001];
bool used[1001];
int flow,n,m;
void compute_flow()
{
int current_flow = INF;
int nod = n;
while(t[nod])
{
current_flow = min(current_flow, c[t[nod]][nod] - f[t[nod]][nod]);
nod = t[nod];
}
if(current_flow == 0)
return;
nod = n;
while(t[nod])
{
f[t[nod]][nod] += current_flow;
f[nod][t[nod]] -= current_flow;
nod = t[nod];
}
flow += current_flow;
}
bool find_path()
{
memset(used,0,sizeof(used));
used[1] = true;
Q.push(1);
while(!Q.empty())
{
int nod = Q.front();
Q.pop();
for(vector<int>::iterator it = V[nod].begin(); it != V[nod].end() && nod != n; it ++)
if(!used[*it] && c[nod][*it] > f[nod][*it])
{
Q.push(*it);
used[*it] = true;
t[*it] = nod;
}
}
return used[n];
}
int main()
{
fin>>n>>m;
int x,y;
for(int i=1;i<=m;i++)
{
fin>>x>>y;
V[x].push_back(y);
V[y].push_back(x);
fin>>c[x][y];
}
while(find_path())
{
for(vector<int>::iterator it = V[n].begin(); it!=V[n].end(); it++)
{
t[n] = *it;
if(used[*it] && c[*it][n] > f[*it][n])
compute_flow();
}
}
fout<<flow<<'\n';
fin.close();
fout.close();
return 0;
}