[ACM] POJ 3686 The Windy ' s (binary graph min Right match, km algorithm, special build)

Source: Internet
Author: User

The windy ' s
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 4158 Accepted: 1777

Description

The windy's is a world famous toy factory, owns M Top-class workshop to make toys. This year, the manager receivesN orders for toys. The manager knows that every order would take different amount of hours in different workshops. More precisely, theI-th order would take Zij hours if the toys is making in theJ-th wo Rkshop. Moreover, each order's work must is 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 was the number of the test case. The first line of each test case contains the integers,n andm (1≤ n,m ≤50).
The next N lines each contain M integers, describing the matrixzij (1≤zij ≤100,000) There is a blank line before each test case.

Output

The For each test case output the answer to a single line. The result should is 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

Source

POJ founder Monthly contest–2008.08.31, windy7926778

Problem Solving Ideas:

Test instructions for n orders, m factories, the first order in the J Factory production time is t[i][j], a factory can produce multiple orders, but only one order at a time, that is, if the first production of a order, then B orders to wait until a production after the production, Ask n orders the minimum amount of time required to complete the production of this M factory.

ideas reproduced in: http://blog.csdn.net/lin375691011/article/details/19292473

There are some places on the map that need to think for a long time. Because the time spent on each order is the time the shop completes the order and the order waits. We are located in workshop a need to complete the K orders, the total time spent is t1+ (T1+T2) + (T1+T2+T3) ... Conversion is t1*k+t2* (k-1) +t3* (k-3) ... We found the rule: when the first order in the J workshop is the last K task, the total consumption time needs to add order I in the workshop corresponding to the consumption time K times.

Add: That is to say the M plant as a m point, each point is split into N points (because each factory can produce up to n orders), the break-up of the first point to represent an order in the factory is the bottom of the next I was produced.

g[i][k*n+j]=-t[i][k]* (j+1);//The first order is produced at the K-factory, and all orders produced in the K-factory are produced in the Countdown J.

0<=i<n, 0<=k<m, 0<=j<n

KM algorithm is to find the maximum weight obtained by the N-side, and in the subject of the minimum weight, the time taken to reverse, then the use of the KM algorithm to find the maximum weight of the opposite number is the minimum value we require.

Code:

#include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> #include < iomanip>using namespace Std;const int maxn=52;const int inf=0x3f3f3f3f;int n,m;int t[maxn][maxn];int nx,ny;int G[MAXN ][maxn*maxn];int linked[maxn*maxn],lx[maxn],ly[maxn*maxn];int Slack[maxn*maxn];bool VISX[MAXN],VISY[MAXN*MAXN];    BOOL DFS (int x) {visx[x]=true;        for (int y=0;y<ny;y++) {if (visy[y]) continue;        int tmp=lx[x]+ly[y]-g[x][y];            if (tmp==0) {visy[y]=true; if (linked[y]==-1| |                DFS (Linked[y])) {linked[y]=x;            return true;    }} else if (slack[y]>tmp) slack[y]=tmp; } return false;}    int KM () {memset (linked,-1,sizeof (linked));    memset (ly,0,sizeof (ly));        for (int i=0;i<nx;i++) {lx[i]=-inf;    for (int j=0;j<ny;j++) if (G[i][j]>lx[i]) lx[i]=g[i][j]; } for (int x=0;x<nx;x+ +) {for (int i=0;i<ny;i++) Slack[i]=inf;            while (true) {memset (visx,0,sizeof (VISX));            memset (visy,0,sizeof (Visy));            if (DFS (x)) break;            int d=inf;            for (int i=0;i<ny;i++) if (!visy[i]&&d>slack[i]) d=slack[i];            for (int i=0;i<nx;i++) if (visx[i]) Lx[i]-=d;                for (int i=0;i<ny;i++) {if (visy[i]) Ly[i]+=d;            else Slack[i]-=d;    }}} int ans=0;    for (int i=0;i<ny;i++) if (linked[i]!=-1) ans+=g[linked[i]][i]; Return-ans;}    int main () {int cas;    cin>>cas;        while (cas--) {cin>>n>>m;        for (int i=0;i<n;i++) for (int j=0;j<m;j++) cin>>t[i][j];      for (int i=0;i<n;i++) for (int j=0;j<n;j++)          for (int k=0;k<m;k++) g[i][k*n+j]=-t[i][k]* (j+1);//The first order is produced at the K-factory, and all orders produced in the K factory are        Production m=n*m;        Nx=n;ny=m;    Cout<<setiosflags (ios::fixed) <<setprecision (6) <<1.0*km ()/n<<endl; } return 0;}


[ACM] POJ 3686 The Windy ' s (binary graph min Right match, km algorithm, special build)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.