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

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

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 99
98 1 98 98

Sample Output

2.000000
1.000000
1.333333

题目连接:POJ 3686

刷白书的网络流问题看到的,跟某一道导弹发射的问题很像,但是这题不同工厂对不同的物品都有不同的加工时间,按加工次序拆点是很容易想到, 但这样一想有一个问题,比如我把物品连到工场2的第二次加工时间点,那我这条边流量是1,费用是多少根本不知道,因为我不知道工厂2第一次加工的那条边费用是多少,即不知道当前物品加工时等待了多久,因此需要换一种思路。

比如有三个物品a,b,c都在同一个工厂加工,那么时间可以这么写:ta+ta+tb+ta+tb+tc=3ta+2tb+1tc,显然最先加工的是a,其次是b,最后是c,可以发现物品前面的系数的最大值就是在工厂加工的总物品数,那么这题实际上就是一个系数分配问题,那么建图就好办了, 费用就是加工次序*加工时间,由于流量是1,确实解决了同一时间只能加工一个物品的问题,但可能又会想,那我的某一个物品万一在工厂1的第k次序加工,但是实际上工厂1前k-1次机会都没有用过,这岂不是非常浪费吗?实际上由于用的是SPFA最短路增广,求解过程就是求出了总时间最优的情况,不会出现前面空着的加工次序问题,如果有空着的加工次序,那么SPFA一定会找到并把这条边松弛算进网络流中

代码:

#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];
int mc, 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源点到需加工物品
add(S, i, 1, 0);
for (i = n + 1; i <= n + n * m; ++i) //n*m加工时间点到汇点
add(i, T, 1, 0);
for (i = 1; i <= n; ++i)
{
for (j = 1; j <= m; ++j)
{
for (k = 1; k <= n; ++k)//遍历不同时间点
{
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(思维+费用流好题)的更多相关文章

  1. POJ 3686 The Windy's (费用流)

    [题目链接] http://poj.org/problem?id=3686 [题目大意] 每个工厂对于每种玩具的加工时间都是不同的, 并且在加工完一种玩具之后才能加工另一种,现在求加工完每种玩具的平均 ...

  2. POJ 3686 The Windy's 最小费用最大流

    每个工厂拆成N个工厂,费用分别为1~N倍原费用. //#pragma comment(linker, "/STACK:1024000000,1024000000") #includ ...

  3. poj 3422 Kaka's Matrix Travels 费用流

    题目链接 给一个n*n的矩阵, 从左上角出发, 走到右下角, 然后在返回左上角,这样算两次. 一共重复k次, 每个格子有值, 问能够取得的最大值是多少, 一个格子的值只能取一次, 取完后变为0. 费用 ...

  4. 2018.06.27The Windy's(费用流)

    The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6003 Accepted: 2484 Descripti ...

  5. POJ 2677 旅行商问题 双调dp或者费用流

    Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3408   Accepted: 1513 Description ...

  6. poj - 3686 The Windy's (KM算法)

    题意:n个订单和m个生产车间,每个订单在不同的车间生产所需要的时间不一样,并且每个订单只能在同一个车间中完成,直到这个车间完成这个订单就可以生产下一个订单.现在需要求完成n个订单的平均时间最少是多少. ...

  7. HDU 3376 &amp;&amp; 2686 方格取数 最大和 费用流裸题

    题意: 1.一个人从[1,1] ->[n,n] ->[1,1] 2.仅仅能走最短路 3.走过的点不能再走 问最大和. 对每一个点拆点限流为1就可以满足3. 费用流流量为2满足1 最大费用流 ...

  8. Lunch Time(费用流变型题,以时间为费用)

    Lunch Time http://acm.hdu.edu.cn/showproblem.php?pid=4807 Time Limit: 4000/2000 MS (Java/Others)     ...

  9. Coding Contest(费用流变形题,double)

    Coding Contest http://acm.hdu.edu.cn/showproblem.php?pid=5988 Time Limit: 2000/1000 MS (Java/Others) ...

随机推荐

  1. 【HDU4473】Exam(数学题)

    点此看题面 大致题意: 设\(f(x)=\sum[(a*b)|x]\),求\(\sum_{x=1}^nf(x)\). 转化题意 将题意进行转换,我们就可以发现,我们要求的\(ans\)就是满足\(x* ...

  2. 一些常用的集合工具的代码块(缓慢更新XD,更新了多属性过滤:) )

    更新记录 虽然经常放鸽子,但是还是要记录一下更新 2017.8.30 更新了listToMap的方法,现在可以指定多个属性进行分组了,例如你要指定一个学生集合,按照名字和年龄相同的放在一组,现在只要调 ...

  3. 笔试算法题(46):简介 - 二叉堆 & 二项树 & 二项堆 & 斐波那契堆

    二叉堆(Binary Heap) 二叉堆是完全二叉树(或者近似完全二叉树):其满足堆的特性:父节点的值>=(<=)任何一个子节点的键值,并且每个左子树或者右子树都是一 个二叉堆(最小堆或者 ...

  4. 通过LDB_PROCESS函数使用逻辑数据库

    1.概览    通过LDB_PROCESS函数可以允许任何程序访问逻辑数据库,允许一个程序访问多个逻辑数据库,当然也允许多次连续访问访问同个逻辑数据库.当使用LDB_PROCESS函数来访问逻辑数据库 ...

  5. phpstudy配置SSL证书的步骤(Apache环境)以及一些注意事项

    准备工具(我自己的): 腾讯云的域名和云主机,还有SSL证书,以及phpstudy 首先要下载自己的SSL证书,会得到一个压缩包,解压以后会得到四个文件夹和一个csr文件, Apache文件夹内三个文 ...

  6. Django2.1集成xadmin管理后台所遇到的错误集锦,解决填坑

    django默认是有一个admin的后台管理模块,但是丑,功能也不齐全,但是大神给我们已经集成好了xadmin后台,我们拿来用即可,但是呢,django已经升级到2.1版本了,xadmin貌似跟不上节 ...

  7. BFS:Open and Lock(一个数的逐位变化问题的搜索)

    解体心得: 1.关于定义四维数组的问题,在起初使用时,总是在运行时出错,找了很多方法,最后全部将BFS()部分函数写在主函数中,将四维数组定义在主函数中才解决了问题.运行成功后再次将四维数组定义为全局 ...

  8. U1

    如果 activity_main.xml没有xml代码可以对图像右键 go to mxl同时可以在design  和  text  切换 在安装了Android Studio3.3版本之后,第一个He ...

  9. JQ剪辑图片插件,适用于移动端和PC端

    主要用到以下JS文件: <script src="js/photo/iscroll-zoom.js"></script> <script src=&q ...

  10. D3DXCreateTexture

    HRESULT D3DXCreateTexture( __in LPDIRECT3DDEVICE9 pDevice, __in UINT Width, __in UINT Height, __in U ...