#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
#define inf 0x7fffffff
typedef long long ll;
const int mod=10007;
const int N =222;
struct po {
int b,c;
int nxt;
}e[99999];
int cnt;
int p[N];
int n,m;
int pre[N];
int dis[N];
int gap[N];
int cur[N];
int sap(int s, int t,int n) {
int i,v,u,c;
int maxflow=0;
int aug=-1;
for(i=0;i<=n;i++) cur[i]=p[i];
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
u=pre[s]=s,
gap[0]=n;
while(dis[s]<n) {
loop:
for(int &ee=cur[u];ee!=-1;ee=e[ee].nxt) {
v=e[ee].b;
c=e[ee].c;
if(c>0&&dis[u]==dis[v]+1) {
if(aug==-1||aug>c) aug=c;
pre[v]=u;
u=v;
if(v==t) {
maxflow+=aug;
for(u=pre[u];v!=s;v=u,u=pre[u])
e[ cur[u] ].c-=aug , e[ cur[u]^1 ].c+=aug;
aug=inf;
}
goto loop;
}
}
int mindis = n;
for(i=p[u];i!=-1;i=e[i].nxt) {
v=e[i].b;
if(e[i].c>0&&mindis>dis[v])
cur[u]=i , mindis=dis[v];
}
if((--gap[dis[u]])==0) break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return maxflow;
}
void addedge(int a,int b,int c) {
e[cnt].b=b; e[cnt].c=c; e[cnt].nxt=p[a]; p[a]=cnt++;
e[cnt].b=a; e[cnt].c=0; e[cnt].nxt=p[b]; p[b]=cnt++;
}
void init() {
memset(p,-1,sizeof(p));
cnt=0;
}
int main()
{
int a,b,c;
int i,t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
init();
for(i=1;i<=m;i++) {
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
int ans=sap(1,n,n);
static int p=0;
printf("Case %d: %d\n",++p,ans);
}
}
SAP模板经典
最新推荐文章于 2023-01-08 13:35:10 发布