bzoj1070 [SCOI2007]修车

Description

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

Input

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

Output

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

Sample Input

2 2
3 2
1 4

Sample Output

1.50

HINT

数据范围: (2<=M<=9,1<=N<=60), (1<=T<=1000)

正解:最小费用最大流。

考虑把工人按时间拆点,每个工人拆成1*t,2*t...n*t共n个时间,每辆汽车向每个工人的第k个时间段连费用为t*k,容量为1的边。其他边容量也是1,费用为0。

 1 //It is made by wfj_2048~
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <complex>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <cmath>
10 #include <queue>
11 #include <stack>
12 #include <map>
13 #include <set>
14 #define inf (1<<30)
15 #define il inline
16 #define RG register
17 #define ll long long
18 #define c(j,k) ( j*n+k )
19 
20 using namespace std;
21 
22 struct edge{ int nt,to,flow,cap,cost; }g[1000010];
23 
24 int head[100010],dis[100010],f[100010],fa[100010],p[100010],q[1000010],n,m,S,T,flow,cost,num=1;
25 
26 il int gi(){
27     RG int x=0,q=1; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
28     if (ch=='-') q=-1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q*x;
29 }
30 
31 il void insert(RG int from,RG int to,RG int cap,RG int cost){ g[++num]=(edge){head[from],to,0,cap,cost},head[from]=num; }
32 
33 il int bfs(RG int S,RG int T){
34     for (RG int i=1;i<=T;++i) dis[i]=inf;
35     RG int h=0,t=1; q[t]=S,dis[S]=0,f[S]=inf;
36     while (h<t){
37     RG int x=q[++h];
38     for (RG int i=head[x];i;i=g[i].nt){
39         RG int v=g[i].to;
40         if (dis[v]>dis[x]+g[i].cost && g[i].cap>g[i].flow){
41         dis[v]=dis[x]+g[i].cost,q[++t]=v,fa[v]=x,p[v]=i;
42         f[v]=min(f[x],g[i].cap-g[i].flow);
43         }
44     }
45     }
46     if (dis[T]==inf) return 0; flow+=f[T],cost+=f[T]*dis[T];
47     for (RG int x=T;x!=S;x=fa[x]) g[p[x]].flow+=f[T],g[p[x]^1].flow-=f[T];
48     return 1;
49 }
50 
51 il int mcmf(RG int S,RG int T){ flow=0,cost=0; while (bfs(S,T)); return cost; }
52 
53 il void work(){
54     m=gi(),n=gi(); RG int t; S=(m+1)*n+1,T=S+1;
55     for (RG int i=1;i<=n;++i) insert(S,i,1,0),insert(i,S,0,0);
56     for (RG int i=1;i<=n;++i)
57     for (RG int j=1;j<=m;++j){
58         t=gi();
59         for (RG int k=1;k<=n;++k) insert(i,c(j,k),1,t*k),insert(c(j,k),i,0,-t*k);
60     }
61     for (RG int i=1;i<=m;++i)
62     for (RG int j=1;j<=n;++j) insert(c(i,j),T,1,0),insert(T,c(i,j),0,0);
63     printf("%0.2lf
",1.0*mcmf(S,T)/n); return;
64 }
65 
66 int main(){
67     work();
68     return 0;
69 }
原文地址:https://www.cnblogs.com/wfj2048/p/6436766.html