不能再水的题了。
Dinic算法,比EK更快。
想要学习请看链接 https://comzyh.com/blog/archives/568/
并附上我的模板(其实和comzyh大神的一样)
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int tab[203][203],dis[203],d[203],n,m; 6 bool bfs() 7 { 8 int i,head=0,tail=0; 9 memset(d,0,sizeof(d)); 10 memset(dis,0xff,sizeof(dis)); 11 d[0]=1; dis[1]=0; 12 while (head<=tail) 13 { 14 for (i=1;i<=n;++i) 15 if ((dis[i]<0)&&(tab[d[head]][i]>0)) 16 { 17 dis[i]=dis[d[head]]+1; 18 tail++; d[tail]=i; 19 } 20 head++; 21 } 22 if (dis[n]>0) return 1; 23 else return 0; 24 } 25 int wll(int x,int low) 26 { 27 int i,a=0; if (x==n) return low; 28 for (i=1;i<=n;++i) 29 if ((tab[x][i]>0)&&(dis[i]==dis[x]+1)&&(a=wll(i,min(low,tab[x][i])))) 30 { 31 tab[x][i]-=a; 32 tab[i][x]+=a; 33 return a; 34 } 35 return 0; 36 } 37 int main() 38 { 39 scanf("%d %d ",&m,&n); 40 int i,j,u,v,flow,ans=0,at; 41 memset(tab,0,sizeof(tab)); 42 for (i=1;i<=m;++i) 43 { 44 scanf("%d %d %d ",&u,&v,&flow); 45 tab[u][v]+=flow; 46 } 47 while (bfs()) 48 { 49 while (at=wll(1,0x7fffffff)) ans+=at; 50 } 51 printf("%d ",ans); 52 }