Pagini recente » Cod sursa (job #190012) | Cod sursa (job #599096) | Cod sursa (job #110562) | Cod sursa (job #798339) | Cod sursa (job #3328583)
#include <bits/stdc++.h>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
const int Nmax =1e3;
int capacitate[Nmax+1][Nmax+1];
int flux[Nmax+1][Nmax+1];
int n,m;
vector<int>G[Nmax+1];
int vis[Nmax+1];
int p[Nmax+1];
int bfs(int s,int d){
for(int i=1;i<=n;++i)
{
vis[i]=0;
p[i]=0;
}
queue<int> q;
q.push(s);
vis[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(auto vecin:G[u]){
if(!vis[vecin]&&capacitate[u][vecin]-flux[u][vecin]>0)
{
vis[vecin]=1;
p[vecin]=u;
q.push(vecin);
}
}
}
if(!vis[d])
{
return 0;
}
vector<int> path;
int x=d;
while(x!=0)
{
path.push_back(x);
x=p[x];
}
reverse(path.begin(),path.end());
int flow=1e9;
for(int i=0;i<path.size()-1;++i)
{
int a=path[i];
int b=path[i+1];
flow=min(flow,capacitate[a][b]-flux[a][b]);
}
for(int i=0;i<path.size()-1;++i)
{
int a=path[i];
int b=path[i+1];
flux[a][b]+=flow;
flux[b][a]-=flow;
}
return flow;
}
int main() {
f>>n>>m;
for(int i=1;i<=m;i++){
int x,y,c;
f>>x>>y>>c;
capacitate[x][y]=c;
G[x].push_back(y);
G[y].push_back(x);
}
int maxflow=0;
while(1)
{
int flow=bfs(1,n);
if(flow==0)
{
break;
}
maxflow+=flow;
}
g<<maxflow;
f.close();
g.close();
return 0;
}