Pagini recente » Cod sursa (job #2979717) | Cod sursa (job #2889290) | Cod sursa (job #3277460) | Cod sursa (job #2661584) | Cod sursa (job #2957151)
#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 dest;
int n,m;
int maxflow;
int cost[Max][Max];
vector < int > v[Max];
int viz[Max];
int parent[Max];
set < int > 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)
{
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;
}
struct muchie {
int x,y,val;
};
vector < muchie > graf;
void add_edge(muchie m)
{
int x = m.x;
int y = m.y;
int val = m.val;
cost[x][y] = val;
v[x].push_back(y);
v[y].push_back(x);
if(y == dest)
mp.insert(x);
}
void read()
{
f>>n>>m;
int i;
for(i=1;i<=m;i++)
{
int x,y,val;
f>>x>>y>>val;
graf.push_back({x,y,val});
}
}
void solve()
{
dest = n;
for(auto it : graf)
add_edge(it);
// edmond karp
while(bfs());
g<<maxflow;
// taietura minima
}
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;
}