#include"iostream" //3549
#include"algorithm"
#include"string"
#include"vector"
#include"cstdio"
#include"cstring"
#include"queue"
using namespace std;
const int maxn=100;
const int inf=200000;
struct edge{
int from,to,cap,flow;
edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}
};
struct EdmondsKarp{
vector<edge>edges;
vector<int>g[maxn];
int n,m;
int a[maxn];
int p[maxn]; //记录上一条弧
void init(int n){
for(int i=0;i<n;i++)
g[i].clear();
edges.clear();
}
void add(int from,int to,int cap){
edges.push_back(edge(from,to,cap,0));
edges.push_back(edge(to,from,0,0));
m=edges.size();
g[from].push_back(m-2);
g[to].push_back(m-1);
}
int main(int s,int t)
{
int flow=0;
for(;;)
{
memset(a,0,sizeof(a));
queue<int>Q;
Q.push(s);
a[s]=inf;
#pragma region 主循环
while(!Q.empty())
{
int x=Q.front();Q.pop();
for(int i=0;i<g[x].size();i++){
edge &e = edges[g[x][i]];
if(!a[e.to]&&e.cap>e.flow){
p[e.to]=g[x][i];
a[e.to]=min(a[x],e.cap-e.flow);
Q.push(e.to);
}
}
if(a[t]) break;
}
#pragma endregion
if(!a[t]) break;
for(int u=t;u!=s;u=edges[p[u]].from){
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
}
flow+=a[t];
}
return flow;
}
};
int main()
{
//freopen("a.txt","r",stdin);
int Case;
scanf("%d",&Case);
for(int i=1;i<=Case;i++)
{
EdmondsKarp a;
int n,m;
scanf("%d%d",&n,&m);
a.init(n);
for(int j=0;j<m;j++)
{
int from,to,cap;
scanf("%d%d%d",&from,&to,&cap);
a.add(from-1,to-1,cap);
}
printf("Case %d: %d\n",i,a.main(0,n-1));
}
return 0;
}
增广路算法 EdmondsKarp
最新推荐文章于 2024-07-14 12:17:20 发布