POJ3686 The Windy's 【费用流】*

本文介绍了一种解决最小成本最大流问题的算法,并通过一个具体的编程实例详细展示了该算法的应用过程。针对某一玩具工厂的订单分配问题,利用最小成本最大流算法找到了最优解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

POJ3686 The Windy’s


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


 


#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define N 10010
struct Edge{
    int u,v,cap,flow,cost;
    Edge(int xu,int xv,int xcap,int xflow,int xcost){
        u=xu;v=xv;cap=xcap;flow=xflow;cost=xcost;
    }
};
struct MCMF{
    int s,t;
    int d[N],f[N],p[N];
    bool inq[N];
    vector<Edge> E;
    vector<int> G[N];
    void clear(){
        E.clear();
        for(int i=0;i<N;i++)G[i].clear();
    }
    void add(int u,int v,int cap,int cost){
        Edge w1(u,v,cap,0,cost);
        Edge w2(v,u,0,0,-cost);
        E.push_back(w1);
        E.push_back(w2);
        int m=E.size();
        G[u].push_back(m-2);
        G[v].push_back(m-1);
    }
    bool SPFA(int &flow,int &cost){
        memset(inq,0,sizeof(inq));
        memset(d,0x3f,sizeof(d));
        queue<int> Q;
        Q.push(s);
        d[s]=0;f[s]=INF;
        while(!Q.empty()){
            int u=Q.front();Q.pop();inq[u]=0;
            for(int i=0;i<G[u].size();i++){
                Edge e=E[G[u][i]];
                if(e.cap>e.flow&&d[e.v]>d[u]+e.cost){
                    d[e.v]=d[u]+e.cost;
                    p[e.v]=G[u][i];
                    f[e.v]=min(f[u],e.cap-e.flow);
                    if(!inq[e.v]){
                        Q.push(e.v);
                        inq[e.v]=1;
                    }
                }
            }
        }
        if(d[t]==INF)return false;
        flow+=f[t];cost+=f[t]*d[t];
        int u=t;
        while(u!=s){
            E[p[u]].flow+=f[t];
            E[p[u]^1].flow-=f[t];
            u=E[p[u]].u;
        }
        return true;
    }
    int Min_cost_Max_flow(){
        int flow=0,cost=0;
        while(SPFA(flow,cost));
        return cost;
    }
}mcmf;
int n,m,a[100];
int main(){
    int T;scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        mcmf.clear();
        mcmf.s=0;
        mcmf.t=n+n*m+1;
        for(int i=1;i<=n;i++){
            mcmf.add(0,i,1,0);
            for(int j=1;j<=m;j++){
                int p;scanf("%d",&p);
                for(int k=1;k<=n;k++)
                    mcmf.add(i,j*n+k,1,k*p);
            }
        }
        for(int i=n+1;i<=n*m+n;i++)mcmf.add(i,n*m+n+1,1,0);
        printf("%.6lf\n",mcmf.Min_cost_Max_flow()*1.0/n);
    }
    return 0;
}

转载于:https://www.cnblogs.com/dream-maker-yk/p/9676353.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值