#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
struct node{
int to,next,flow;
}e[2010];
int head[1010],cnt,S,T,d[1010],cur[1010],n,m;
void add_edge(int from,int to,int flow){
e[cnt].to = to;
e[cnt].flow = flow;
e[cnt].next = head[from];
head[from] = cnt++;
e[cnt].to = from;
e[cnt].flow = 0;
e[cnt].next = head[to];
head[to] = cnt++;
}
int BFS(){
memset(d,0,sizeof(d));
d[S] = 1;
queue<int>q;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
for( int i = head[u]; i != -1; i = e[i].next){
int to = e[i].to;
if(!d[to] && e[i].flow){
d[to] = d[u] + 1;
q.push(to);
if(to == T) return 1;
}
}
}
return 0;
}
int dfs(int u,int flow){
int cost = 0;
if(u == T) return flow;
for(int &i = cur[u]; i!= -1; i = e[i].next){
int to = e[i].to;
if(d[to] == d[u] + 1 && e[i].flow){
int t = dfs(to,min(flow - cost,e[i].flow));
if(t){
e[i].flow -= t;
e[i^1].flow += t;
cost += t;
if(flow == cost) break;
}
}
}
return cost;
}
int Dinic(){
int res = 0;
while(BFS()){
for(int i = 0; i <= n; i++)
cur[i] = head[i];
res += dfs(S,INF);
}
return res;
}
int main(){
int t,a,b,c;
scanf("%d",&t);
while(t--){
memset(head,-1,sizeof(head));
cnt = 0;
scanf("%d%d",&n,&m);
scanf("%d%d",&S,&T);
for(int i = 0; i < m; i++){
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c * (m + 1) + 1);
}
printf("%d\n",Dinic()%(m+1));
}
}
求边数最少的最小割
最新推荐文章于 2021-11-09 17:04:19 发布