Island Transport
淦,竟然卡dinic
给出IASP的模板:
#include<iostream> #include<algorithm> #include<cstring> #include<queue> using namespace std; typedef long long ll; const int max_n=100050; const int max_m=200050; const int inf=0x3f3f3f3f; struct edge { int to,cap,next; }E[max_m<<2]; int n,m; int head[max_n],cnt=1,gap[max_n],last[max_n],d[max_n],que[max_n],ql,qr; void add(int u,int to,int cap) { E[++cnt].to=to;E[cnt].cap=cap; E[cnt].next=head[u];head[u]=cnt; }int aug(int x,int s,int t,int mi) { if (x==t) return mi; int flow=0; for (int &i=last[x],v=E[i].to;i;i=E[i].next,v=E[i].to) if (d[x]==d[v]+1) { int tmp=aug(v,s,t,min(mi,E[i].cap)); flow+=tmp,mi-=tmp,E[i].cap-=tmp,E[i^1].cap+=tmp; if (!mi) return flow; } if (!(--gap[d[x]])) d[s]=n+1; ++gap[++d[x]],last[x]=head[x]; return flow; } int maxflow(int s,int t) { fill(gap,gap+n+10,0);fill(d,d+n+10,0); ++gap[d[t]=1]; for (int i=1;i<=n;++i) last[i]=head[i]; que[ql=qr=1]=t; while (ql<=qr) { int x=que[ql++]; for (int i=head[x],v=E[i].to;i;i=E[i].next,v=E[i].to) if (!d[v]) ++gap[d[v]=d[x]+1],que[++qr]=v; } int ret=aug(s,s,t,inf); while (d[s]<=n) ret+=aug(s,s,t,inf); return ret; } int xy[max_n][2]; int main(){ int T;scanf("%d",&T); while (T--){ scanf("%d %d",&n,&m); fill(head,head+n+5,0);cnt=1; for (int i=1;i<=n;++i) scanf("%d %d",&xy[i][0],&xy[i][1]); for (int i=1,u,v,c;i<=m;++i){ scanf("%d %d %d",&u,&v,&c); add(u,v,c);add(v,u,c); } int s;int t;int cmp1 = inf;int cmp2 = -inf; for (int i=1;i<=n;++i){ if (xy[i][0]<cmp1){ cmp1=xy[i][0]; s = i; } if (xy[i][0]>cmp2){ cmp2=xy[i][0]; t = i; } }printf("%d\n",maxflow(s,t)); } }
kuangbin题单刷题详解(网络流) 文章被收录于专栏
题单:https://vjudge.net/article/371