数字梯形问题【网络流24题】【最大流最小费用求最大费用】

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

题目链接

这个题目链接就比较水了,具体下面会说


  这道题的题意倒是很好理解,但是怎么建边思索了我几天,于是有了这样子的想法。

  先是解决怎么处理最大费用的问题,看过网上的一些博主写的,直接利用SPFA去跑最大费用,在洛谷这个平台提交的话会一直WA在第2组,后来我尝试着换种写法于是才过,那么这第二组到底是什么个情况呢?这组数据就是卡了直接跑最大费用的情况,所以,我们得换着处理这个问题,不妨就去把每个点的权值赋值为其相反数,然后跑的是最大流最小费用,最后得到的是相反数的最小费用,取反就是原数的最大费用。——第二个链接可以不考虑这样的问题,也能A,所以是数据太水了。

  对于问题一:每个点只能流过一次,那么既然每个点只能经过一次,就会保证每条边都不会重叠上去,于是乎直接把每个点拆点,然后直接跑一遍最大流最小费用。

  对于问题二:每条边只能跑一次,这次我们对于每个点就不用限流了,每个点可以流过的流量是INF,但是对于每条边,还是需要限流。

  对于问题三:没有这样的限制了,直接去连图然后直接跑最大流最小费用即可。


#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, maxE = 2e5 + 7, maxN = 2e3 + 7;
int N, M, w[maxN], head[maxN], cnt, T, tot, pre[maxN], dist[maxN], Flow[maxN];
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));    memset(Flow, 0, sizeof(Flow));
    while(!Q.empty()) Q.pop();
    Q.push(S);  pre[S] = 0; 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 += dist[T] * Flow[T];
    }
    return -ans;
}
inline void Solve_1()   //每个点只经过一次
{
    for(int i=1; i<=M; i++) _add(S, i, 1, w[i]);
    for(int i=1; i<N; i++)
    {
        for(int j=1, now, to; j<M+i; j++)
        {
            now = (i - 1) * ((M<<1) + i - 2)/2 + j;
            _add(now, now + tot, 1, 0);
            to = now + M + i - 1;
            _add(now + tot, to, 1, w[to]);
            to++;
            _add(now + tot, to, 1, w[to]);
        }
    }
    for(int i=1, u; i < M + N; i++)
    {
        u = (N - 1) * ((M<<1) + N - 2)/2 + i;
        _add(u, u + tot, 1, 0);
        _add(u + tot, T, 1, 0);
    }
    printf("%d\n", Out_put());
}
inline void Solve_2()   //路径只经过一次
{
    cnt = 0;    memset(head, -1, sizeof(head));
    for(int i=1; i<=M; i++) _add(S, i, 1, 0);
    for(int i=1; i<N; i++)
    {
        for(int j=1, now, to; j<M+i; j++)
        {
            now = (i - 1) * ((M<<1) + i - 2)/2 + j;
            _add(now, now + tot, INF, w[now]);
            to = now + M + i - 1;
            _add(now + tot, to, 1, 0);
            to++;
            _add(now + tot, to, 1, 0);
        }
    }
    for(int i=1, u; i < M + N; i++)
    {
        u = (N - 1) * ((M<<1) + N - 2)/2 + i;
        _add(u, T, INF, w[u]);
    }
    printf("%d\n", Out_put());
}
inline void Solve_3()
{
    cnt = 0;    memset(head, -1, sizeof(head));
    for(int i=1; i<=M; i++) _add(S, i, 1, w[i]);
    for(int i=1; i<N; i++)
    {
        for(int j=1, now, to; j<M+i; j++)
        {
            now = (i - 1) * ((M<<1) + i - 2)/2 + j;
            to = now + M + i - 1;
            _add(now, to, INF, w[to]);
            to++;
            _add(now, to, INF, w[to]);
        }
    }
    for(int i=1, u; i < M + N; i++)
    {
        u = (N - 1) * ((M<<1) + N - 2)/2 + i;
        _add(u, T, INF, 0);
    }
    printf("%d\n", Out_put());
}
inline void init()
{
    cnt = 0;    tot = ((M<<1) + N - 1) * N / 2;    T = (tot<<1) + 1;
    memset(head, -1, sizeof(head));
}
int main()
{
    scanf("%d%d", &M, &N);
    init();
    for(int i=1; i<=N; i++) for(int j=1; j<M+i; j++) { scanf("%d", &w[(i - 1) * ((M<<1) + i - 2)/2 + j]); w[(i - 1) * ((M<<1) + i - 2)/2 + j] = -w[(i - 1) * ((M<<1) + i - 2)/2 + j]; }
    Solve_1();
    Solve_2();
    Solve_3();
    return 0;
}

猜你喜欢

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