//Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
// same ids are connected.
// prefix 的格式要求是 acm-[一级标题编号].[二级标题编号][该代码片段的英文简称]
// description 的格式要求是 "插入 [该代码片段的中文名]"

{
    "深海机器人问题  (EK)": {
        "prefix": "acm-3.5MCMF",
        "body": [
            "#include<algorithm>",
            "#include<cstdio>",
            "#include<queue>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=260,M=1540,inf=2e9;",
            "int x,y,z,o=1,n,m,h,t,Q1,Q2,st,ed,cyf[N],pan[N],pre[N],dis[N],head[N];LL maxcost,maxflow; ",
            "struct QAQ{int w,to,next,flow;}a[M<<1];queue<int>Q;",
            "inline void in(Re &x){",
            "    int f=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')f|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=f?-x:x;",
            "}",
            "inline void add(Re x,Re y,Re z,Re w){a[++o].flow=z,a[o].w=w,a[o].to=y,a[o].next=head[x],head[x]=o;}",
            "inline void add_(Re a,Re b,Re flow,Re w){add(a,b,flow,w),add(b,a,0,-w);}",
            "inline int SPFA(Re st,Re ed){",
            "\tfor(Re i=0;i<=ed;++i)dis[i]=-inf,pan[i]=0;",
            "\tQ.push(st),pan[st]=1,dis[st]=0,cyf[st]=inf;",
            "\twhile(!Q.empty()){",
            "\t\tRe x=Q.front();Q.pop();pan[x]=0;",
            "\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\tif(a[i].flow&&dis[to=a[i].to]<dis[x]+a[i].w){",
            "\t\t\t\tdis[to]=dis[x]+a[i].w,pre[to]=i;",
            "\t\t\t\tcyf[to]=min(cyf[x],a[i].flow);",
            "\t\t\t\tif(!pan[to])pan[to]=1,Q.push(to);",
            "\t\t\t}",
            "\t}",
            "\treturn dis[ed]!=-inf;",
            "}",
            "inline void EK(Re st,Re ed){",
            "\twhile(SPFA(st,ed)){",
            "\t\tRe x=ed;maxflow+=cyf[ed],maxcost+=(LL)cyf[ed]*dis[ed];",
            "\t\twhile(x!=st){",
            "\t\t\tRe i=pre[x];",
            "\t\t\ta[i].flow-=cyf[ed];",
            "\t\t\ta[i^1].flow+=cyf[ed];",
            "\t\t\tx=a[i^1].to;",
            "\t\t}",
            "\t}",
            "}",
            "inline int Poi(Re x,Re y){return (x-1)*m+y;}",
            "int main(){",
            "\tin(Q1),in(Q2),in(n),in(m),++n,++m,st=n*m+1,ed=st+1;",
            "\tfor(Re i=1;i<=n;++i)",
            "\t\tfor(Re j=1;j<m;++j)",
            "\t\t\tin(x),add_(Poi(i,j),Poi(i,j+1),1,x),add_(Poi(i,j),Poi(i,j+1),inf,0);",
            "\tfor(Re j=1;j<=m;++j)",
            "\t\tfor(Re i=1;i<n;++i)",
            "\t\t\tin(x),add_(Poi(i,j),Poi(i+1,j),1,x),add_(Poi(i,j),Poi(i+1,j),inf,0);",
            "\twhile(Q1--)in(z),in(x),in(y),add_(st,Poi(x+1,y+1),z,0);",
            "\twhile(Q2--)in(z),in(x),in(y),add_(Poi(x+1,y+1),ed,z,0);",
            "\tEK(st,ed);",
            "\tprintf(\"%lld\",maxcost);",
            "}",
            ""
        ],
        "description": "插入 深海机器人问题  (EK)"
    }
}