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) ...
随机推荐
- js 数组方法大集合,各方法是否改变原有的数组详解
不会改变原来数组的有: concat()---连接两个或更多的数组,并返回结果. every()---检测数组元素的每个元素是否都符合条件. some()---检测数组元素中是否有元素符合指定条件. ...
- 【luogu P1637 三元上升子序列】 题解
题目链接:https://www.luogu.org/problemnew/show/P1637 BIT + 离散化. 读题得数据规模需离散化.BIT开不到longint这么大的数组. 对于题目所求的 ...
- Redis学习记录(三)
1.Redis集群的搭建 1.1redis-cluster架构图 架构细节: (1)所有的redis节点批次互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽. (2)节点的fail ...
- 关于Runtime Issues
前言:在使用某移动直播的SDK的时候发现,在Run应用的时候会有紫色的警告(Xcode9 + iOS11) 当时还专门提交了工单,当时对方的回复是,大概意思是不影响使用,后期修复. 今天看视频发现这是 ...
- POJ 2406 Power String
算出next数组. 对于任何一个循环字串,len-next[len]必为最小循环节长度 若len%(len-next[len])==0 即为循环字串,n=len/(len-next[len]) 否则输 ...
- mysql查询哪个表数据量最大
use information_schema;select table_name,table_rows from tables where table_schema='cargo_new' order ...
- linux-最最最最最常用的操作
去除两个文件中相同的内容 比如我想把file1中不含文件file2的内容保留下来:(这个在抠一些内容的时候挺好用的) awk '{print $0}' file1 file2 |sort|uniq - ...
- 描述符应用 -- 让python变成一个强类型的语言
众所周知,python是一门弱类型的语言,变量可以随意赋值成任意类型,但是通过描述符,我们可以把数据变成强类型的. 我们为数据设置数据描述符,因为数据描述的优先级大于实例属性,所以在给数据赋值的时候会 ...
- stm32之PWM博客好文收藏
https://www.cnblogs.com/jiwangbujiu/p/5616376.html STM32F103 使用TIM3产生四路PWM https://www.cnblogs.com/c ...
- POJ 2763 Housewife Wind 树链拋分
一.前言 这破题WA了一天,最后重构还是WA,最后通过POJ讨论版得到的数据显示,我看上去是把某个变量写错了..于是,还是低级错误背锅啊....代码能力有待进一步提升2333333 二.题意 某家庭主 ...