[费用流]洛谷 P2053 修车

题目描述

同一时刻有N位车主带着他们的爱车来到了汽车维修中心。维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的。现在需要安排这M位技术人员所维修的车及顺序,使得顾客平均等待的时间最小。

说明:顾客的等待时间是指从他把车送至维修中心到维修完毕所用的时间。

输入输出格式

输入格式:

第一行有两个数M,N,表示技术人员数与顾客数。

接下来n行,每行m个整数。第i+1行第j个数表示第j位技术人员维修第i辆车需要用的时间T。

输出格式:

最小平均等待时间,答案精确到小数点后2位。

输入输出样例

输入样例#1: 
2 2
3 2
1 4
输出样例#1: 
1.50

说明

(2<=M<=9,1<=N<=60), (1<=T<=1000)

题解

  • 明显这题要求平均时间最短,就等同于要求总时间最短
  • 我们考虑一下构图方式:
  • 把n台车每个拆成m个点,然后再建m个点表示工人,一共就是n*m+m个点(不包括s,t)
  • 那么可以从起始点向(i-1)*m+j连一条费用为0的边,流量为1。(也就是按排哪台车给那个工人)
  • 我们考虑工人j倒数修的倒数第k辆车为i,那么i对答案的贡献就为a[j,i]*k
  • 那么对于工人i的第j个节点,则在该节点与每个汽车节点之间连上一条流量为1费用为time*j的边就好了

代码

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N=10005;
const int inf=0x3f3f3f3f;
int n,m,cnt,last[N],t,dis[N],f[N],d[N],ans,s,v[N],q[N],a[100][100];
struct edge{int from,to,c,w,next,op;}e[N*100];
queue<int> Q;
void insert(int u,int v,int x,int y)
{
	e[++cnt].from=u; e[cnt].to=v; e[cnt].c=x; e[cnt].w=y; e[cnt].next=last[u]; last[u]=cnt; e[cnt].op=cnt+1;
	e[++cnt].from=v; e[cnt].to=u; e[cnt].c=0; e[cnt].w=-y; e[cnt].next=last[v]; last[v]=cnt; e[cnt].op=cnt-1;
}
bool spfa()
{
	for (int i=s;i<=t;i++)
	{
		dis[i]=inf;
		f[i]=d[i]=0;
	}
	dis[s]=0; f[s]=1; Q.push(s);
	while (!Q.empty())
	{
		int u=Q.front(); Q.pop();
		for (int i=last[u];i;i=e[i].next)
			if (e[i].c&&e[i].w+dis[u]<dis[e[i].to])
			{
				dis[e[i].to]=e[i].w+dis[u];
				d[e[i].to]=i;
				if (!f[e[i].to])
				{
					f[e[i].to]=1;
					Q.push(e[i].to);
				}
			}
		f[u]=0;
	}
	if (dis[t]<inf) return 1; else return 0;
	
}
void mcf()
{
	int mn=inf,x=t;
	while (d[x])
	{
		mn=min(mn,e[d[x]].c);
		x=e[d[x]].from;
	}
	ans+=mn*dis[t];
	x=t;
	while (d[x])
	{
		e[d[x]].c-=mn;
		e[e[d[x]].op].c+=mn;
		x=e[d[x]].from;
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++)
		for (int j=1;j<=n;j++)
			scanf("%d",&a[j][i]);
	s=0; t=n*m+m+1;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
		{
			insert(s,(i-1)*m+j,1,0);
			for (int k=1;k<=m;k++) insert((i-1)*m+j,n*m+k,1,a[i][k]*j);
		}
	for (int i=1;i<=m;i++) insert(n*m+i,t,1,0);
	while (spfa()) mcf();
	printf("%.2lf",(double)ans/m);
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/Comfortable/p/9213639.html