山东济南彤昌机械科技有限公司 山东济南江鹏工贸游有限公司

bzoj 1070 [SCOI2007]修车(最小费用最大流)

1070: [SCOI2007]修车

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 3515  Solved: 1411
[Submit][Status][Discuss]

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)

Source

 

【思路】

       最小费用最大流。

       类似于白书的Fixed Partition Memory Management 。对于一个修车人员而言,k个车需要等待的总时间为: t1+t1*t2+t1+t2+t3+…t1+..tk =>t1*k+t2(k-1)+…tk,于是考虑将m个人每个人分别拆成m个点表示该车作为倒数第几个被修。由第i个人拆分成的第k个点向第j辆车连边(1,k*t[j][i]),由S向n*m个点连边,由车向T连边。由于是最小费用流所以不会出现“越级”的情况。

   ps:这时间卡的惊心动魄=-=。

【代码】

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<vector>
 5 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
 6 using namespace std;
 7 
 8 typedef long long LL ;
 9 const int maxn = 1000+10;
10 const int INF = 1e9;
11 
12 struct Edge{ int u,v,cap,flow,cost;
13 };
14 
15 struct MCMF {
16     int n,m,s,t;
17     int inq[maxn],a[maxn],d[maxn],p[maxn];
18     vector<int> G[maxn];
19     vector<Edge> es;
20     
21     void init(int n) {
22         this->n=n;
23         es.clear();
24         for(int i=0;i<n;i++) G[i].clear();
25     }
26     void AddEdge(int u,int v,int cap,int cost) {
27         es.push_back((Edge){u,v,cap,0,cost});
28         es.push_back((Edge){v,u,0,0,-cost});
29         m=es.size();
30         G[u].push_back(m-2);
31         G[v].push_back(m-1);
32     }
33     
34     bool SPFA(int s,int t,int& flow,LL& cost) {
35         for(int i=0;i<n;i++) d[i]=INF;
36         memset(inq,0,sizeof(inq));
37         d[s]=0; inq[s]=1; p[s]=0; a[s]=INF; 
38         queue<int> q; q.push(s);
39         while(!q.empty()) {
40             int u=q.front(); q.pop(); inq[u]=0;
41             for(int i=0;i<G[u].size();i++) {
42                 Edge& e=es[G[u][i]];
43                 int v=e.v;
44                 if(e.cap>e.flow && d[v]>d[u]+e.cost) {
45                     d[v]=d[u]+e.cost;
46                     p[v]=G[u][i];
47                     a[v]=min(a[u],e.cap-e.flow);        //min(a[u],..)
48                     if(!inq[v]) { inq[v]=1; q.push(v);
49                     }
50                 }
51             }
52         }
53         if(d[t]==INF) return false;
54         flow+=a[t] , cost+=(LL) a[t]*d[t];
55         for(int x=t; x!=s; x=es[p[x]].u) {
56             es[p[x]].flow+=a[t]; es[p[x]^1].flow-=a[t];
57         }
58         return true;
59     }
60     int Mincost(int s,int t,LL& cost) {
61         int flow=0; cost=0;
62         while(SPFA(s,t,flow,cost)) ;
63         return flow;
64     }
65 } mc;
66 
67 int n,m;
68 int t[maxn][maxn];
69 
70 int main() {
71     scanf("%d%d",&n,&m);
72     FOR(i,1,m) FOR(j,1,n)
73       scanf("%d",&t[i][j]);
74     mc.init(m*n+m+3);
75     int S=0,T=n*m+m+2;
76     FOR(i,1,n*m)
77         mc.AddEdge(0,i,1,0);
78     FOR(i,n*m+1,n*m+m)
79         mc.AddEdge(i,T,1,0);
80     FOR(i,1,n) FOR(j,1,m) FOR(k,1,m)
81         mc.AddEdge((i-1)*m+j,n*m+k,1,t[k][i]*j);
82     LL cost;
83     mc.Mincost(S,T,cost);
84     printf("%.2lf",(double)cost/m);
85     return 0;
86 }

 

posted on 2015-12-22 16:48  hahalidaxin  阅读(291)  评论(0编辑  收藏  举报