Pagini recente » Cod sursa (job #2050428) | Cod sursa (job #1292268) | Cod sursa (job #2050529) | Cod sursa (job #345727) | Cod sursa (job #1065809)
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
const int MAXN=1005;
const int INF=(1<<30)-1;
int n,m,lg,MAX_FLOW;
int mark[MAXN],capacity[MAXN][MAXN],flow[MAXN][MAXN];
vector<int> g[MAXN];
queue<int> q;
void read()
{
int x,y,c;
freopen("maxflow.in","r",stdin);
scanf("%d%d",&n,&m);
for (int i=1; i<=m; ++i)
{
scanf("%d%d%d",&x,&y,&c);
g[x].push_back(y);
g[y].push_back(x);
capacity[x][y]=c;
}
}
void write()
{
freopen("maxflow.out","w",stdout);
printf("%d\n",MAX_FLOW);
}
bool bfs()
{
int i,u,v;
for (i=1; i<=n; mark[i]=0, ++i);
q.push(1);
while (!q.empty())
{
u=q.front();
for (vector<int>::iterator it=g[u].begin(); it!=g[u].end(); ++it)
{
v=*it;
if (!mark[v] && flow[u][v]<capacity[u][v])
{
mark[v]=u;
q.push(v);
}
}
q.pop();
}
return mark[n];
}
void ek()
{
int u,v,increment=0;
while (bfs())
{
increment=INF;
for (v=n; v!=1; v=mark[v])
{
increment=min(increment,capacity[mark[v]][v]-flow[mark[v]][v]);
}
if (increment==INF)
return;
for (v=n; v!=1; v=mark[v])
{
flow[mark[v]][v]+=increment;
flow[v][mark[v]]-=increment;
}
MAX_FLOW+=increment;
}
}
int main()
{
read();
ek();
write();
return 0;
}