Cod sursa(job #2693821)

Utilizator Danut200333Dumitru Daniel Danut200333 Data 7 ianuarie 2021 10:52:39
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int n,m;
vector<int> graf[1005];
int capacity[1005][1005],flux[1005][1005],t[1005];
bool viz[1005];
bool bfs()
{
	memset(viz,0,sizeof(viz));
	viz[1]=1;
	queue<int> q;
	q.push(1);
	while(!q.empty())
	{
		int node=q.front();
		q.pop();
		if(node==n) continue;
		for(int j=0;j<graf[node].size();j++)
		{
		    int x=graf[node][j];
			if(!viz[x] && capacity[node][x]!=flux[node][x])
			{
				t[x]=node;
				q.push(x);
				viz[x]=1;
			}
		}
	}
	if(viz[n]==0) return 0;
	return 1;
}
int main()
{
	fin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		int x,y,c;
		fin>>x>>y>>c;
		graf[x].push_back(y);
		graf[y].push_back(x);
		capacity[x][y]+=c;
	}
	int ans=0;
	while(bfs())
	{
		for(int j=0;j<graf[n].size();j++)
		{
		    int x=graf[n][j];
			if(viz[x] && capacity[x][n]!=flux[x][n])
			{
				int minn=1e9;
				t[n]=x;
				for(int i=n;i>1;i=t[i])minn=min(minn,capacity[t[i]][i]-flux[t[i]][i]);
				for(int i=n;i>1;i=t[i])
				{
					flux[t[i]][i]+=minn;
					flux[i][t[i]]-=minn;
				}
				ans+=minn;
			}
		}
	}
	fout<<ans;
	return 0;
}