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. mysql外键设置(待测试)

    外键的定义语法:[CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...)    REFERENCES tbl_name (index_col ...

  2. 创建自己的oracle解释计划

    1.解释计划 当使用explain plan来为一个查询生成预期的执行计划时,输出将包括一下几种: SQL访问的每一张表: 访问每张表的方法: 每一个需要联结的数据源所使用的联结方法: 按次序列出的所 ...

  3. SQLite数据库与Contentprovider(2)

    ContentProvider: 在创建ContentProvider时,需要首先使用数据库.文件系统或网络实现底层存储功能, 然后在继承ContentProvider的类中实现基本数据操作的接口函数 ...

  4. javascript面向对象和原型

    /* //工厂模式 function createObject(name,age){ var obj = new Object();//新建一个对象 obj.name=name;//新建对象的属性 o ...

  5. golang的goroutine与channel

    Golang的goroutine是非抢占式的, 令人相当蛋疼! 有痛不能呻吟...只能配合channel在各goroutine之间传递信号来实现抢占式, 而这形成了golang最灵活与最具性能的核心. ...

  6. ios学习笔记之内存管理

    一,内存管理类型定义      1,基本类型  任何C的类型,eg:      int,short,char,long,long long,struct,enum,union等属于基本类型或结构体   ...

  7. 单行bash、shell、perl命令

    主题:单行经典bash.shell.perl命令 作者:luomg 摘要: 会陆陆续的写自己工作中的常用有意思的命令,争取你能看完后就能搞定常见操作, 且尽量自少提供基本shell.perl的实现方式 ...

  8. 2.2孙鑫C++

    1.继承 动物有 吃 睡 呼吸的方法 当然 鱼也有    不用重复再定义 1)public 那里都可以访问 #include <iostream.h> class Animal //类 基 ...

  9. LC_ALL=C

    设置LC_ALL=C,可以让sort按照字节排序;

  10. DNS劫持(网页打不开的解决方法)

    我们上网,必不可少的就是DNS,在这里先介绍下DNS的相关知识. DNS 是域名系统 (Domain NameSystem) 的缩写,它是由解析器和域名服务器组成的.域名服务器是指保存有该网络中所有主 ...