标签:min efi nic pac memset bsp long queue back
注意pos和head都初始化为-1
M要开两倍 因为连双向边
stnm都开局部变量即可
#include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define RI(n) scanf("%d",&(n)) #define RII(n,m) scanf("%d%d",&n,&m) #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k) #define RS(s) scanf("%s",s); #define ll long long #define pb push_back #define REP(i,N) for(int i=0;i<(N);i++) #define CLR(A,v) memset(A,v,sizeof A) ////////////////////////////////// #define inf 0x3f3f3f3f const int N=10000+5; const int M=200000+5; int head[M],pos; struct Edge { int to,nex=-1,v; }edge[M]; void init() { pos=-1; CLR(head,-1); } void add(int a,int b,int c) { edge[++pos].nex=head[a]; head[a]=pos; edge[pos].to=b; edge[pos].v=c; edge[++pos].nex=head[b]; head[b]=pos; edge[pos].to=a; edge[pos].v=0; } int deth[N]; int bfs(int s,int t) { CLR(deth,0); queue<int>q; deth[s]=1; q.push(s); while(!q.empty()) { int u=q.front();q.pop(); for(int i=head[u];i!=-1;i=edge[i].nex) { int v=edge[i].to; if(edge[i].v>0&&deth[v]==0) { deth[v]=deth[u]+1; q.push(v); } } } return deth[t]; } int dfs(int s,int t,int dis) { if(s==t)return dis; for(int i=head[s];i!=-1;i=edge[i].nex) { int v=edge[i].to; if(deth[v]==deth[s]+1&&edge[i].v!=0) { int di=dfs(v,t,min(dis,edge[i].v)); if(di>0) { edge[i].v-=di; edge[i^1].v+=di; return di; } } } return 0; } int dinic(int s,int t) { int ans=0; while(bfs(s,t)) while(int di=dfs(s,t,inf)) ans+=di; return ans; } int main() { int n,m,s,t; RII(n,m); RII(s,t); init(); while(m--) { int a,b,c;RIII(a,b,c); add(a,b,c); } cout<<dinic(s,t); }
标签:min efi nic pac memset bsp long queue back
原文地址:https://www.cnblogs.com/bxd123/p/10815567.html