Pagini recente » Cod sursa (job #3217552) | Cod sursa (job #190263) | Cod sursa (job #289031) | Cod sursa (job #2888841) | Cod sursa (job #2702112)
#include <bits/stdc++.h>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
#define int long long
const int Max = 1e3 + 1;
void nos()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
int n,m;
int maxflow;
int cost[Max][Max];
vector < int > v[Max];
int viz[Max];
int parent[Max];
map < int , bool > mp;
void baga(int leaf);
bool bfs()
{
int bagat[Max]={};
int node = 1;
int i;
for(i=1;i<=n;i++)
viz[i] = bagat[i] = 0;
queue < int > q;
q.push(node);
viz[node] = 1;
parent[node] = -1;
while(!q.empty())
{
int nod = q.front();
q.pop();
auto it = mp.find(nod);
if(it!=mp.end() and cost[nod][n] > 0 and bagat[nod] ==0)
baga(nod),bagat[nod]=1;
for(auto vec : v[nod])
if(viz[vec]==0 and cost[nod][vec] > 0)
{
q.push(vec);
parent[vec] = nod;
viz[vec] = 1;
}
}
return viz[n] == 1;
}
int my_leafs[Max];
void baga(int leaf)
{
//cout<<"apel";
int path_flow = cost[leaf][n];
for(int nod = leaf; nod != 1;nod = parent[nod])
path_flow = min(path_flow,cost[parent[nod]][nod]);
cost[leaf][n] -= path_flow;
cost[n][leaf] += path_flow;
for(int nod = leaf; nod != 1;nod = parent[nod])
{
cost[parent[nod]][nod] -= path_flow;
cost[nod][parent[nod]] += path_flow;
}
maxflow += path_flow;
}
void read()
{
f>>n>>m;
int i;
for(i=1;i<=m;i++)
{
int x,y,val;
f>>x>>y>>val;
cost[x][y] = val;
v[x].push_back(y);
v[y].push_back(x);
if(y == n)
mp.insert(pair < int , bool > (x,true));
}
}
void apa_face_pic_pic()
{
while(bfs())
{
}
}
void solve()
{
apa_face_pic_pic();
g<<maxflow;
}
void restart()
{
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cost[i][j] = 0;
}
int32_t main()
{
// nos();
read();
solve();
restart();
return 0;
}