最大流模板题
作为记录最大流
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <stack>
#define lint long long
using namespace std;
map<string ,int>maps;
int road[1005][1005]={0};
int flow[1005][1005]={0};
int pre[1005]={0},res[1005];
int ek(int st,int en,int n)
{
int maxflow =0 ;
queue<int>q;
while(1)
{
memset(res,0,sizeof(res));
res[st]=0x3f3f3f3f;
q.push(st);
while(!q.empty()) //BFS寻找增广路
{
int u=q.front();
q.pop();
for(int v=1;v<=n;v++)
if(!res[v]&&road[u][v]>flow[u][v])
{
res[v]=min(res[u],road[u][v]-flow[u][v]);
//start-v路径上的最小残量
//记录v的父亲,并加入队列中
pre[v]=u;
q.push(v);
}
}
if(res[en]==0) return maxflow;
//无法继续更新最大流量,则当前流已经是最大流
for(int u=en;u!=st;u=pre[u])
{//从汇点往回走
flow[pre[u]][u]+=res[en];//更新正向流
flow[u][pre[u]]-=res[en];//更新反向流
}
maxflow+=res[en];
}
return maxflow;
}
int main()
{
int i,j,k,l,t,m,n;
string a,b;
k=1;
cin>>a;
if(maps[a]==0)
{
maps[a]=k;
k++;
}
cin>>b;
if(maps[b]==0)
{
maps[b]=k;
k++;
}
scanf("%d",&n);
string aa,bb;
for(i=0;i<n;i++)
{
cin>>aa;
if(maps[aa]==0)
{
maps[aa]=k;
k++;
}
cin>>bb;
if(maps[bb]==0)
{
maps[bb]=k;
k++;
}
scanf("%d",&j);
road[maps[aa]][maps[bb]]+=j;
}
printf("%d",ek(maps[a],maps[b],k-1));
return 0;
}