网络流24题(十八)

网络流24题(十八)

十八、分配问题

题目描述

有 n 件工作要分配给 n 个人做。第 i 个人做第 j 件工作产生的效益为 \(c_{ij}\)​ 。试设计一个将 n 件工作分配给 n 个人做的分配方案,使产生的总效益最大。

输入格式

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

接下来的 n 行中,每行有 n 个整数 \(c_{ij}\)​​​,表示第 i 个人做第 j 件工作产生的效益为 \(c_{ij}\)

输出格式

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

题解

模型

二分图最大权匹配

建图

把所有人看做二分图中顶点\(X_i\),所有任务看做二分图中顶点\(Y_i\),建立附加源S汇T。

  1. 从S向每个\(X_i\)连一条容量为\(1\),费用为0的有向边。
  2. 从每个\(Y_i\)向T连一条容量为\(1\),费用为0的有向边。
  3. 从每个\(X_i\)向每个\(Y_j\)连接一条容量为1,费用为\(c_{ij}\)的有向边。

求最小费用最大流,最小费用流值就是最少最小效益,求最大费用最大流,最大费用流值就是最大效益。

代码

#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
using namespace std;
#define ll long long
const ll inf = 0x3f3f3f3f;
const int N = 5000,M = 5e4+50;
ll head[N],cnt = 1;
struct Edge{
    ll to,w,cost,nxt;
}edge[M*2];
void add(ll u,ll v,ll w,ll c){
    edge[++cnt] = {v,w,c,head[u]};
    head[u] = cnt;
}
void add2(ll u,ll v,ll w,ll cost){
    add(u,v,w,cost);
    add(v,u,0,-cost);
}
ll s,t,dis[N],cur[N];
bool inq[N],vis[N];
queue<ll>Q;
bool spfa(){
    while(!Q.empty()) Q.pop();
    copy(head,head+N,cur);
    fill(dis,dis+N,inf);
    dis[s] = 0;
    Q.push(s);
    while(!Q.empty()){
        ll p = Q.front();
        Q.pop();
        inq[p] = false;
        for(ll e = head[p];e;e = edge[e].nxt){
            ll to = edge[e].to,vol = edge[e].w;
            if(vol > 0 && dis[to]>dis[p]+edge[e].cost){
                dis[to] = dis[p] + edge[e].cost;
                if(!inq[to]){
                    Q.push(to);
                    inq[to] = true;
                }
            }
        }
    }
    return dis[t] != inf;
}
ll dfs(ll p = s,ll flow = inf){
    if(p == t) return flow;
    vis[p] = true;
    ll rmn = flow;
    for(ll eg = cur[p];eg && rmn;eg = edge[eg].nxt){
        cur[p] = eg;
        ll to = edge[eg].to,vol = edge[eg].w;
        if(vol > 0 && !vis[to]&&dis[to] == dis[p]+edge[eg].cost){
            ll c = dfs(to,min(vol,rmn));
            rmn -= c;
            edge[eg].w -= c;
            edge[eg^1].w += c;
        }
    }
    vis[p] = false;
    return flow-rmn;
}
ll maxflow,mincost;
void dinic(){
    maxflow = 0,mincost = 0;
    while(spfa()){
        ll flow = dfs();
        maxflow += flow;
        mincost += dis[t]*flow;
    }
}
ll n;
ll c[N][N];
void init1(){
    for(ll i = 1;i <= n;i++)add2(s,i,1,0);
    for(ll i = 1;i <= n;i++)add2(i+n,t,1,0);
    for(ll i = 1;i <= n;i++){
        for(ll j = 1;j <= n;j++){
            add2(i,j+n,1,c[i][j]);
        }
    }
    dinic();
    cout<<mincost<<endl;
}
void init2(){
    memset(head,0, sizeof head);
    cnt = 1;
    for(ll i = 1;i <= n;i++)add2(s,i,1,0);
    for(ll i = 1;i <= n;i++)add2(i+n,t,1,0);
    for(ll i = 1;i <= n;i++){
        for(ll j = 1;j <= n;j++){
            add2(i,j+n,1,-c[i][j]);
        }
    }
    dinic();
    cout<<-mincost<<endl;
}
int main(){
    //ios::sync_with_stdio(false);
    cin>>n;
    s = 0,t = 2*n+1;
    for(ll i = 1;i <= n;i++){
        for(ll j = 1;j <= n;j++){
            cin>>c[i][j];
        }
    }
    init1();
    init2();
    return 0;
}
posted @ 2021-10-26 13:18  Paranoid5  阅读(16)  评论(0编辑  收藏  举报