P4012 深海机器人问题 建图+最大费用最大流

P4012 深海机器人问题 建图+最大费用最大流

https://www.luogu.com.cn/problem/P4012

做法: 难在建图
1️⃣超级源点1连向机器人位置,费用为0,容量为机器人数量
2️⃣顶点之间连边分为两种,只有第一次经过是带费用的,故容量为1,费用题目给出,之后经过是不带费用的,故容量为无穷,费用0
3️⃣目的地连向超级源点2,费用为0,容量为可容纳机器人容量

最大费用只需费用全都取负跑一遍最小费用最大流即可

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<queue>
#include<map>
#define ll long long
#define pb push_back
#define rep(x,a,b) for (int x=a;x<=b;x++)
#define repp(x,a,b) for (int x=a;x<b;x++)
#define W(x) printf("%d\n",x)
#define WW(x) printf("%lld\n",x)
#define pi 3.14159265358979323846
#define mem(a,x) memset(a,x,sizeof a)
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
using namespace std;
const int maxn=1e4+7;
const int INF=1e9;
const ll INFF=1e18;
struct node
{
    int to,next,cap,flow,cost;
}edge[maxn+4];
int head[maxn+4],tol,n,m,x,y,z,a,b,cost=0;
int pre[maxn+4],dis[maxn+4];
bool vis[maxn+4];
void init()
{
    tol=0;
    mem(head,-1);
}
void add(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 sx,int ex)
{
    queue<int> q;
    for (int i=0;i<=maxn+3;i++)
    {
        dis[i]=INF;
        vis[i]=false;
        pre[i]=-1;
    }
    dis[sx]=0;
    vis[sx]=true;
    q.push(sx);
    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[ex]==-1)return false;
    else return true;
}
int solve(int sx,int ex,int &cost)
{
    cost=0;
    int flow=0;
    while(spfa(sx,ex))
    {
        int minn=INF;
        for (int i=pre[ex];i!=-1;i=pre[edge[i^1].to])
            minn=min(minn,edge[i].cap-edge[i].flow);
        for (int i=pre[ex];i!=-1;i=pre[edge[i^1].to])
        {
            edge[i].flow+=minn;
            edge[i^1].flow-=minn;
            cost+=edge[i].cost*minn;
        }
        flow+=minn;
    }
    return flow;
}
int f(int x,int y)
{
    return x*(m+1)+y;
}
int main()
{
    scanf("%d%d",&a,&b);
    scanf("%d%d",&n,&m);
    init();
    rep(i,0,n)
    {
        rep(j,0,m-1)
        {
            scanf("%d",&x);
            add(f(i,j),f(i,j+1),1,-x);
            //第一次经过是有收益的,由于求最大费用故取负
            add(f(i,j),f(i,j+1),INF,0);
            //之后经过是没有收益的,但是可以经过
        }
    }
    rep(j,0,m)
    {
        rep(i,0,n-1)
        {
            scanf("%d",&x);
            add(f(i,j),f(i+1,j),1,-x);
            add(f(i,j),f(i+1,j),INF,0);
        }
    }
    while(a--)
    {
        scanf("%d%d%d",&x,&y,&z);
        add(maxn,f(y,z),x,0);//超级源点1号
    }
    while(b--)
    {
        scanf("%d%d%d",&x,&y,&z);
        add(f(y,z),maxn+1,x,0);//超级源点2号
    }
    int ans=solve(maxn,maxn+1,cost);
    //从源点一号到源点二号跑一遍最小费用最大流,对答案取负
    W(-cost);
}
发布了109 篇原创文章 · 获赞 7 · 访问量 6032

猜你喜欢

转载自blog.csdn.net/w_udixixi/article/details/104203654