const int N =5005, M=1e5+100; #define inf 1e9 int all=1,hd[N],go[M],w[M],nxt[M],cost[M]; int S,T,n,m; int pre[N],mn[N],dis[N],vis[N],ans=0; void add_(int x,int y,int z,int c){ nxt[++all]=hd[x]; hd[x]=all; go[all]=y; w[all]=z; cost[all]=c; swap(x,y); nxt[++all]=hd[x]; hd[x]=all; go[all]=y; w[all]=0; cost[all]= -c; } bool spfa(){ int i; queue<int> q; q.push(S); for(i=0;i<N;i++) dis[i]=inf; for(i=1;i<N;i++) vis[i]=0; mn[S]=inf, dis[S]=0, vis[S]=1; while(!q.empty()){ int x=q.front(); vis[x]=0; q.pop(); for(i=hd[x];i;i=nxt[i]){ int y=go[i],z=w[i],c=cost[i]; if(w[i]==0) continue; if(dis[x]+c<dis[y]){ dis[y]=dis[x]+c; mn[y]=min(mn[x],z); pre[y]=i; if(vis[y]==0) vis[y]=1,q.push(y); } } } if(dis[T]==inf) return 0; return 1; } void solve(){ int maxf=0, minc=0; while(spfa()){ int x= T; maxf+= mn[T] ,minc+= mn[T]*dis[T]; while(x!=S){ int i=pre[x]; w[i]-= mn[T]; w[i^1]+=mn[T]; x=go[i^1]; } } cout<<maxf<<' '<<minc<<endl; }
标签:费用,nxt,int,最小,spfa,cost,go,hd From: https://www.cnblogs.com/towboa/p/17253534.html