刚入OI的萌新求助,DinicT爆了

P3376 【模板】网络最大流

_stellar @ 2019-03-24 11:41:05

我哪里写错了吗.. 感觉没任何问题啊

``` #include<bits/stdc++.h> using namespace std; #define N 1000004 int head[N],to[N],nxt[N],dis[N]; int dep[N]; int n,m,s,t; int ans; int cnt=-1; void add(int u,int v,int w){ nxt[++cnt]=head[u]; head[u]=cnt; to[cnt]=v; dis[cnt]=w; } bool bfs(){ queue<int > q; dep[s]=1;q.push(s); while(!q.empty()){ int u=q.front();q.pop(); for(int i=head[u];i!=-1;i=nxt[i]){ int To=to[i]; if(!dep[To]&&dis[i]){ dep[To]=dep[u]+1; q.push(To); } } } return (dep[t]!=0); } int dfs(int step,int ans){ if(step==t) return ans; int flow=0; for(int i=head[step];i!=-1;i=nxt[i]){ int To=to[i]; if(dep[To]==dep[step]+1&&dis[i]){ if(flow=(dfs(To,min(ans,dis[i])))){ dis[i]-=flow; dis[i^1]+=flow; return flow; } } } return 0; } const int INF=0x3f3f3f3f; void dinic(){ ans=0; while(bfs()){ while(int a=dfs(s,INF)) ans+=a; } } int read(){ int x=0,w=0; char ch=0; while(!isdigit(ch)) w|=ch=='-',ch=getchar(); while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return w?-x:x; } int main(){ memset(head,-1,sizeof(head)); n=read(),m=read(),s=read(),t=read(); for(int i=1;i<=m;i++){ int a,b,c; a=read(),b=read(),c=read(); add(a,b,c);add(b,a,0); } dinic(); printf("%d",ans); printf("Time used:%.2f\n",(double)clock()/CLOCKS_PER_SEC); return 0; } ```

by _stellar @ 2019-03-24 11:43:18

调试已删


by PurpleWonder @ 2019-03-24 11:57:47

@MrWangnacl head数组别开太大,因为每次bfs都要memset一整遍


by _stellar @ 2019-03-24 12:07:42

@PurpleWonder 那只和爆不爆空间有关啊,应该没关系吧


by 万弘 @ 2019-04-09 15:33:15

@MrWangnacl 没有当前弧优化会被卡的


by _stellar @ 2019-04-09 18:45:45

@万弘 已经A了,感谢


|