这个题就是求最小割边数目,太菜了,这个题交的时候没法看是wa还是TLE,结果连交三发,全都是红,我有些怀疑TLE,但是又不敢肯定,(MDZZ,200点,1000边稠密图还用dinic),当时也蒙了,我就盯着每秒刷新看了11页,看的眼睛都花了,终于找到tle。果断上SAP,终于是过了。MDZZ,这周智障的格外厉害。
关于最小割边的边数量,我们用E代表最大边数量。
每条边的流量记为w,我们存图时,存入的为(w*(E+1)+1)
为什么呢?
我们可以把最后一个1看成每条边的标记,这标记最后取,这样只有着天边全取时才会把这个1加入到最大流中,代表这条增广路中限制流量的那条边取到了,而这条边恰恰是最小割的组成部分。
这样最大流为ans/(E+1),最小割边数量为ans%(E+1)
code:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string>
#include <set>
#include<time.h>
//a&3==a%4
using namespace std;
#define ll long long
#define intt long long
#define mem(a) memset(a,0,sizeof(a))
const double eps=1e-8;
const int MAXN = 1010;//点数的最大值+10
const int MAXM = 10010;//边数的最大值+10
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow;
}edge[MAXM];
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN];
void init()
{
tol = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int rw = 0)
{
edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = 0;
edge[tol].next = head[u]; head[u] = tol++;
edge[tol].to = u; edge[tol].cap = rw; edge[tol].flow = 0;
edge[tol].next = head[v]; head[v] = tol++;
}
int Q[MAXN];
void BFS(int start,int end)
{
memset(dep,-1,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0] = 1;
int front = 0, rear = 0;
dep[end] = 0;
Q[rear++] = end;
while(front != rear)
{
int u = Q[front++];
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(dep[v] != -1)continue;
Q[rear++] = v;
dep[v] = dep[u] + 1;
gap[dep[v]]++;
}
}
}
int S[MAXN];
ll sap(int start,int end,int N)
{
BFS(start,end);
memcpy(cur,head,sizeof(head));
int top = 0;
int u = start;
ll ans = 0;
while(dep[start] < N)
{
if(u == end)
{
int Min = INF;
int inser;
for(int i = 0;i < top;i++)
if(Min > edge[S[i]].cap - edge[S[i]].flow)
{
Min = edge[S[i]].cap - edge[S[i]].flow;
inser = i;
}
for(int i = 0;i < top;i++)
{
edge[S[i]].flow += Min;
edge[S[i]^1].flow -= Min;
}
ans += (ll)(Min);
top = inser;
u = edge[S[top]^1].to;
continue;
}
bool flag = false;
int v;
for(int i = cur[u]; i != -1; i = edge[i].next)
{
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
{
flag = true;
cur[u] = i;
break;
}
}
if(flag)
{
S[top++] = cur[u];
u = v;
continue;
}
int Min = N;
for(int i = head[u]; i != -1; i = edge[i].next)
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
{
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if(!gap[dep[u]])return ans;
dep[u] = Min + 1;
gap[dep[u]]++;
if(u != start)u = edge[S[--top]^1].to;
}
return ans;
}
int main()
{
int kase;
scanf("%d",&kase);
while(kase--)
{
init();
int n,m,s,t;
cin>>n>>m>>s>>t;
for(int i=0;i<m;i++)
{
int u,v,w;
cin>>u>>v>>w;
addedge(u,v,w*MAXM+1);
}
ll res=sap(s,t,n);
res=res%(ll)(MAXM);
cout<<res<<endl;
}
return 0;
}