這題的建圖實在是太神了
假設某個機器處理了k個玩具,那麼對於這些玩具,有兩種時間,一種是真正處理的時間,一種是等待的時間,等待的時間就是之前所有處理的玩具的時間,
假設這k個玩具真正用在加工的時間分為a1,a2,a3...ak, 那麼每個玩具實際的時間是加工的時間+等待時間,分別為
a1, a1+a2, a1+a2+a3.......a1+a2+...ak
求和之後變為 a1 *k + a2 * (k - 1) + a3 * (k - 2).... + ak
這時就發現,每個玩具之間的實際時間可以分開來算 然後求和了。
因為對每個機器,最多可以處理n個玩具,所以可以拆成n個點,1~n分別代表某個玩具在這個機器上倒數第幾個被加工的
所以我們對於每個玩具i,機器j中拆的每個點k,連接一條z[i][j]*k權值的邊。
然後求最小權匹配即可
[cpp]
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define eps 1e-5
#define MAXN 55
#define MAXM 55555
#define INF 100000007
using namespace std;
int n, m, ny, nx;
int w[MAXN][2555];
int lx[MAXN], ly[2555];
int linky[2555];
int visx[MAXN], visy[2555];
int slack[2555];
bool find(int x)
{
visx[x] = 1;
for(int y = 1; y <= ny; y++)
{
if(visy[y]) continue;
int t = lx[x] + ly[y] - w[x][y];
if(t == 0)
{
visy[y] = 1;
if(linky[y] == -1 || find(linky[y]))
{
linky[y] = x;
return true;
}
}
else if(slack[y] > t) slack[y] = t;
}
return false;
}
int KM()
{
memset(linky, -1, sizeof(linky));
for(int i = 1; i <= nx; i++) lx[i] = -INF;
memset(ly, 0, sizeof(ly));
for(int i = 1; i <= nx; i++)
for(int j = 1; j <= ny; j++)
if(w[i][j] > lx[i]) lx[i] = w[i][j];
for(int x = 1; x <= nx; x++)
{
for(int i = 1; i <= ny; i++) slack[i] = INF;
while(true)
{
memset(visx, 0, sizeof(visx));
memset(visy, 0, sizeof(visy));
if(find(x)) break;
int d = INF;
for(int i = 1; i <= ny; i++)
if(!visy[i]) d = min(d, slack[i]);
if(d == INF) return -1;
for(int i = 1; i <= nx; i++)
if(visx[i]) lx[i] -=d;
for(int i = 1; i <= ny; i++)
if(visy[i]) ly[i] += d;
else slack[i] -= d;
}
}
int tp = 0;
for(int i = 1; i <= ny; i++)
if(linky[i] != -1) tp += w[linky[i]][i] - 5000000;
return -tp;
}
int a[MAXN][MAXN];
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(w, 0, sizeof(w));
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%d", &a[i][j]);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
for(int k = 1; k <= n; k++)
w[i][(j - 1) * n + k] = 5000000 - a[i][j] * k;
nx = n;
ny = n * m;
double ans = 1.0 * KM() / n;
printf("%f\n", ans);
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define eps 1e-5
#define MAXN 55
#define MAXM 55555
#define INF 100000007
using namespace std;
int n, m, ny, nx;
int w[MAXN][2555];
int lx[MAXN], ly[2555];
int linky[2555];
int visx[MAXN], visy[2555];
int slack[2555];
bool find(int x)
{
visx[x] = 1;
for(int y = 1; y <= ny; y++)
{
if(visy[y]) continue;
int t = lx[x] + ly[y] - w[x][y];
if(t == 0)
{
visy[y] = 1;
if(linky[y] == -1 || find(linky[y]))
{
linky[y] = x;
return true;
}
}
else if(slack[y] > t) slack[y] = t;
}
return false;
}
int KM()
{
memset(linky, -1, sizeof(linky));
for(int i = 1; i <= nx; i++) lx[i] = -INF;
memset(ly, 0, sizeof(ly));
for(int i = 1; i <= nx; i++)
for(int j = 1; j <= ny; j++)
if(w[i][j] > lx[i]) lx[i] = w[i][j];
for(int x = 1; x <= nx; x++)
{
for(int i = 1; i <= ny; i++) slack[i] = INF;
while(true)
{
memset(visx, 0, sizeof(visx));
memset(visy, 0, sizeof(visy));
if(find(x)) break;
int d = INF;
for(int i = 1; i <= ny; i++)
if(!visy[i]) d = min(d, slack[i]);
if(d == INF) return -1;
for(int i = 1; i <= nx; i++)
if(visx[i]) lx[i] -=d;
for(int i = 1; i <= ny; i++)
if(visy[i]) ly[i] += d;
else slack[i] -= d;
}
}
int tp = 0;
for(int i = 1; i <= ny; i++)
if(linky[i] != -1) tp += w[linky[i]][i] - 5000000;
return -tp;
}
int a[MAXN][MAXN];
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(w, 0, sizeof(w));
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%d", &a[i][j]);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
for(int k = 1; k <= n; k++)
w[i][(j - 1) * n + k] = 5000000 - a[i][j] * k;
nx = n;www.2cto.com
ny = n * m;
double ans = 1.0 * KM() / n;
printf("%f\n", ans);
}
return 0;
}