需要一定思考才能建出模型的费用流。
设有n辆车,m个维修人员
每辆车看作一个点u,连边s -> u,容量为1,费用为0
然后将每个维修人员拆成n个点,连边u -> v,容量为1,费用为k * time[u][v](k表示拆分的n个点中的第k个点),实际意义为第v个维修人员在倒数第k个位置修第u辆车
最后对于所有与维修人员相关的点,连边v -> t,容量为1,费用为0
做一次最小费用最大流就是最小等待时间了,题目要求平均最小时间,再除以总人数n就行了
#include<cstdio>
#include<cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxp = 100;
const int maxn = 1000;
const int maxm = 200000;
struct Edge
{
int pos,c,d,w;
int next;
}E[maxm];
int head[maxn],time[maxp][maxp];
int dis[maxn],pre[maxn];
int que[maxm];
bool vis[maxn];
int s,t,NE;
int n,m;
void init()
{
freopen("bzoj1070.in","r",stdin);
freopen("bzoj1070.out","w",stdout);
}
void add(int u,int v,int c,int w)
{
E[NE].pos = v;E[NE].d = u;E[NE].c = c;E[NE].w = w;
E[NE].next = head[u];head[u] = NE++;
E[NE].pos = u;E[NE].d = v;E[NE].c = 0;E[NE].w = -w;
E[NE].next = head[v];head[v] = NE++;
}
void readdata()
{
scanf("%d%d",&m,&n);
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= m;j++)
{
scanf("%d",&time[i][j]);
}
}
}
void build_map()
{
memset(head,-1,sizeof(head));
memset(E,0,sizeof(E));
s = 0,t = n * (m + 1) + 1;
NE = 0;
for(int i = 1;i <= n;i++)add(s,i,1,0);
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= m;j++)
for(int k = 1;k <= n;k++)
add(i,j * n + k,1,k * time[i][j]);
}
for(int i = n + 1;i < t;i++)add(i,t,1,0);
}
bool spfa()
{
memset(dis,0x3f,sizeof(dis));
memset(vis,false,sizeof(vis));
memset(pre,-1,sizeof(pre));
int l = 0,r = 0;
dis[s] = 0;
que[r++] = s;
vis[s] = true;
while(l < r)
{
int u = que[l++];
vis[u] = false;
for(int i = head[u];i != -1;i = E[i].next)
{
int v = E[i].pos;
if(E[i].c && dis[u] + E[i].w < dis[v])
{
pre[v] = i;
dis[v] = dis[u] + E[i].w;
if(!vis[v])
{
que[r++] = v;
vis[v] = true;
}
}
}
}
if(dis[t] == inf)return false;
else return true;
}
int MCMF()
{
int ret = 0;
while(spfa())
{
int min = inf;
int u = t;
while(u != s)
{
if(E[pre[u]].c < min)min = E[pre[u]].c;
u = E[pre[u]].d;
}
u = t;
while(u != s)
{
E[pre[u]].c -= min;
E[pre[u]^1].c += min;
u = E[pre[u]].d;
}
ret += min * dis[t];
}
return ret;
}
void solve()
{
build_map();
double ans = (double)MCMF() / (double)n;
printf("%.2lf\n",ans);
}
int main()
{
init();
readdata();
solve();
return 0;
}