1. 程式人生 > >最小費用最大流(白書+kuangbin)

最小費用最大流(白書+kuangbin)

講解:

附上白書程式碼:


#include<bits/stdc++.h>

using namespace std;

const int MAX_V=100;
const int INF=0x3f3f3f3f;

struct edge{
    int to,cap,cost,rev;
    edge(int _to,int _cap,int _cost,int _rev):to(_to),cap(_cap),cost(_cost),rev(_rev){}
};

int V;
vector<edge>G[MAX_V];
int dist[MAX_V];
int prevv[MAX_V],preve[MAX_V];

void add_edge(int from,int to,int cap,int cost)
{
    G[from].push_back(edge(to,cap,cost,G[to].size()));
    G[to].push_back(edge(from,0,-cost,G[from].size()-1));
}

int min_cost_flow(int s,int t,int f)
{
    int res=0;
    while(f>0){
        fill(dist,dist+V,INF);
        dist[s]=0;
        bool update=true;
        while(update){
            update=false;
            for(int v=0;v<V;v++){
                if(dist[v]==INF){
                    continue;
                }
                for(int i=0;i<G[v].size();i++){
                    edge &e=G[v][i];
                    if(e.cap>0&&dist[e.to]>dist[v]+e.cost){
                        dist[e.to]=dist[v]+e.cost;
                        prevv[e.to]=v;
                        preve[e.to]=i;
                        update=true;
                    }
                }
            }
        }
        if(dist[t]==INF){
            return -1;
        }
        int d=f;
        for(int v=t;v!=s;v=prevv[v]){
            d=min(d,G[prevv[v]][preve[v]].cap);
        }
        f-=d;
        res+=d*dist[t];
        for(int v=t;v!=s;v=prevv[v]){
            edge &e=G[prevv[v]][preve[v]];
            e.cap-=d;
            G[v][e.rev].cap+=d;
        }
    }
    return res;
}

int main()
{
    int m,s,t,f;
    scanf("%d%d%d%d%d",&V,&m,&s,&t,&f);
    int u,v,w,cost;
    for(int i=0;i<m;i++){
        scanf("%d%d%d%d",&u,&v,&w,&cost);
        add_edge(u,v,w,cost);
    }
    printf("%d\n",min_cost_flow(s,t,f));
    return 0;
}

附上白書Dijkstra程式碼:

#include<bits/stdc++.h>

using namespace std;

const int MAX_V=100;
const int INF=0x3f3f3f3f;

typedef pair<int,int>P;

struct edge{
    int to,cap,cost,rev;
    edge(int _to,int _cap,int _cost,int _rev):to(_to),cap(_cap),cost(_cost),rev(_rev){}
};

int V;
vector<edge>G[MAX_V];
int h[MAX_V];
int dist[MAX_V];
int prevv[MAX_V],preve[MAX_V];

void add_edge(int from,int to,int cap,int cost)
{
    G[from].push_back(edge(to,cap,cost,G[to].size()));
    G[to].push_back(edge(from,0,-cost,G[from].size()-1));
}

int min_cost_flow(int s,int t,int f)
{
    int res=0;
    fill(h,h+V,0);
    while(f>0){
        priority_queue<P,vector<P>,greater<P> >que;
        fill(dist,dist+V,INF);
        dist[s]=0;
        que.push(P(0,s));
        while(!que.empty()){
            P p=que.top();que.pop();
            int v=p.second;
            if(dist[v]<p.first){
                continue;
            }
            for(int i=0;i<G[v].size();i++){
                edge &e=G[v][i];
                if(e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
                    dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
                    prevv[e.to]=v;
                    preve[e.to]=i;
                    que.push(P(dist[e.to],e.to));
                }
            }
        }
        if(dist[t]==INF){
            return -1;
        }
        for(int v=0;v<V;v++){
            h[v]+=dist[v];
        }
        int d=f;
        for(int v=t;v!=s;v=prevv[v]){
            d=min(d,G[prevv[v]][preve[v]].cap);
        }
        f-=d;
        res+=d*h[t];
        for(int v=t;v!=s;v=prevv[v]){
            edge &e=G[prevv[v]][preve[v]];
            e.cap-=d;
            G[v][e.rev].cap+=d;
        }
    }
    return res;
}

int main()
{
    int m,s,t,f;
    scanf("%d%d%d%d%d",&V,&m,&s,&t,&f);
    int u,v,w,cost;
    for(int i=0;i<m;i++){
        scanf("%d%d%d%d",&u,&v,&w,&cost);
        add_edge(u,v,w,cost);
    }
    printf("%d\n",min_cost_flow(s,t,f));
    return 0;
}

附上Kuangbin程式碼:

//spfa版費用流
//最小費用最大流,求最大流費用只需要取相反數,結果取相反數即可
//點的總數為N,點的編號為0~N-1
#include<bits/stdc++.h>

using namespace std;

const int MAXN=10000;
const int MAXM=100000;
const int INF=0x3f3f3f3f;

struct Edge{
    int to,next,cap,flow,cost;
}edge[MAXM];
int head[MAXN],tol;

int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;

void init(int n)
{
    N=n;
    tol=0;
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int cap,int cost)
{
    edge[tol].to=v;
    edge[tol].cap=cap;
    edge[tol].cost=cost;
    edge[tol].flow=0;
    edge[tol].next=head[u];
    head[u]=tol++;
    edge[tol].to=u;
    edge[tol].cap=0;
    edge[tol].cost=-cost;
    edge[tol].flow=0;
    edge[tol].next=head[v];
    head[v]=tol++;
}

bool spfa(int s,int t)
{
    queue<int>q;
    for(int i=0;i<N;i++){
        dist[i]=INF;
        vis[i]=false;
        pre[i]=-1;
    }
    dist[s]=0;
    vis[s]=true;
    q.push(s);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost){
                dis[v]=dis[u]+edge[i].cost;
                pre[v]=i;
                if(!vis[v]){
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t]==-1){
        return false;
    }else{
        return true;
    }
}

int minCostMaxflow(int s,int t,int &cost)
{
    int flow=0;
    cost=0;
    while(spfa(s,t)){
        int Min=INF;
        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){
            if(Min>edge[i].cap-edge[i].flow){
                Min=edge[i].cap-edge[i].flow;
            }
        }
        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){
            edge[i].flow+=Min;
            edge[i^1].flow-=Min;
            cost+=edge[i].cost*Min;
        }
        flow+=Min;
    }
    return flow;
}