POJ 3686 The windy ' s (thinking + cost flow good question)

Source: Internet
Author: User

The windy ' s
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5362 Accepted: 2249

Description

The windy's is a world famous toy factory, owns M Top-class workshop to make toys. This year, the manager receives N orders for toys. The manager knows that every order would take different amount of hours in different workshops. More precisely, the I-th order would take zij hours if the toys is making in the J-th Workshop. 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 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

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

Topic Connection: POJ 3686

Washed book network flow problem to see, with a missile launched the problem is very similar, but this problem different factory to different items have different processing time, according to the processing order to split is very easy to think of, but such a want to have a problem, such as I put the item to factory 2 second processing time point, then I this edge flow is 1, The cost is how much do not know, because I do not know the factory 2 the first processing of the side of the cost is how much, that is not aware of the current item processing wait for how long, so need to change a way of thinking.

For example, there are three items a,b,c are processed in the same factory, so the time can be written: TA+TA+TB+TA+TB+TC=3TA+2TB+1TC, obviously the first processing is a, followed by B, and finally C, can be found that the maximum value of the front of the item is the total number of items processed in the factory, Then this question is actually a coefficient allocation problem, then the map is good to do, the cost is the processing order * processing time, because the flow is 1, did solve the same time can only process an item of the problem, but may also want to, that one of my items in the factory 1 in the first order of K processing, But is it not a waste of the fact that the factory did not use the k-1 once before 1? In fact, because the use of SPFA is the shortest-path augmentation, the solution is to find the total time optimal situation, will not appear in front of the empty processing order, if there is empty processing order, then SPFA will find and put this edge slack into the network stream

Code:

#include <stdio.h> #include <iostream> #include <algorithm> #include <cstdlib> #include < sstream> #include <numeric> #include <cstring> #include <bitset> #include <string> #include <deque> #include <stack> #include <cmath> #include <queue> #include <set> #include <map >using namespace std, #define INF 0x3f3f3f3f#define LC (x) (x<<1) #define RC (x) ((x<<1) +1) #define MID (x, y) ( (x+y) >>1) #define CLR (Arr,val) memset (arr,val,sizeof (arr)) #define FAST_IO Ios::sync_with_stdio (false); Cin.tie  (0); typedef pair<int, int> pii;typedef long long ll;const double PI = ACOs ( -1.0); const int N = 55;const int MAXV = N    + n * N;const int maxe = n + n * n + n * n * n;struct edge{int to, NXT, cap, cost; Edge () {} edge (int _to, int _nxt, int _cap, int _cost): to (_to), NXT (_NXT), Cap (_CAP), cost (_cost) {}};edge E[maxe < < 1];int HEAD[MAXV], Tot;int VIS[MAXV], PRE[MAXV], PATH[MAXV], D[maxv];int z[n][n];intMC, Mf;void Init () {CLR (head,-1);    tot = 0; MC = MF = 0;}    inline void Add (int s, int t, int cap, int cost) {E[tot] = Edge (t, Head[s], cap, cost);    Head[s] = tot++;    E[tot] = Edge (S, Head[t], 0,-cost); Head[t] = tot++;}    int SPFA (int s, int t) {queue<int>q;    Q.push (s);    CLR (d, INF);    CLR (Vis, 0);    D[s] = 0;    Vis[s] = 1; while (!        Q.empty ()) {int u = q.front ();        Q.pop ();        Vis[u] = 0;            for (int i = head[u]; ~i; i = e[i].nxt) {int v = e[i].to;                if (D[v] > D[u] + e[i].cost && e[i].cap > 0) {d[v] = D[u] + e[i].cost;                PRE[V] = u;                PATH[V] = i;                    if (!vis[v]) {vis[v] = 1;                Q.push (v); }}}}} return d[t]! = INF;}    void MCMF (int s, int t) {int i;        while (SPFA (s, t)) {int Min = INF; for (i = t; I! = s; i = Pre[i]) min = min (min, e[path[i]].cap);            for (i = t; I! = s; i = Pre[i]) {e[path[i]].cap = Min;        E[path[i] ^ 1].cap + = Min;        } MF + = Min;    MC + = Min * D[t];    }}int Main (void) {int tcase, n, M, I, J, K;    scanf ("%d", &tcase);        while (tcase--) {init ();        scanf ("%d%d", &n, &m);        for (i = 1; I <= n; ++i) for (j = 1; j <= m; ++j) scanf ("%d", &z[i][j]);        int S = 0, T = n + n * m + 1;        for (i = 1; I <= n; ++i)//n source point to the desired processing item add (S, I, 1, 0);        for (i = n + 1; I <= n + n * m; ++i)//n*m processing time point to Meeting Point Add (I, T, 1, 0); for (i = 1; I <= N, ++i) {for (j = 1; j <= m; ++j) {for (k = 1; k < = N;                    ++K)//Traverse different time points {int id = J * n + K; Add (i, ID, 1, k * Z[i][j]); N*m*n}}} MCMF (S, T);    printf ("%.6f\n", MC * 1.0/n); } return 0;}

POJ 3686 The windy ' s (thinking + cost flow good question)

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.