Flow Problem
思路:裸的网络流,第一次写。M忘记开2倍了,因为要开反向边
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+5;
const int M=1e5+5;
const int MOD=1e9+7;
const int inf=1<<30;
template <class T>
bool sf(T &ret){ //Faster Input
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
int ver[M],edge[M],Next[M],head[N],d[N];
int s,t,ks;
int tot;
int maxflow;
void add(int x,int y,int z){
ver[++tot]=y,edge[tot]=z,Next[tot]=head[x],head[x]=tot;
ver[++tot]=x,edge[tot]=0,Next[tot]=head[y],head[y]=tot;
}
void init(){
tot=1;
maxflow=0;
memset(ver,0,sizeof ver);
memset(edge,0,sizeof edge);
memset(Next,0,sizeof Next);
memset(head,-1,sizeof head);
}
bool bfs(){
memset(d,0,sizeof d);
queue<int> q;
q.push(s);
d[s]=1;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=head[x];i!=-1;i=Next[i]){
if(edge[i] && !d[ver[i]]){
q.push(ver[i]);
d[ver[i]]=d[x]+1;
if(ver[i]==t) return true;
}
}
}
return false;
}
int dinic(int x,int flow){
if(x==t) return flow;
int res=0;
for(int i=head[x];i!=-1;i=Next[i]){
if(edge[i]&&d[ver[i]]==d[x]+1){
int k=dinic(ver[i],min(flow-res,edge[i]));
if(!k) d[ver[i]]=0;
edge[i]-=k;
edge[i^1]+=k;
// cout <<i <<" and "<<(1^i)<<endl;
res+=k;
}
}
return res;
}
void solve(){
int n,m;
sf(n);sf(m);
init();
s=1,t=n;
for(int i=1;i<=m;i++){
int u,v,w;
sf(u),sf(v),sf(w);
add(u,v,w);
}
int flow;
while(bfs())
while(flow=dinic(s,inf)) maxflow+=flow;
printf("Case %d: %d\n",++ks,maxflow);
}
int main(void){
int T;
sf(T);
while(T--){
solve();
}
return 0;
}