"Maximum Flow" "Cost flow" bzoj1834 [zjoi2010]network Network expansion

Source: Internet
Author: User

Reference:

maximum stream + expense flow. first ask the maximum flow can be. The second question is "minimum cost maximum flow". by test instructions, this question can be translated into the "residual network" on the previous question, expanding the capacity of some edges so that the maximum flow from the new graph is K. so easy: for the side of the remaining traffic, the cost of passing through them is 0. The "Increase in traffic" variable is: to the residual network on each side of the building a capacity is ∞ cost is the edge of W. This means that from these sides, the cost of each flow is w, which is in line with test instructions. finally build a super source point, from the super source to 1 to build a capacity of k, the cost of 0 of the edge, you can make the minimum cost maximum flow algorithm.
#include <cstdio> #include <algorithm> #include <cstring> #include <queue>using namespace std;# Define MAXN 1011#define maxm 25001#define INF 2147483647int s,t,n,m,a[5001],b[5001],w1,goal;int en,u[maxm],w2[5001],v[ Maxm],first[maxn],next[maxm],cap[maxm],cost[maxm];//next Arraybool Inq[maxn];int d[MAXN]/*spfa*/,p[MAXN]/*spfa*/, a[maxn]/* can improve the amount of */;queue<int>q;void init_mcmf () {memset (first,-1,sizeof (first)); en=0; S=1; T=n;}  void Addedge (const int &AMP;U,CONST int &AMP;V,CONST int &w,const int &c) {u[en]=u; v[en]=v; cap[en]=w; cost[en]=c; Next[en]=first[u]; First[u]=en++;u[en]=v; V[en]=u; Cost[en]=-c; NEXT[EN]=FIRST[V]; first[v]=en++;}    BOOL SPFA (int &flow,int &cost) {memset (d,0x7f,sizeof (d));    memset (inq,0,sizeof (INQ)); d[s]=0; Inq[s]=1; p[s]=0; A[s]=inf;    Q.push (S);        while (!q.empty ()) {int U=q.front (); Q.pop (); inq[u]=0;          for (int i=first[u];i!=-1;i=next[i]) if (Cap[i] && d[v[i]]>d[u]+cost[i]) {    D[v[i]]=d[u]+cost[i];              P[v[i]]=i;              A[v[i]]=min (A[u],cap[i]);            if (!inq[v[i]]) {Q.push (v[i]); inq[v[i]]=1;}    }} if (d[t]>2100000000) return 0; FLOW+=A[T]; COST+=D[T]*A[T];    int u=t;        while (u!=s) {cap[p[u]]-=a[t]; cap[p[u]^1]+=a[t];      U=u[p[u]]; } return 1;}    int Flow,cost;int mincost () {flow=0,cost=0; while (SPFA (Flow,cost));} int main () {scanf ("%d%d%d", &n,&m,&goal), INIT_MCMF (); for (int i=1;i<=m;++i) {scanf ("%d%d%d%d", &a[  I],&b[i],&w1,&w2[i]);  Addedge (a[i],b[i],w1,0); }mincost (); printf ("%d", Flow); for (int i=1;i<=m;++i) Addedge (A[i],b[i],inf,w2[i]); s=0; Addedge (s,1,goal,0); Mincost (); printf ("%d\n", cost); return 0;}

  

"Maximum Flow" "Cost flow" bzoj1834 [zjoi2010]network Network expansion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.