码迷,mamicode.com
首页 > 其他好文 > 详细

Luogu P4015 运输问题

时间:2019-03-08 09:36:06      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:水题   nod   最大   memset   emc   inf   name   inline   struct   

题目链接 \(Click\) \(Here\)

继续颓网络流\(hhhhh\),虽然这次写的是个大水题,但是早上水一个网络流果然还是让人心情舒畅啊~

最大费用最大流不用非得反着费用建边.只要没有正环,初始化的时候小心一点就好啦~

#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;

int cnt = -1, head[210];

struct edge {
    int nxt, to, w, f;
}e[40010];

void add_edge (int from, int to, int flw, int val) {
    e[++cnt].nxt = head[from];
    e[cnt].to = to;
    e[cnt].f = flw;
    e[cnt].w = val;
    head[from] = cnt;
}

void add_len (int u, int v, int f, int w) {
    add_edge (u, v, f, +w);
    add_edge (v, u, 0, -w);
}

int _head[210]; edge _e[40010];
int n, m, numA[110], numB[110], cost[110][110];

int A (int x) {return n * 0 + x;}
int B (int x) {return n * 1 + x;}

queue <int> q;
int dis[210], vis[210], flow[210];
int pre_edge[210], pre_node[210];

bool can_use (int u, int v, int i, int type) {
    if (type == +1) return dis[v] > dis[u] + e[i].w;
    if (type == -1) return dis[v] < dis[u] + e[i].w;
}

int spfa (int s, int t, int type) {
    memset (vis, 0, sizeof (vis));
    memset (flow, 0x3f, sizeof (flow));
    if (type == +1) memset (dis, +0x3f, sizeof (dis));
    if (type == -1) memset (dis, -0x3f, sizeof (dis));
    vis[s] = true; dis[s] = 0; q.push (s);
    while (!q.empty ()) {
        int u = q.front (); q.pop ();
        for (int i = head[u]; ~i; i = e[i].nxt) {
            int v = e[i].to;
            if (can_use (u, v, i, type) && e[i].f) {
                dis[v] = dis[u] + e[i].w;
                flow[v] = min (flow[u], e[i].f);
                pre_edge[v] = i;
                pre_node[v] = u;
                if (!vis[v]) {
                    vis[v] = true;
                    q.push (v);
                }
            }
        }
        vis[u] = false;
    }
    return flow[t] != INF;
}

int MCMF (int s, int t, int type) {
    int cost = 0;
    while (spfa (s, t, type)) {
        cost += dis[t] * flow[t];
        int u = t;
        while (u != s) {
            e[pre_edge[u] ^ 0].f -= flow[t];
            e[pre_edge[u] ^ 1].f += flow[t];
            u = pre_node[u];
        } 
    }
    return cost;
}

int main () {
    memset (head, -1, sizeof (head));
    cin >> n >> m;
    int s = n + m + 1;
    int t = n + m + 2;
    for (int i = 1; i <= n; ++i) {cin >> numA[i]; add_len (s, A (i), numA[i], 0);}
    for (int i = 1; i <= m; ++i) {cin >> numB[i]; add_len (B (i), t, numB[i], 0);}
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> cost[i][j];
            add_len (A (i), B (j), INF, cost[i][j]);
        }
    }
    memcpy (_e, e, sizeof (e)); memcpy (_head, head, sizeof (head));
    cout << MCMF (s, t, +1) << endl;
    memcpy (e, _e, sizeof (e)); memcpy (head, _head, sizeof (head));
    cout << MCMF (s, t, -1) << endl;
}

Luogu P4015 运输问题

标签:水题   nod   最大   memset   emc   inf   name   inline   struct   

原文地址:https://www.cnblogs.com/maomao9173/p/10493681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!