[网络流24题][CODEVS1914]运输问题(费用流)

题目描述

传送门

题解

从源点向每一个仓库连边,费用为0,容量为仓库中货物数量;
从每一个零售店向汇点连边,费用为0,容量为零售店应得的货物数量;
从仓库向零售店连边,费用为该仓库运到零售店的费用。
跑最大费用和最小费用即可。

代码

#include
#include
#include
#include
using namespace std;

const int max_n=105;
const int max_m=105;
const int max_N=max_n*max_m+2;
const int max_M=max_n*max_m*10;
const int max_e=max_M*2;
const int INF=1e9;

int n,m,N,mincost,maxcost;
int A[max_n],B[max_m],C[max_n][max_m];
int point[max_N],next[max_e],v[max_e],remain[max_e],c[max_e],tot;
int last[max_N],dis[max_N];
bool vis[max_N];
queue <int> q;

inline void clear(){
    tot=-1;
    memset(point,-1,sizeof(point));
    memset(next,-1,sizeof(next));
    memset(v,0,sizeof(v));
    memset(remain,0,sizeof(remain));
    memset(c,0,sizeof(c));
    memset(last,0,sizeof(last));
    memset(dis,0,sizeof(dis));
    memset(vis,0,sizeof(vis));
}

inline void addedge(int x,int y,int cap,int z){
    ++tot; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap; c[tot]=z;
    ++tot; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0; c[tot]=-z;
}

inline int addflow(int s,int t){
    int ans=INF,now=t;

    while (now!=s){
        ans=min(ans,remain[last[now]]);
        now=v[last[now]^1];
    }

    now=t;
    while (now!=s){
        remain[last[now]]-=ans;
        remain[last[now]^1]+=ans;
        now=v[last[now]^1];
    }
    return ans;
}

inline bool bfs_min(int s,int t){
    memset(dis,0x7f,sizeof(dis));
    dis[s]=0;
    memset(vis,0,sizeof(vis));
    vis[s]=true;
    while (!q.empty()) q.pop();
    q.push(s);

    while (!q.empty()){
        int now=q.front(); q.pop();
        vis[now]=false;
        for (int i=point[now];i!=-1;i=next[i])
          if (dis[v[i]]>dis[now]+c[i]&&remain[i]){
            dis[v[i]]=dis[now]+c[i];
            last[v[i]]=i;
            if (!vis[v[i]]){
                vis[v[i]]=true;
                q.push(v[i]);
            }
          }
    }

    if (dis[t]>INF) return false;
    int flow=addflow(s,t);
    mincost+=flow*dis[t];
    return true;
}

inline bool bfs_max(int s,int t){
    memset(dis,128,sizeof(dis));
    dis[s]=0;
    memset(vis,0,sizeof(vis));
    vis[s]=true;
    while (!q.empty()) q.pop();
    q.push(s);

    while (!q.empty()){
        int now=q.front(); q.pop();
        vis[now]=false;
        for (int i=point[now];i!=-1;i=next[i])
          if (dis[v[i]]if (!vis[v[i]]){
                vis[v[i]]=true;
                q.push(v[i]);
            }
          }
    }

    if (dis[t]<0) return false;
    int flow=addflow(s,t);
    maxcost+=flow*dis[t];
    return true;
}

inline void major_min(int s,int t){
    mincost=0;
    while (bfs_min(s,t));
}

inline void major_max(int s,int t){
    maxcost=0;
    while (bfs_max(s,t));
}

int main(){
    clear();

    scanf("%d%d",&n,&m);
    N=n+m+2;
    for (int i=1;i<=n;++i){
        scanf("%d",&A[i]);
        addedge(1,1+i,A[i],0);
    }
    for (int i=1;i<=m;++i){
        scanf("%d",&B[i]);
        addedge(1+n+i,N,B[i],0);
    }
    for (int i=1;i<=n;++i)
      for (int j=1;j<=m;++j){
        scanf("%d",&C[i][j]);
        addedge(1+i,1+n+j,INF,C[i][j]);
      }

    major_min(1,N);

    clear();

    for (int i=1;i<=n;++i)
      addedge(1,1+i,A[i],0);
    for (int i=1;i<=m;++i)
      addedge(1+n+i,N,B[i],0);
    for (int i=1;i<=n;++i)
      for (int j=1;j<=m;++j)
        addedge(1+i,1+n+j,INF,C[i][j]);

    major_max(1,N);

    printf("%d\n",mincost);
    printf("%d\n",maxcost);
}

总结

①重新建图。

你可能感兴趣的:(题解,网络流)