Pagini recente » Monitorul de evaluare | Borderou de evaluare (job #441368) | Borderou de evaluare (job #1269982) | Rezultatele filtrării | Cod sursa (job #1167004)
#include<cstdio>
#include<vector>
#include<bitset>
#include<deque>
using namespace std;
void Read(),Solve(),Print();
typedef long long lld;
const int NMAX = 1000+5;
const int INF = (1<<30);
int N,M,Source,Sink,MaxFlow;
int Cap[NMAX][NMAX];
int Flow[NMAX][NMAX];
int Father[NMAX];
vector<int> V[NMAX];
bitset<NMAX> Viz;
deque<int> Q;
int BFS()
{
vector<int>::iterator it;
int x;
Q.clear();
Viz=0;
Q.push_back(Source);
Viz[Source] = 1;
while(!Q.empty())
{
x = Q.front();
Q.pop_front();
if(x == Sink) break;
for(it = V[x].begin(); it != V[x].end(); it++)
{
if(Viz[*it] || Flow[x][*it] >= Cap[x][*it]) continue;
Father[*it] = x;
Q.push_back(*it);
Viz[*it] = 1;
}
}
return Viz[Sink];
}
int main()
{
Read();
Solve();
Print();
return 0;
}
void Read()
{
int x,y,c;
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%d%d",&N,&M);
Source = 1;
Sink = N;
for(; M; --M)
{
scanf("%d%d%d",&x,&y,&c);
V[x].push_back(y);
V[y].push_back(x);
Cap[x][y] = c;
}
}
void Solve()
{
vector<int>::iterator it;
int flow,x;
while(BFS())
{
for(it = V[Sink].begin(); it != V[Sink].end(); it++)
{
if(!Viz[*it] || Flow[*it][Sink] >= Cap[*it][Sink]) continue;
Father[Sink] = *it;
flow = INF;
for(x = Sink; x != Source; x = Father[x])
flow = min(flow, Cap[Father[x]][x] - Flow[Father[x]][x]);
for(x = Sink; x != Source; x = Father[x])
Flow[Father[x]][x] += flow,
Flow[x][Father[x]] -= flow;
MaxFlow += flow;
}
}
}
void Print()
{
printf("%d\n",MaxFlow);
}