POJ 3686 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(思维+费用流好题)的更多相关文章
- POJ 3686 The Windy's (费用流)
[题目链接] http://poj.org/problem?id=3686 [题目大意] 每个工厂对于每种玩具的加工时间都是不同的, 并且在加工完一种玩具之后才能加工另一种,现在求加工完每种玩具的平均 ...
- POJ 3686 The Windy's 最小费用最大流
每个工厂拆成N个工厂,费用分别为1~N倍原费用. //#pragma comment(linker, "/STACK:1024000000,1024000000") #includ ...
- poj 3422 Kaka's Matrix Travels 费用流
题目链接 给一个n*n的矩阵, 从左上角出发, 走到右下角, 然后在返回左上角,这样算两次. 一共重复k次, 每个格子有值, 问能够取得的最大值是多少, 一个格子的值只能取一次, 取完后变为0. 费用 ...
- 2018.06.27The Windy's(费用流)
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6003 Accepted: 2484 Descripti ...
- POJ 2677 旅行商问题 双调dp或者费用流
Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3408 Accepted: 1513 Description ...
- poj - 3686 The Windy's (KM算法)
题意:n个订单和m个生产车间,每个订单在不同的车间生产所需要的时间不一样,并且每个订单只能在同一个车间中完成,直到这个车间完成这个订单就可以生产下一个订单.现在需要求完成n个订单的平均时间最少是多少. ...
- HDU 3376 && 2686 方格取数 最大和 费用流裸题
题意: 1.一个人从[1,1] ->[n,n] ->[1,1] 2.仅仅能走最短路 3.走过的点不能再走 问最大和. 对每一个点拆点限流为1就可以满足3. 费用流流量为2满足1 最大费用流 ...
- Lunch Time(费用流变型题,以时间为费用)
Lunch Time http://acm.hdu.edu.cn/showproblem.php?pid=4807 Time Limit: 4000/2000 MS (Java/Others) ...
- Coding Contest(费用流变形题,double)
Coding Contest http://acm.hdu.edu.cn/showproblem.php?pid=5988 Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- CSS之常见文字样式整理
常见文字样式 行高:line-height,当我i们将行高的大小设置成当前元素的高度时,可以实现当行文本在当前元素中垂直方向居中显示的效果 水平对齐方式:text-align:left|center| ...
- 复习C++_基础、函数、数组、字符串
程序的开发过程 程序 源程序:用源语言写的,有待翻译的程序: 目标程序:源程序通过翻译程序加工以后生成的机器语言程序: 可执行程序:连接目标程序以及库中的某些文件,生成的一个可执行文件,例如Windo ...
- Java读取properties配置文件工具类
1. PropertyUtils.java package javax.utils; import java.io.InputStream; import java.util.Properties ...
- Windows8.1任务栏取消oneDrive图标
Windows8.1任务栏会有oneDrive图标,用不着,想取消,方法如下:
- cocoapods 类库管理利器
作为iOS开发者,第三方类库的使用是最经常的,但鉴于第三方类库的不断更新以及其可能需要依存其他类,如果要使用最新版那么我们需要重新下载再添加到项目中,无疑带来一些繁琐的麻烦,那么现在这里就有一款能解决 ...
- JavaScript常用八种继承方案
更新:在常用七种继承方案的基础之上增加了ES6的类继承,所以现在变成八种啦,欢迎加高级前端进阶群一起学习(文末). --- 2018.10.30 1.原型链继承 构造函数.原型和实例之间的关系:每个构 ...
- hdu 5667
Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- Word 2013发布博客测试
Hello world ! I am from word2013! 测试修改 这里添加一行文字. 参考 1在 Word 中建立博客的相关帮助 2使用Word2013发布随笔到博客园 PS: 参考2 ...
- eeeeeeeeeee
http://58.241.123.38/hot.cdn.baidupcs.com/file/91623e76f776475da9c3223cdac861f0?xcode=68983c005f6e3c ...
- 将SpringBoot默认Json解析框架jackson替换成fastjson
步骤一:引入依赖<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson< ...