Pagini recente » Cod sursa (job #1130343) | Cod sursa (job #3163792) | Cod sursa (job #630742) | Cod sursa (job #357600) | Cod sursa (job #2811339)
#include <iostream>
#include <bits/stdc++.h>
#define maxFlow 110000
using namespace std;
ifstream in ("maxflow.in");
ofstream out ("maxflow.out");
int n, m;
int s, f;
vector<int> la[1001];
int fluxLeft[1001][1001];
int parent[1001];
int flux = 0;
void getPath()
{
memset(parent, 0, sizeof(parent));
vector<int> q;
parent[s] = -1;
q.push_back(s);
for (int i = 0; i < q.size() ; i++)
{
if (q[i] == n)
{
continue;
}
for (auto to: la[q[i]])
{
if (fluxLeft[q[i]][to] <= 0 || parent[to] != 0)
continue;
parent[to] = q[i];
q.push_back(to);
}
}
}
int main()
{
in >> n >> m;
s = 1; f = n;
for (int i = 1; i <= m ; ++i)
{
int x,y,c;
in >> x >> y >> c;
la[x].push_back(y);
la[y].push_back(x);
fluxLeft[x][y] = c;
}
getPath();
while (parent[f] != 0)
{
for (auto& antpen: la[f])
{
parent[f] = antpen;
int minn = maxFlow;
int nod = f;
while (parent[nod] != -1)
{
if (fluxLeft[parent[nod]][nod] < minn)
minn = fluxLeft[parent[nod]][nod];
nod = parent[nod];
}
if (minn <= 0)
continue;
flux += minn;
nod = f;
while (parent[nod] != -1)
{
fluxLeft[parent[nod]][nod] -= minn;
fluxLeft[nod][parent[nod]] += minn;
nod = parent[nod];
}
}
getPath();
}
g << flux;
return 0;
}
/*
6 8
1 2 10
1 3 8
2 3 2
2 4 5
3 5 10
5 4 8
4 6 7
5 6 10
// 15
*/
/*
6 7
1 2 6
1 3 8
2 4 5
3 5 4
2 5 3
4 6 7
5 6 5
// 10
*/