The Windy's
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3791   Accepted: 1631

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

Source

 
每个玩具点对(1 ~ N) * z[i][j]费用的 工厂 建边,求最小费用流
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector> using namespace std; const int MAX = ;
const int INF = 1e9 + ;
int N, M;
int z[MAX][MAX];
struct Edge {int from, to, cap, flow, cost;};
vector<Edge> edges;
vector<int> G[MAX * MAX * MAX + * MAX];
int p[MAX * MAX * MAX + * MAX], a[MAX * MAX * MAX + * MAX];
int d[MAX * MAX * MAX + * MAX];
bool inq[MAX * MAX * MAX + * MAX]; void add_edge(int from, int to, int cap, int cost) {
edges.push_back(Edge {from, to, cap, , cost});
edges.push_back(Edge {to, from, , , -cost});
int m = edges.size();
G[from].push_back(m - );
G[to].push_back(m - );
} bool bellmanford(int s, int t, int &flow, int &cost) {
for(int i = s; i <= t; ++i) {
d[i] = INF;
} memset(inq, , sizeof(inq));
d[s] = ; inq[s] = ; p[s] = ; a[s] = INF; queue<int> Q;
Q.push(s);
while(!Q.empty()) {
//printf("fuck\n");
int u = Q.front(); Q.pop();
inq[u] = ;
for(int i = ; i < G[u].size(); ++i) {
Edge& e = edges[ G[u][i] ];
if(e.cap > e.flow && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if(!inq[e.to]) {
inq[e.to] = ;
Q.push(e.to);
}
}
}
} if(d[t] == INF) return ;
flow += a[t];
cost += d[t] * a[t]; int u = t;
while(u != s) {
edges[p[u]].flow += a[t];
edges[p[u] ^ ].flow -= a[t];
u = edges[p[u]].from;
} return ;
} int Mincost(int s, int t) {
int flow = , cost = ;
while(bellmanford(s, t, flow, cost));
return cost;
} int main()
{
//freopen("sw.in", "r", stdin);
int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &N, &M);
for(int i = ; i <= N; ++i) {
for(int j = ; j <= M; ++j) {
scanf("%d", &z[i][j]);
//printf("%d ", z[i][j]);
}
} int s = , t = N + N * M + ;
for(int i = s; i <= t; ++i) G[i].clear();
edges.clear(); for(int i = ; i <= N; ++i) {
add_edge(s, i, , );
} for(int j = ; j <= M; ++j) {
for(int k = ; k <= N; ++k) {
add_edge(j * N + k, t, , );
for(int i = ; i <= N; ++i) {
add_edge(i, j * N + k, , z[i][j] * k);
}
}
}
printf("%.6f\n", (double) Mincost(s, t) / N); } //cout << "Hello world!" << endl;
return ;
}

poj 3686的更多相关文章

  1. POJ 3686 The Windy's(思维+费用流好题)

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

  2. poj 3686 The Windy's

    http://poj.org/problem?id=3686 #include <cstdio> #include <cstring> #include <algorit ...

  3. poj 3686(拆点+最小权匹配)

    题目链接:http://poj.org/problem?id=3686 思路:显然工件为X集,机器为Y集合.由于每个机器一次只能加工一个部件,因此我们可以将一台机器拆成N个点,至于部件与机器之间连多大 ...

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

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

  5. POJ 3686:The Windy's(最小费用最大流)***

    http://poj.org/problem?id=3686 题意:给出n个玩具和m个工厂,每个工厂加工每个玩具有一个时间,问要加工完这n个玩具最少需要等待的平均时间.例如加工1号玩具时间为t1,加工 ...

  6. Poj(3686),最小权匹配,多重匹配,KM

    题目链接 The Windy's | Time Limit: 5000MS | Memory Limit: 65536K | | Total Submissions: 4939 | Accepted: ...

  7. [ACM] POJ 3686 The Windy&#39;s (二分图最小权匹配,KM算法,特殊建图)

    The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4158   Accepted: 1777 Descr ...

  8. poj 3686 Priest John's Busiest Day

    http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...

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

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

随机推荐

  1. C 中 关于printf 函数中度剖析

    题外话  这篇博文主要围绕printf函数分析的,主要讲解printf 使用C的可变参数机制, printf是否可重入(是否线程安全), printf函数的源码实现. 正文 1.C中可变参数机制 我们 ...

  2. hdu 5146 Sequence

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5146 Sequence Description Today we have a number sequ ...

  3. 九度oj 1184 二叉树遍历

    原题链接:http://ac.jobdu.com/problem.php?pid=1184 简单的二叉树重建,遍历. 如下: #include<cstdio> #include<cs ...

  4. iOS学习之UI可视化编程-StoryBoard

    一.StoryBoard与xib 对比: 相同点:都属于IB编程的方式,可以快速构建GUI. 不同点:xib侧重于单文件(单独的控制器或者视图)编辑,storyboard侧重于多页面关联.storyb ...

  5. 从零开始学ios开发(二):Hello World!来啦!

    今天看了书的第二章,主要介绍了一下Xcode的使用方法和一些必要的说明,最后做了一个“Hello World!”的小程序,其实就是在屏幕上用一个Label显示“Hello World!”,一行代码都没 ...

  6. simplexml_load_string获取xml节点里的属性值

    http://stackoverflow.com/questions/14359658/get-xml-attribute-using-simplexml-load-string 问: I am us ...

  7. JAVA栈实例—括号匹配

    import java.util.Stack; public class test { public static void main(String[] args){ System.out.print ...

  8. Node.js 学习(二) 创建第一个应用

    如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个"接收 HTTP 请求并提供 ...

  9. 前端之JavaScript第三天学习(7)-JavaScript-数据类型

    字符串.数字.布尔.数组.对象.Null.Undefined JavaScript 拥有动态类型 JavaScript 拥有动态类型.这意味着相同的变量可用作不同的类型: 实例 var x      ...

  10. WPF 策略模式

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...