POJ 3686 The Windy's

Source: Internet
Author: User

The Windy's
Time Limit: 5000 MS Memory Limit: 65536 K
Total Submissions: 3299 Accepted: 1413

Description

The Windy's is a world famous toy factory that owns M top-class workshop to make toys. this year the manager has es 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 shocould be rounded to six decimal places.

Sample Input

3

3 4
100 100 100 1
99 99 99 1
98 98 98 1

3 4
1 100 100 100
99 1 99 99
98 98 1 98

3 4
1 100 100 100
1 99 99
98 1 98 98
Sample Output

2.000000
1.000000
1.333333

Source

POJ Founder Monthly Contest-2008.08.31, windy7926778
[Cpp]
/*************************************** ************************
> File Name: poj1_6.cpp
> Author: SDUT_GYX
> Mail: 2272902662@qq.com
> Created Time: 0:16:38
**************************************** **********************/
 
# Include <iostream>
# Include <cstring>
# Include <algorithm>
# Include <cstdio>
# Include <queue>
# Include <cstdlib>
# Include <iomanip>
# Include <string>
# Include <vector>
# Include <map>
# Include <cmath>
# Include <stack>
# Define LL long
# Define N 3600
# Define inf 0x7ffffff
Using namespace std;
Int lx [N], ly [N], slack [N], linky [N], w [N] [N], n, m;
Bool visitx [N], visity [N];
Int main ()
{
// Freopen ("data1.in", "r", stdin );
Int KM ();
Int t;
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d", & n, & m );
Int x;
For (int I = 1; I <= n; I ++)
{
For (int j = 1; j <= m; j ++)
{
Scanf ("% d", & x );
For (int k = 1; k <= n; k ++)
{
W [I] [(J-1) * n + k] =-x * k;
}
}
}
M = n * m;
Int res = KM ();
Printf ("%. 6lf \ n", (double) res/n );
}
Return 0;
}
Bool find (int k)
{
Visitx [k] = true;
For (int I = 1; I <= m; I ++)
{
If (visity [I])
{
Continue;
}
Int t = lx [k] + ly [I]-w [k] [I];
If (t = 0)
{
Visity [I] = true;
If (! Linky [I] | find (linky [I])
{
Linky [I] = k;
Return true;
}
} Else
{
Slack [I] = min (slack [I], t );
}
}
Return false;
}
Int KM ()
{
Memset (ly, 0, sizeof (ly ));
Memset (linky, 0, sizeof (linky ));
For (int I = 1; I <= n; I ++)
{
Lx [I] =-inf;
}
For (int I = 1; I <= n; I ++)
{
For (int j = 1; j <= m; j ++)
{
Lx [I] = max (lx [I], w [I] [j]);
}
}
For (int I = 1; I <= n; I ++)
{
For (int j = 1; j <= m; j ++)
{
Slack [j] = inf;
}
While (1)
{
Memset (visitx, false, sizeof (visitx ));
Memset (visity, false, sizeof (visity ));
If (find (I ))
{
Break;
}
Int d = inf;
For (int I = 1; I <= m; I ++)
{
If (! Visity [I])
{
D = min (d, slack [I]);
}
}
For (int I = 1; I <= n; I ++)
{
If (visitx [I])
{
Lx [I]-= d;
}
}
For (int I = 1; I <= m; I ++)
{
If (visity [I])
{
Ly [I] + = d;
} Else
{
Slack [I]-= d;
}
}
}
}
Int s = 0;
For (int I = 1; I <= m; I ++)
{
If (linky [I])
{
Int j = linky [I];
S + = w [j] [I];
}
}
Return-s;
}

/*************************************** ************************
> File Name: poj1_6.cpp
> Author: SDUT_GYX
> Mail: 2272902662@qq.com
> Created Time: 0:16:38
**************************************** **********************/

# Include <iostream>
# Include <cstring>
# Include <algorithm>
# Include <cstdio>
# Include <queue>
# Include <cstdlib>
# Include <iomanip>
# Include <string>
# Include <vector>
# Include <map>
# Include <cmath>
# Include <stack>
# Define LL long
# Define N 3600
# Define inf 0x7ffffff
Using namespace std;
Int lx [N], ly [N], slack [N], linky [N], w [N] [N], n, m;
Bool visitx [N], visity [N];
Int main ()
{
// Freopen ("data1.in", "r", stdin );
Int KM ();
Int t;
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d", & n, & m );
Int x;
For (int I = 1; I <= n; I ++)
{
For (int j = 1; j <= m; j ++)
{
Scanf ("% d", & x );
For (int k = 1; k <= n; k ++)
{
W [I] [(J-1) * n + k] =-x * k;
}
}
}
M = n * m;
Int res = KM ();
Printf ("%. 6lf \ n", (double) res/n );
}
Return 0;
}
Bool find (int k)
{
Visitx [k] = true;
For (int I = 1; I <= m; I ++)
{
If (visity [I])
{
Continue;
}
Int t = lx [k] + ly [I]-w [k] [I];
If (t = 0)
{
Visity [I] = true;
If (! Linky [I] | find (linky [I])
{
Linky [I] = k;
Return true;
}
} Else
{
Slack [I] = min (slack [I], t );
}
}
Return false;
}
Int KM ()
{
Memset (ly, 0, sizeof (ly ));
Memset (linky, 0, sizeof (linky ));
For (int I = 1; I <= n; I ++)
{
Lx [I] =-inf;
}
For (int I = 1; I <= n; I ++)
{
For (int j = 1; j <= m; j ++)
{
Lx [I] = max (lx [I], w [I] [j]);
}
}
For (int I = 1; I <= n; I ++)
{
For (int j = 1; j <= m; j ++)
{
Slack [j] = inf;
}
While (1)
{
Memset (visitx, false, sizeof (visitx ));
Memset (visity, false, sizeof (visity ));
If (find (I ))
{
Break;
}
Int d = inf;
For (int I = 1; I <= m; I ++)
{
If (! Visity [I])
{
D = min (d, slack [I]);
}
}
For (int I = 1; I <= n; I ++)
{
If (visitx [I])
{
Lx [I]-= d;
}
}
For (int I = 1; I <= m; I ++)
{
If (visity [I])
{
Ly [I] + = d;
} Else
{
Slack [I]-= d;
}
}
}
}
Int s = 0;
For (int I = 1; I <= m; I ++)
{
If (linky [I])
{
Int j = linky [I];
S + = w [j] [I];
}
}
Return-s;
}

 

 

Related Keywords:

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.