动态加点KM
这个KM模型已经比较常见了:
http://blog.csdn.net/cyxhahaha/article/details/44657619
构图:
X 侧点为对菜品的需求,Y 侧点为厨师对菜品的供应
如果让
i
厨师在倒数第
记 ps=∑pi ,如果把每个厨师拆成 ps 个点,最多会有 n∗ps 个点,显然是不能承受的。
考虑第一次匹配,我们只会让每个厨师最先制作该菜品。
而第二次匹配时,可以通过在Y侧,新增已经匹配的厨师倒数第二次制作菜品的点。
所以我们可以通过每次匹配完一个X侧点之后,新增一个Y侧点,这样就能只建 m+ps 的点,从而解决问题。
同类的问题有 bzoj1070
bzoj 1070 的构图
void build()
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
for(int k = 1; k <= n; k++)
w[i][m*(k-1)+j] = -fix[i][j]*k;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>
template<class Num>void read(Num &x)
{
char c; int flag = 1;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flag *= -1;
x = c - '0';
while((c = getchar()) >= '0' && c <= '9')
x = (x<<3) + (x<<1) + (c-'0');
x *= flag;
return;
}
template<class Num>void write(Num x)
{
if(x < 0) putchar('-'), x = -x;
static char s[20];int sl = 0;
while(x) s[sl++] = x%10 + '0',x /= 10;
if(!sl) {putchar('0');return;}
while(sl) putchar(s[--sl]);
}
const int maxp = 805, maxn = 45, maxm = 105, size = maxp*maxm + maxp;
const int INF = 0x3f3f3f3f, Nya = -1;
int n, m, tot, ind, p[maxn];
int cost[maxp][maxm], w[maxp][size];
int set[maxp], cnt[maxm], type[size];
bool visx[maxp], visy[size];
int lnk[size], lx[maxp], ly[size];
int slack;
void add(int t)
{
type[++ind] = t, ++cnt[t];
for(int i = 1; i <= tot; i++)
w[i][ind] = -cnt[t]*cost[set[i]][t];
}
bool find(int a)
{
visx[a] = true;
for(int i = 1; i <= ind; i++)
{
if(visy[i]) continue;
int calc = lx[a] + ly[i] - w[a][i];
if(!calc)
{
visy[i] = true;
if(!lnk[i] || find(lnk[i]))
{
if(!lnk[i]) add(type[i]);
lnk[i] = a;
return true;
}
}
else
slack = std::min(slack, calc);
}
return false;
}
void clear()
{
for(int i = 1; i <= tot; i++) visx[i] = false;
for(int i = 1; i <= ind; i++) visy[i] = false;
}
void init()
{
read(n), read(m);
for(int i = 1; i <= n; i++)
read(p[i]), tot += p[i];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
read(cost[i][j]);
}
void build()
{
int id = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= p[i]; j++)
set[id + j] = i;
id += p[i];
}
for(int i = 1; i <= m; i++) add(i);
}
void adjust()
{
for(int i = 1; i <= tot; i++)
if(visx[i]) lx[i] -= slack;
for(int i = 1; i <= ind; i++)
if(visy[i]) ly[i] += slack;
}
int KM()
{
int ret = 0;
for(int i = 1; i <= tot; i++)
{
lx[i] = -INF;
for(int j = 1; j <= ind; j++)
lx[i] = std::max(lx[i], w[i][j]);
}
// lx[i] + ly[j] >= w[i][j]
for(int i = 1; i <= tot; i++)
{
while(true)
{
slack = INF;
clear();
if(find(i)) break;
adjust();
}
}
for(int i = 1; i <= ind; i++)
if(lnk[i]) ret += w[lnk[i]][i];
return -ret;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj2879.in","r",stdin);
freopen("bzoj2879.out","w",stdout);
#endif
init(), build();
write(KM());
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
然而上面的程序实测会 TLE,因为边数太多,共有 ps∗(m+ps) 条边
其实我们可以对
n
种菜品建点,而不是对
将 KM 改成最小费用最大流算法,X侧结点由
ps
个缩成
n
个,源点到X侧结点的流量设为
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
template<class Num>void read(Num &x)
{
char c;
int flag = 1;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flag *= -1;
x = c - '0';
while((c = getchar()) >= '0' && c <= '9')
x *= 10, x += c - '0';
x *= flag;
return;
}
template<class Num>void write(Num x)
{
static char s[20];
int sl = 0;
if(x < 0) putchar('-'), x = -x;
while(x) s[sl++] = x%10 + '0', x /= 10;
if(!sl) {putchar('0'); return;}
while(sl) putchar(s[--sl]);
return;
}
#define REP(__i,__start,__end) for(int __i = (__start); __i <= (__end); __i++)
#define REPD(__i,__start,__end) for(int __i = (__start); __i >= (__end); __i--)
const int maxn = 50, maxm = 105, maxp = 805, INF = 0x3f3f3f3f;
const int node = maxn + maxm + maxp, size = maxn * node;
struct Edge
{
int v, cap, cost, next;
Edge(int v = 0,int cap = 0,int cost = 0,int next = 0):v(v), cap(cap), cost(cost), next(next){}
}edge[size<<1];
int n, m, p[maxn], t[maxn][maxm];
int head[node], el = 1, S, T, ind;
int dist[node], fr[node], fd[node];
int set[node], cnt[maxm];
bool flag[node];
void newedge(int u,int v,int cap,int cost)
{
edge[++el] = Edge(v, cap, cost, head[u]), head[u] = el;
edge[++el] = Edge(u, 0, -cost, head[v]), head[v] = el;
}
bool SPFA()
{
static int line[node];
static bool hash[node];
register int f = 0, r = 0;
REP(i, 1, ind) dist[i] = INF, fr[i] = fd[i] = 0;
dist[S] = 0, line[r] = S, r = (r + 1)%node;
hash[S] = true;
while(f != r)
{
int x = line[f], p, calc;
line[f] = 0, f = (f + 1)%node;
hash[x] = false;
for(register int i = head[x]; i; i = edge[i].next)
if(edge[i].cap)
{
p = edge[i].v, calc = dist[x] + edge[i].cost;
if(calc < dist[p])
{
dist[p] = calc, fr[p] = x, fd[p] = i;
if(!hash[p])
{
if(dist[p] <= dist[line[f]])
f = (f - 1 + node)%node, line[f] = p;
else
line[r] = p, r = (r + 1)%node;
hash[p] = true;
}
}
}
}
return (dist[T] < INF);
}
void addnode(int x)
{
int y = ++ind;
cnt[x] ++, set[y] = x;
REP(i, 1, n)
{
int c = cnt[x]*t[i][x];
newedge(i, y, p[i], c);
}
newedge(y, T, 1, 0);
}
int change(int a,int flow)
{
if(a != S)
{
flow = std::min(flow, edge[fd[a]].cap);
flow = change(fr[a], flow);
if(!flag[a])
{
addnode(set[a]);
flag[a] = true;
}
edge[fd[a]].cap -= flow;
edge[fd[a]^1].cap += flow;
}
return flow;
}
void init()
{
read(n), read(m);
REP(i, 1, n) read(p[i]);
REP(i, 1, n)
REP(j, 1, m)
read(t[i][j]);
}
void prework()
{
S = n + 1, ind = T = n + 2;
REP(i, 1, n) newedge(S, i, p[i], 0);
REP(i, 1, ind) flag[i] = true;
REP(i, 1, m) addnode(i);
}
int greedy()
{
static int poi[maxp];
int calc = 0, tot = 0;
REP(i, 1, n)
{
REP(j, 1, n) poi[tot + j] = t[i][1];
tot += p[i];
}
std::sort(poi + 1, poi + tot + 1, std::greater<int>());
REP(i, 1, tot) calc += poi[i] * i;
return calc;
}
int solve()
{
if(m == 1) return greedy();
int mincost = 0, maxflow = 0;
while(SPFA())
{
int f = change(T, INF);
mincost += dist[T]*f;
maxflow += f;
}
return mincost;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("2879.in","r",stdin);
freopen("2879.out","w",stdout);
#endif
init();
prework();
write(solve());
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}