代码模板——P3381 【模板】最小费用最大流 dij版

本文深入探讨了最小费用流算法的实现细节,包括使用邻接表表示图结构,通过Dijkstra算法更新顶点势,以及如何求解从源点到汇点的最小费用流。文章提供了两种实现方式:一种使用vector存储邻接表,另一种则采用手动创建邻接表的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vector版:

// luogu-judger-enable-o2
#include<bits/stdc++.h>

using namespace std;

const int MAXN = 5e3+50;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> P;
struct edge{int to, cap, cost, rev;};
int n;                          //顶点数
vector<edge> G[MAXN];           //图的邻接表表示
int h[MAXN];                    //顶点的势
int dist[MAXN];                 //最短距离
int prevv[MAXN], preve[MAXN];   //最短路中的前驱结点和对应的边

//优化输入
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}

//向图中增加一条从from到to容量为cap费用为cost的边
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});
}

//求解从s到t流量为f的最小费用流
//如果没有流量为f的流,则返回-1
//当f为0时,是流量为f的最小费用
//当f大于0时,是最大流的最小费用
int min_cost_flow(int s, int t, int &f){
    int res = 0;
    fill(h, h+n+1,0); //初始化h
    while(f>0){
        //使用Dijstra算法更新h
        priority_queue<P, vector<P>, greater<P> >que;
        fill(dist, dist+n+1, 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 res;
        }
        for(int v = 1; v <= n; 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() {
	//ios::sync_with_stdio(false);
	int m, s, t;
	scanf("%d%d%d%d", &n, &m, &s, &t);
    int u, v, w, f;
    for(int i = 1; i <= m; i++){
        scan_d(u);scan_d(v);scan_d(w);scan_d(f);
        add_edge(u, v, w, f);
    }
    int maxflow = INF;
    int mincost = min_cost_flow(s, t, maxflow);
    printf("%d %d\n", INF-maxflow, mincost);
	return 0;
}
/*
9 2
5 3 2 1 4 2 1 4 6
*/

手写邻接表版:

// luogu-judger-enable-o2
#include<bits/stdc++.h>

using namespace std;

const int MAXN = 5e3+50;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> P;
struct edge{int from, to, cap, cost, rev, next;};
int n;                          //顶点数
int tot;                        //统计边数
edge G[MAXN*20];                //图的邻接表表示
int head[MAXN];                 //每个顶点的第一条边
int h[MAXN];                    //顶点的势
int dist[MAXN];                 //最短距离
int prevv[MAXN], preve[MAXN];   //最短路中的前驱结点和对应的边

//优化输入
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}

//图初始化
void init()
{
    memset(head, -1, sizeof(head));
    tot = 2;
}

//向图中增加一条从from到to容量为cap费用为cost的边
void add_edge(int from, int to, int cap, int cost){
    G[tot] = edge{from, to, cap, cost, tot+1, head[from]}; head[from] = tot++;
    G[tot] = edge{to, from, 0, -cost, tot-1, head[to]}; head[to] = tot++;
//    G[from].push_back((edge){to, cap, cost, G[to].size()});
//    G[to].push_back((edge){from, 0, -cost, G[from].size()-1});
}

//求解从s到t流量为f的最小费用流
//如果没有流量为f的流,则返回-1
//当f为0时,是流量为f的最小费用
//当f大于0时,是最大流的最小费用
int min_cost_flow(int s, int t, int &f){
    int res = 0;
    fill(h, h+n+1,0); //初始化h
    while(f>0){
        //使用Dijstra算法更新h
        priority_queue<P, vector<P>, greater<P> >que;
        fill(dist, dist+n+1, 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 = head[p.second]; ~i; i = G[i].next){
                edge &e = G[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 res;
        }
        for(int v = 1; v <= n; v++) h[v] += dist[v];

        int d = f;
        for(int v = t; v != s; v = G[preve[v]].from){
            d = min(d, G[preve[v]].cap);
        }
        f -= d;
        res += d*h[t];
        for(int v = t; v != s; v = G[preve[v]].from){
            edge &e = G[preve[v]];
            e.cap -= d;
            G[e.rev].cap += d;
        }
    }
    return res;
}

int main() {
	//ios::sync_with_stdio(false);
	init();
	int m, s, t;
	scanf("%d%d%d%d", &n, &m, &s, &t);
    int u, v, w, f;
    for(int i = 1; i <= m; i++){
        scan_d(u);scan_d(v);scan_d(w);scan_d(f);
        add_edge(u, v, w, f);
    }
    int maxflow = INF;
    int mincost = min_cost_flow(s, t, maxflow);
    printf("%d %d\n", INF-maxflow, mincost);
	return 0;
}
/*
9 2
5 3 2 1 4 2 1 4 6
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值