裸的网络流,递归的dinic会爆栈,在第一行加一句就行了
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <algorithm>
#include <vector>
#define Maxn 120010
#define Maxm 210000
#define LL int
#define inf 100000000
#define Abs(a) (a)>0?(a):(-a)
using namespace std;
struct Edge{
int from,to,next;
LL val;
}edge[Maxm];
const double eps=1e-;
LL value[Maxn];
int head[Maxn],work[Maxn],dis[Maxn],q[Maxn],e,vi[Maxn];
void init()
{
e=;
memset(head,-,sizeof(head));
}
void add(int u,int v,LL c)//有向边
{
edge[e].to=v;edge[e].val=c;edge[e].next=head[u];head[u]=e++;
edge[e].to=u;edge[e].val=c;edge[e].next=head[v];head[v]=e++;
}
int bfs(int S,int T)
{
int rear=;
memset(dis,-,sizeof(dis));
dis[S]=;q[rear++]=S;
for(int i=;i<rear;i++)
{
for(int j=head[q[i]];j!=-;j=edge[j].next)
{
if(edge[j].val&&dis[edge[j].to]==-)
{
dis[edge[j].to]=dis[q[i]]+;
q[rear++]=edge[j].to;
if(edge[j].to==T) return ;
}
}
}
return ;
}
LL dfs(int cur,LL a,int T)
{
if(cur==T) return a;
for(int &i=work[cur];i!=-;i=edge[i].next)
{
if(edge[i].val&&dis[edge[i].to]==dis[cur]+)
{
LL t=dfs(edge[i].to,min(a,edge[i].val),T);
if(t)
{
edge[i].val-=t;
edge[i^].val+=t;
return t;
}
}
}
return ;
}
LL Dinic(int S,int T)
{
LL ans=;
while(bfs(S,T))
{
memcpy(work,head,sizeof(head));
while(LL t=dfs(S,inf,T)) ans+=t;
}
return ans;
}
int main()
{
int n,m,i,j,num=,t,a,b,west=,east=-,S,T;
LL c;
scanf("%d",&t);
while(t--)
{
init();
west=,east=-;
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
{
scanf("%d%d",&a,&b);
if(a<west)
west=a,S=i;
if(a>east)
east=a,T=i;
}
for(i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
LL ans=Dinic(S,T);
printf("%d\n",ans);
}
return ;
}