运输问题【网络流24题】【最大流最小费用+最大费用】

版权声明:https://blog.csdn.net/qq_41730082 https://blog.csdn.net/qq_41730082/article/details/88828642

题目链接


  一道简单的模板题了,在跑最大费用的时候得去考虑将每条边赋为负值,然后再跑最小费用才行。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int S = 0, maxN = 207, maxE = 2.1e4;
int M, N, a[107], b[107], head[maxN], cnt, pre[maxN], Flow[maxN], dist[maxN], T, w[107][107];
bool inque[maxN];
queue<int> Q;
struct Eddge
{
    int nex, u, v, flow, cost;
    Eddge(int a=-1, int b=0, int c=0, int d=0, int f=0):nex(a), u(b), v(c), flow(d), cost(f) {}
}edge[maxE];
inline void addEddge(int u, int v, int flow, int cost)
{
    edge[cnt] = Eddge(head[u], u, v, flow, cost);
    head[u] = cnt++;
}
inline void _add(int u, int v, int flow, int cost) { addEddge(u, v, flow, cost); addEddge(v, u, 0, -cost); }
bool spfa()
{
    memset(pre, -1, sizeof(pre));   memset(dist, INF, sizeof(dist));    memset(inque, false, sizeof(inque));
    while(!Q.empty()) Q.pop();  Q.push(S);  inque[S] = true;    Flow[S] = INF;  dist[S] = 0;
    while(!Q.empty())
    {
        int u = Q.front();  Q.pop();    inque[u] = false;
        for(int i=head[u], v, flow, cost; ~i; i=edge[i].nex)
        {
            v = edge[i].v;  flow = edge[i].flow;    cost = edge[i].cost;
            if(flow && dist[v] > dist[u] + cost)
            {
                dist[v] = dist[u] + cost;
                pre[v] = i;
                Flow[v] = min(Flow[u], flow);
                if(!inque[v])
                {
                    inque[v] = true;
                    Q.push(v);
                }
            }
        }
    }
    return pre[T] ^ -1;
}
int Out_put()
{
    int ans = 0;
    while(spfa())
    {
        int now = T, last = pre[now];
        while(now)
        {
            edge[last].flow -= Flow[T];
            edge[last ^ 1].flow += Flow[T];
            now = edge[last].u;
            last = pre[now];
        }
        ans += Flow[T] * dist[T];
    }
    return ans;
}
inline void init()
{
    cnt = 0;
    memset(head, -1, sizeof(head));
}
inline void Solve_1()
{
    init();
    for(int i=1; i<=M; i++) _add(S, i, a[i], 0);
    for(int i=1; i<=N; i++) _add(i + M, T, b[i], 0);
    for(int i=1; i<=M; i++) for(int j=1; j<=N; j++) _add(i, j + M, INF, w[i][j]);
    printf("%d\n", Out_put());
}
inline void Solve_2()
{
    init();
    for(int i=1; i<=M; i++) _add(S, i, a[i], 0);
    for(int i=1; i<=N; i++) _add(i + M, T, b[i], 0);
    for(int i=1; i<=M; i++) for(int j=1; j<=N; j++) _add(i, j + M, INF, -w[i][j]);
    printf("%d\n", -Out_put());
}
int main()
{
    scanf("%d%d", &M, &N);  T = N + M + 1;
    for(int i=1; i<=M; i++) scanf("%d", &a[i]);
    for(int i=1; i<=N; i++) scanf("%d", &b[i]);
    for(int i=1; i<=M; i++) for(int j=1; j<=N; j++) scanf("%d", &w[i][j]);
    Solve_1();
    Solve_2();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41730082/article/details/88828642