zoukankan      html  css  js  c++  java
  • P4014 分配问题 最小费用最大流

      

    题目描述

    有 nn 件工作要分配给 nn 个人做。第 ii 个人做第 jj 件工作产生的效益为 c_{ij}cij 。试设计一个将 nn 件工作分配给 nn 个人做的分配方案,使产生的总效益最大。

    输入输出格式

    输入格式:

    文件的第 11 行有 11 个正整数 nn,表示有 nn 件工作要分配给 nn 个人做。

    接下来的 nn 行中,每行有 nn 个整数 c_{ij}cij​​,表示第 ii 个人做第 jj 件工作产生的效益为 c_{ij}cij

    输出格式:

    两行分别输出最小总效益和最大总效益。

    输入输出样例

    输入样例#1: 复制
    5
    2 2 2 1 2
    2 3 1 2 4
    2 0 1 1 1
    2 3 4 3 3
    3 2 1 2 1
    输出样例#1: 复制
    5
    14

    说明

    1 leq n leq 1001n100

    一个人只能修一个工件

    费用流水题

    跑两次分别求最小和最大即可

    #include<bits/stdc++.h>
    using namespace std;
    //input by bxd
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i>=(b);--i)
    #define RI(n) scanf("%d",&(n))
    #define RII(n,m) scanf("%d%d",&n,&m)
    #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
    #define RS(s) scanf("%s",s);
    #define ll long long
    #define pb push_back
    #define inf 0x3f3f3f3f
    #define CLR(A,v)  memset(A,v,sizeof A)
    //////////////////////////////////
    const int N=1000001;
    
    int n,m,S,T,maxflow,mincost,last[N],pre[N],dis[N],flow[N];
    bool vis[N];
    struct Edge{
        int next,to,flow,dis;
    }edge[N<<1];
    int pos=1,head[N];
    void init()
    {
        pos=1;
        CLR(head,0);
        mincost=maxflow=0;
    }
    queue <int> q;
    int id(int x,int y) {return n*(x-1)+y;}
    
    void add(int from,int to,int flow,int dis)//flow流量 dis费用
    {
        edge[++pos].next=head[from];
        edge[pos].flow=flow;
        edge[pos].dis=dis;
        edge[pos].to=to;
        head[from]=pos;
        edge[++pos].next=head[to];
        edge[pos].flow=0;
        edge[pos].dis=-dis;
        edge[pos].to=from;
        head[to]=pos;
    }
    bool spfa(int s,int t)
    {
        CLR(dis,0x3f);
        CLR(flow,0x3f);
        CLR(vis,0);
        while (!q.empty()) q.pop();
        dis[s]=0; pre[t]=-1; q.push(s); vis[s]=1;
        int tot=0;
        while (!q.empty())
        {
            int now=q.front(); q.pop(); vis[now]=0;
            for (int i=head[now]; i; i=edge[i].next)
            {
                int to=edge[i].to;
                if  (edge[i].flow>0 && dis[to]>dis[now]+edge[i].dis)
                {
                    dis[to]=edge[i].dis+dis[now];
                    flow[to]=min(edge[i].flow,flow[now]);
                    last[to]=i;
                    pre[to]=now;
                    if (!vis[to])
                    {
                        q.push(to); vis[to]=1;
                    }
                }
            }
        }
        return pre[t]!=-1;
    }
    void MCMF(int s,int t)
    {
        while (spfa(s,t))
        {
            int now=t;
            maxflow+=flow[t];
            mincost+=flow[t]*dis[t];
            while (now!=s)
            {
                edge[last[now]].flow-=flow[t];//dis . flow
                edge[last[now]^1].flow+=flow[t];
                now=pre[now];
            }
        }
    }
    struct node
    {
        int u,v,cost;
    }node[N];
    int s,t,k;
    
    int mp[200][200];
    int main()
    {
        RI(n);
        s=0,t=2*n+1;
        rep(i,1,n)
        rep(j,1,n)
        {
            RI(mp[i][j]);add(i,j+n,1,mp[i][j]);
        }
        rep(i,1,n){add(s,i,1,0);add(i+n,t,1,0);}
        MCMF(s,t);
        cout<<mincost<<endl;
        
        init();
        rep(i,1,n)rep(j,1,n)add(i,j+n,1,-mp[i][j]);
        rep(i,1,n){add(s,i,1,0);add(i+n,t,1,0);}
        MCMF(s,t);
        cout<<-mincost;
    
        return 0;
    }
    View Code
  • 相关阅读:
    JQuery 学习总结及实例 !! (转载)
    JavaScript 学习笔记
    个人对JS的一些见解
    本博客欢迎交流,文章自由转载,保持署名!
    VSCode:源码编译运行,分析,踩坑
    ant design pro/前端/JS:实现本地运行https
    前端/JS/React/ES6:纯前端实现图片压缩技术
    云服务名词:软件即服务SaaS,怎么这个理解起来这么别扭
    React:effect应该怎么翻译比较合适??
    我给博客加了一个娃娃,一片雪花
  • 原文地址:https://www.cnblogs.com/bxd123/p/10944026.html
Copyright © 2011-2022 走看看