求大佬帮忙优化, 1.7s全TLE
查看原帖
求大佬帮忙优化, 1.7s全TLE
649246
nothing__楼主2022/7/15 12:58
#include<bits/stdc++.h>
using namespace std;
struct edge{int x, y, c, pre, other;}a[4100000];int alen, last[210000], h[21000], st, ed;
void  ins(int x, int y, int c)
{
	a[++alen]=edge{x, y, c, last[x], alen+1};last[x]=alen;
	a[++alen]=edge{y, x, 0, last[y], alen-1};last[y]=alen;
}
int List[21000],head,tail;
bool bh()
{
    memset(h,0,sizeof(h));h[st]=1;
    List[1]=st;head=1;tail=2;
    while(head!=tail)
    {
        int x=List[head];
        for(int k=last[x];k>0;k=a[k].pre)
        {
            int y=a[k].y;
            if(h[y]==0&&a[k].c>0)
            {
                h[y]=h[x]+1;
                List[tail++]=y;
            }
        }
        head++;
    }
    return h[ed]>0;
}
int findflow(int x, int f)
{
	if(x==ed) return f; 
	int sx=0;
	for(int k=last[x];k>0;k=a[k].pre)
	{
		int y=a[k].y;
		if(a[k].c>0&&sx<f&&h[y]==(h[x]+1)) 
		{
			int sy=findflow(y, min(a[k].c, f-sx));
			a[k].c-=sy; a[a[k].other].c+=sy;
			sx+=sy;
			if(sx==f) return f;
		}
	}
	if(sx==0) h[x]=0;
	return sx;
}
int main()
{
	int n, m;
	scanf("%d%d%d%d", &n, &m, &st, &ed);
	for(int i=1;i<=m;i++)
	{
		int x, y, c;
		scanf("%d%d%d", &x, &y, &c);
		ins(x, y, c);
	}
	long long s=0;
	while(bh()) 
	{
		s+=findflow(st, 0x15f);
	}
	printf("%d\n", s);
	return 0;
}
2022/7/15 12:58
加载中...