Pagini recente » Cod sursa (job #1809183) | Cod sursa (job #2175257) | Cod sursa (job #2734919) | Cod sursa (job #2403444) | Cod sursa (job #2702098)
#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];
bool bfs()
{
int node = 1;
int i;
for(i=1;i<=n;i++)
viz[i] = 0;
queue < int > q;
q.push(node);
viz[node] = 1;
parent[node] = -1;
while(!q.empty())
{
int nod = q.front();
q.pop();
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;
}
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);
}
}
void apa_face_pic_pic()
{
while(bfs())
{
int path_flow = LLONG_MAX;
for(int nod = n; nod != 1;nod = parent[nod])
path_flow = min(path_flow,cost[parent[nod]][nod]);
for(int nod = n; nod != 1;nod = parent[nod])
{
cost[parent[nod]][nod] -= path_flow;
cost[nod][parent[nod]] += path_flow;
}
maxflow += path_flow;
}
}
void solve()
{
apa_face_pic_pic();
g<<maxflow;
}
void restart()
{
}
int32_t main()
{
nos();
read();
solve();
restart();
return 0;
}