题意:有N个玩具,M个玩具厂。给出每个玩具厂加工每个玩具的时间。每个工厂只能同时加工一个玩具。求玩具加工分配方案,使每个玩具的平均加工时间最小。
思路:因为玩具的个数是个定值,所以我们只需最小化加工所有玩具的总时间就可以。
我们先考虑一个厂加工完所有玩具的情况。
假设加工i号玩具所用的时间为Zi,当我们按照a1,a2,...,an的顺序加工玩具时,需要的时间是:
T = Za1 +(Za1 + Za2) + ... + (Za1 + Za2 + ... + Zan) = n * Za1 + (n-1) * Za2 +... + Zan;
可以发现,我们只需贪心的从花费时间最小的玩具开始加工,就可以得到最小的总的加工时间。
但是对于多个厂来加工玩具的问题,这样贪心就不可以了。
我们重新看一下求出的式子,除了可以看做一个厂加工多个玩具之外,还可以看做多个只能加工一个玩具的厂来加工这些玩具,只不过花费的时间将改变为1-N倍。
这样,我们就把原问题变成了一般的指派问题。
代码如下:
#include <cstdio>
#include <algorithm>
#include <queue>
#include <utility>
#include <cstring>
using namespace std;
typedef pair<int,int> pii;
template<class T>
inline bool read(T &n){
T x = 0, tmp = 1; char c = getchar();
while ((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if (c == EOF) return false;
if (c == '-') c = getchar(), tmp = -1;
while (c >= '0' && c <= '9') x *= 10, x += (c - '0'), c = getchar();
n = x*tmp;
return true;
}
struct edge{
int from,to,cap,flow,cost;
edge(int u=0, int v=0, int c=0, int f=0, int w=0):
from(u),to(v),cap(c),flow(f),cost(w){}
};
struct zkw_flow{
static const int MAX = 500000;
static const int INF = 0x3f3f3f3f;
edge edges[2 * MAX];
int head[MAX],next[MAX],tot;
int dis[MAX];
bool used[MAX];
int src,sink,n;
int cost,flow;
priority_queue<pii> Q;
void init(int n){
this->n = n;
memset(head,-1,sizeof(head));
tot = 0;
}
void addedge(int u, int v, int c, int w){
edges[tot] = edge(u,v,c,0,w);
next[tot] = head[u], head[u] = tot++;
edges[tot] = edge(v,u,0,0,-w);
next[tot] = head[v], head[v] = tot++;
}
void spfa(){
memset(dis,0x3f,sizeof(dis));
dis[src] = 0; Q.push(make_pair(0,src));
while(!Q.empty()){
pii p = Q.top();Q.pop();
int u = p.second, d = -p.first;
if(dis[u] != d) continue;
for(int v = head[u]; ~v ; v = next[v]){
edge & e = edges[v];
if(e.cap > e.flow && dis[e.to] > d + e.cost){
dis[e.to] = d + e.cost;
Q.push(make_pair(-dis[e.to],e.to));
}
}
}
for(int i = 0 ; i < n; ++i)
dis[i] = dis[sink] - dis[i];
}
int augment(int u,int f){
if(u == sink){
flow += f;
cost += dis[src] * f;
return f;
}
used[u] = true;
int now = f;
for(int v = head[u]; ~v; v = next[v]){
edge & e = edges[v];
if(e.cap > e.flow && !used[e.to] &&
dis[u] == dis[e.to] + e.cost){
int tmp = augment(e.to,min(now,e.cap - e.flow));
e.flow += tmp;
edges[v^1].flow -= tmp;
now -= tmp;
if(now == 0) break;
}
}
return f - now;
}
bool modify(){
int d = INF;
for(int u = 0 ; u < n; ++u) if(used[u])
for(int v = head[u]; ~v; v = next[v]){
edge&e = edges[v];
if(e.cap > e.flow && !used[e.to])
d = min(d, dis[e.to] + e.cost - dis[u]);
}
if(d == INF) return false;
for(int i = 0; i < n; ++i)
if(used[i]) dis[i] += d;
return true;
}
int mincost(int s,int t){
src = s, sink = t;
flow = cost = 0;
spfa();
for(;;){
for(;;){
memset(used,0,sizeof(used));
if(!augment(src,INF)) break;
}
if(!modify()) break;
}
return cost;
}
} solver;
int T;
int N, M;
int c;
int main(void)
{
//freopen("input.txt","r",stdin);
read(T);
while (T--){
read(N), read(M);
int s = 0, t = N + N * M + 1;
solver.init(N + N * M + 2);
for (int i = 1; i <= N; ++i){
solver.addedge(s, i, 1, 0);
for (int j = 1; j <= M; ++j){
read(c);
for (int k = 1; k <= N; ++k){
solver.addedge(i, j * N + k, 1, k * c);
}
}
}
for (int i = 1; i <= M; ++i)
for (int j = 1; j <= N; ++j)
solver.addedge(i * N + j, t, 1, 0);
printf("%f\n", (double)solver.mincost(s, t) / N);
}
return 0;
}