【最大流】【費用流】bzoj1834 [ZJOI2010]network 網路擴容

來源:互聯網
上載者:User

標籤:

引用題解:

最大流+費用流。第一問最大流即可。第二問為“最小費用最大流”。由題意,這一問的可轉化為在上一問的“殘量網路”上,擴大一些邊的容量,使能從新的圖中的最大流為k。那麼易得:對於還有剩餘流量的邊,走過他們的費用為0。而“增加流量”可變為:對殘留網路上的每一條邊建一條容量是∞費用是w的邊。這表示從這些邊走,每一流量的費用為w,這就符合題意了。最後建一個超級源點,從超級源向1建一條容量為k,費用為0的邊,就可進行最小費用最大流演算法。 
#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]/*可改進量*/;queue<int>q;void Init_MCMF(){memset(first,-1,sizeof(first));en=0;S=1;T=n;}void AddEdge(const int &U,const int &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;}

  

【最大流】【費用流】bzoj1834 [ZJOI2010]network 網路擴容

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.