2193. 分配问题

题目链接

2193. 分配问题

\(n\) 件工作要分配给 \(n\) 个人做。

\(i\) 个人做第 \(j\) 件工作产生的效益为 \(c_{ij}\)

试设计一个将 \(n\) 件工作分配给 \(n\) 个人做的分配方案。

对于给定的 \(n\) 件工作和 \(n\) 个人,计算最优分配方案和最差分配方案。

输入格式

\(1\) 行有 \(1\) 个正整数 \(n\),表示有 \(n\) 件工作要分配给 \(n\) 个人做。

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

输出格式

第一行输出最差分配方案下的最小总效益。

第二行输出最优分配方案下的最大总效益。

数据范围

\(1 \le n \le 50\),
\(0 \le c_{ij} \le 100\)

输入样例:

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

输出样例:

5
14

解题思路

费用流,二分图最优匹配

建图:建立源点 \(s\) 和汇点 \(t\)\(s\) 向所有工人表示的点连边,容量为 \(1\),费用为 \(0\),所有的工作表示的点向 \(t\) 连边,容量为 \(1\),费用为 \(0\),所有的工人表示的点向所有工作表示的点连边,容量足够大,费用为工人对待工作的效益,最后 \(s\)\(t\) 的费用流即为所求,\(\color{red}{为什么?}\)简单说一下,可行流与实际问题一一对应,即如果工人表示的点到某个工作表示的点有流量,则说明该工人选择该工作,且由于可行流流量守恒,最后一定是满流状态,即最大流,这时的费用流最小/大,对应实际问题中匹配的效益之和最小/大

  • 时间复杂度:\(O(kn^2f)\)

代码

// Problem: 分配问题
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/2195/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=105,M=(N+2505)*2,inf=1e9;
int n,S,T;
int h[N],f[M],w[M],e[M],ne[M],idx;
int incf[N],d[N],pre[N],q[N];
bool st[N];
void add(int a,int b,int c,int d)
{
	e[idx]=b,f[idx]=c,w[idx]=d,ne[idx]=h[a],h[a]=idx++;
	e[idx]=a,f[idx]=0,w[idx]=-d,ne[idx]=h[b],h[b]=idx++;
}
bool spfa()
{
    memset(d,0x3f,sizeof d);
    memset(incf,0,sizeof incf);
    d[S]=0,incf[S]=inf,q[0]=S;
    int hh=0,tt=1;
    while(hh!=tt)
    {
        int x=q[hh++];
        if(hh==N)hh=0;
        st[x]=false;
        for(int i=h[x];~i;i=ne[i])
        {
            int y=e[i];
            if(d[y]>d[x]+w[i]&&f[i])
            {
                d[y]=d[x]+w[i];
                pre[y]=i;
                incf[y]=min(incf[x],f[i]);
                if(!st[y])
                {
                    q[tt++]=y;
                    if(tt==N)tt=0;
                    st[y]=true;
                }
            }
        }
    }
    return incf[T]>0;
}
int EK()
{
    int cost=0;
    while(spfa())
    {
        int t=incf[T];
        cost+=t*d[T];
        for(int i=T;i!=S;i=e[pre[i]^1])f[pre[i]]-=t,f[pre[i]^1]+=t;
    }
    return cost;
}
int main()
{
	memset(h,-1,sizeof h);
    scanf("%d",&n);
    S=0,T=n*2+1;
    for(int i=1;i<=n;i++)
    	add(S,i,1,0),add(i+n,T,1,0);
    for(int i=1;i<=n;i++)
    	for(int j=1;j<=n;j++)
    	{
    		int c;
    		scanf("%d",&c);
    		add(i,j+n,inf,c);
    	}
    printf("%d\n",EK());
    for(int i=0;i<idx;i+=2)
    {
    	f[i]+=f[i^1],f[i^1]=0;
    	swap(w[i],w[i^1]);
    }
    printf("%d",-EK());
    return 0;
}
posted @ 2022-12-01 22:41  zyy2001  阅读(18)  评论(0编辑  收藏  举报