【POJ 3686 The Windy's】+ 最小费用流

The Windy’s
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5412 Accepted: 2268
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

题意 : n 个玩具 ,m 个工厂,j 号工厂加工 i 号玩具需要 a[i][j] 的时间,没个玩具必须在一个工厂内完成,玩具的制作顺序可以是任意的,每个工厂每次只能加工一个玩具,求加工完所有的玩具的平均最小时间

思路 : 一个工厂时 T = a[1] + (a[1] + a[2]) + ……+ (a[1] + a[2] + …..a[n])
变形得 : T = n * a[1] + (n - 1) * a[2] + ……+ a[n];
同样也可以看成多个只能制作一个玩具得工厂 + 最小费用流

AC代码:

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int MAX = 3e3 + 10;
const int INF = 0x3f3f3f3f;
int a[52][52],n,m,V;
int dis[MAX]; // 最短距离
int pre[MAX],pr[MAX];// 最短路中的前驱结点和对应的边
struct edge{ int to,cap,cost,rev; };
vector <edge> G[MAX];
void add(int from,int to,int cap,int cost){
    G[from].push_back((edge){to,cap,cost,G[to].size()});
    G[to].push_back((edge){from,0,-cost,G[from].size() - 1});
}
int min_cost_flow(int s,int t,int f){
    int res = 0;
    while(f > 0){
        fill(dis,dis + V,INF);
        dis[s] = 0;
        bool update = true;
        while(update){
            update = false;
            for(int v = 0; v < V; v++){
                if(dis[v] == INF) continue;
                for(int i = 0; i < G[v].size(); i++){
                    edge &e = G[v][i];
                    if(e.cap > 0 && dis[e.to] > dis[v] + e.cost){
                        dis[e.to] = dis[v] + e.cost;
                        pre[e.to] = v;
                        pr[e.to] = i;
                        update = true;
                    }
                }
            }
        }
        if(dis[t] == INF) return -1;
        int d = f;
        for(int v = t; v != s; v = pre[v])
            d = min(d,G[pre[v]][pr[v]].cap);
        f -= d;
        res += d * dis[t];
        for(int v = t; v != s; v = pre[v]){
            edge &e = G[pre[v]][pr[v]];
            e.cap -= d;
            G[v][e.rev].cap += d;
        }
    }
    return res;
}
void solve(){
    int s = n + n * m,t = s + 1;
    V = t + 1;
    for(int i = 0; i < MAX; i++)
        G[i].clear();
    for(int i = 0; i < n; i++)
        add(s,i,1,0);
    for(int j = 0; j < m; j++)
        for(int k = 0; k < n; k++){
            add(n + j * n + k,t,1,0);
            for(int i = 0; i < n; i++)
                add(i,n + j * n + k,1,(k + 1) * a[i][j]);
        }
    printf("%.6f\n",(double) min_cost_flow(s,t,n) / n);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                scanf("%d",&a[i][j]);
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值