POJ 3686 最小费用最大流

来源:互联网 发布:成都医疗大数据公司 编辑:程序博客网 时间:2024/05/21 06:17
The Windy's
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 3166 Accepted: 1358

Description

The Windy's is a world famous toy factory that owns M top-class workshop to make toys. This year the manager receives N orders for toys. The manager knows that every order will take different amount of hours in different workshops. More precisely, the i-th order will take Zij hours if the toys are making in the j-th workshop. Moreover, each order's work must be wholly completed in the same workshop. And a workshop can not switch to another order until it has finished the previous one. The switch does not cost any time.

The manager wants to minimize the average of the finishing time of the N orders. Can you help him?

Input

The first line of input is the number of test case. The first line of each test case contains two integers, N and M (1 ≤ N,M ≤ 50). The next N lines each contain M integers, describing the matrix Zij (1 ≤ Zij ≤ 100,000) There is a blank line before each test case.

Output

For each test case output the answer on a single line. The result should be rounded to six decimal places.

Sample Input

33 4100 100 100 199 99 99 198 98 98 13 41 100 100 10099 1 99 9998 98 1 983 41 100 100 1001 99 99 9998 1 98 98

Sample Output

2.0000001.0000001.333333
思路:构图很巧妙,首先,拿sample 1来说,很明显三件玩具都要拿到第四个工作室才能保证所花费的时间最短。但是他们并不能在该工作室同时工作,也就是说必须先做完一个,那么另外一个必须到等他做完之后才能去
做。假设某工作室加工了k件玩具,每件玩具在该工作室需要加工的时间是a1,a2,a3...ak,将等待时间计算进去则有a1,a1+a2,a1+a2+a3,...,a1+a2+a3+...+ak;对这些多项式求和可以得到a1*k + a2*(k-1)
+a3*(k-2) + ... + ak-1*2 + ak;这就是k件玩具都在该工作室加工所需要的时间。因此,在有N个玩具,M个工作室的情况下,将某工作室拆成N个点,每个玩具都分别和这N个点相连,流量为1,费用为该玩具在该工
作室第几个加工时需要的时间,分别表示该玩具在该工厂里是第几个加工的。构造一个源点,将源点和每个玩具连边,流量为1,费用为0,构造一个汇点,将拆出来的N*M个点都和汇点相连,流量为1,费用为0.

View Code
  1 #include <iostream>  2 #include <cstdio>  3 #include <cstring>  4 #include <algorithm>  5 #include <queue>  6   7 using namespace std;  8   9 const int INF = 1<<30; 10 const int MAX = 5000; 11  12 struct node 13 { 14     int to,val,re,next,cost; 15 }; 16  17 node edge[535000]; 18 int head[MAX]; 19 int idx; 20  21 int Case,N,M; 22 int source,sink,pt; 23 int dis[MAX]; 24 bool inQueue[MAX]; 25 int pre[MAX],pos[MAX]; 26  27 void addNode(int from,int to,int val,int cost) 28 { 29     edge[idx].to = to; 30     edge[idx].val = val; 31     edge[idx].cost = cost; 32     edge[idx].re = idx + 1; 33     edge[idx].next = head[from]; 34     head[from] = idx ++; 35     edge[idx].to = from; 36     edge[idx].val = 0; 37     edge[idx].cost = -cost; 38     edge[idx].re = idx - 1; 39     edge[idx].next = head[to]; 40     head[to] = idx ++; 41 } 42  43 bool spfa() 44 { 45     for(int i=0; i<=pt; i++) 46     { 47         dis[i] = INF; 48         inQueue[i] = false; 49     } 50     queue <int> Q; 51     dis[source] = 0; 52     pre[source] = source; 53     Q.push(source); 54     inQueue[source] = true; 55     while(!Q.empty()) 56     { 57         int cur = Q.front(); 58         Q.pop(); 59         inQueue[cur] = false; 60         for(int i=head[cur]; i!=-1; i=edge[i].next) 61         { 62             int to = edge[i].to; 63             if(edge[i].val > 0 && dis[to] > dis[cur] + edge[i].cost) 64             { 65                 dis[to] = dis[cur] + edge[i].cost; 66                 pre[to] = cur; 67                 pos[to] = i; 68                 if(!inQueue[to]) 69                 { 70                     Q.push(to); 71                     inQueue[to] = true; 72                 } 73             } 74         } 75     } 76     if(pre[sink]!=-1 && dis[sink]<INF) 77         return true; 78     return false; 79 } 80  81 double end() 82 { 83     int flow = 0, cost = 0; 84     while(spfa()) 85     { 86         int Min = INF; 87         for(int i=sink; i!=source; i=pre[i]) 88             Min = min(Min,edge[pos[i]].val); 89         flow += Min; 90         cost += Min*dis[sink]; 91         for(int i=sink; i!=source; i=pre[i]) 92         { 93             edge[pos[i]].val -= Min; 94             edge[pos[i]^1].val += Min; 95         } 96     } 97     double ret = cost*1.0/N; 98     return ret; 99 }100 101 int main()102 {103     scanf("%d",&Case);104     while(Case --)105     {106         scanf("%d%d",&N,&M);107         idx = 0;108         memset(head,-1,sizeof(head));109         source = 0;110         sink = N+N*M+1;111         pt = sink + 1;112         for(int i=1; i<=N; i++)113             addNode(source,i,1,0);114         for(int i=1; i<=N; i++)115         {116             for(int j=1; j<=M; j++)117             {118                 int v;119                 scanf("%d",&v);120                 for(int k=1; k<=N; k++)121                     addNode(i,N+j+M*(k-1),1,v*(N-k+1));122             }123         }124         for(int i=N+1; i<=N+N*M; i++)125             addNode(i,sink,1,0);126         double ans = end();127         printf("%.6lf\n",ans);128     }129     return 0;130 }