POJ3686 The Windy's 【费用流】*

55 篇文章 2 订阅
10 篇文章 0 订阅

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


描述一下题意,大概是给你n个物品和m台机器
然后每个物品在每个机器上加工有不同的时间,一台机器只能同时加工一个物品,一个物品只能放在一个机器上加工
想要最小化每个物品加工结束时间的和

我们发现要求的是结束时间的和,但是我们却不知道它前面的物品是什么,那么我们怎么确定时间?

假设在一台机器上运行了 a1,a2,a3,......,ai a 1 , a 2 , a 3 , . . . . . . , a i 个物品
那么结束时间和可以表示成:

(ta1)+(ta1+ta2)+(ta1+ta2+ta3)+......+(ta1+ta2+ta3+......+tai) ( t a 1 ) + ( t a 1 + t a 2 ) + ( t a 1 + t a 2 + t a 3 ) + . . . . . . + ( t a 1 + t a 2 + t a 3 + . . . . . . + t a i )

不难化简成:

ta1i+ta2(i1)+ta3(i2)+......+tai1 t a 1 ∗ i + t a 2 ∗ ( i − 1 ) + t a 3 ∗ ( i − 2 ) + . . . . . . + t a i ∗ 1

那么假如不管先后顺序的限制,我们可以将一个机器拆分成n个普通机器,处理每个物品的时间分别是1到n倍的题目时间,然后从每个物品向每个拆分后的机器连接边,容量为1,就可以保证对于每个机器,处理的物品一定占据前k个处理顺序
那么又因为对于一个机器,它拆分成的所有小机器的花费时间和就是我们要求的总时间,所以直接从每一个拆分出来的机器连到汇点就可以了


#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一道比较经典的计数问题。题目描述如下: 给定一个 $n \times n$ 的网格图,其中一些格子被标记为障碍。一个连通块是指一些被标记为障碍的格子的集合,满足这些格子在网格图中连通。一个格子是连通的当且仅当它与另一个被标记为障碍的格子在网格图中有公共边。 现在,你需要计算在这个网格图中,有多少个不同的连通块,满足这个连通块的大小(即包含的格子数)恰好为 $k$。 这是一道比较经典的计数问题,一般可以通过计算生成函数的方法来解决。具体来说,我们可以定义一个生成函数 $F(x)$,其中 $[x^k]F(x)$ 表示大小为 $k$ 的连通块的个数。那么,我们可以考虑如何计算这个生成函数。 对于一个大小为 $k$ 的连通块,我们可以考虑它的形状。具体来说,我们可以考虑以该连通块的最左边、最上边的格子为起点,从上到下、从左到右遍历该连通块,把每个格子在该连通块中的相对位置记录下来。由于该连通块的大小为 $k$,因此这些相对位置一定是 $(x,y) \in [0,n-1]^2$ 中的 $k$ 个不同点。 现在,我们需要考虑如何计算这些点对应的连通块是否合法。具体来说,我们可以考虑从左到右、从上到下依次处理这些点,对于每个点 $(x,y)$,我们需要考虑它是否能够与左边的点和上边的点连通。具体来说,如果 $(x-1,y)$ 和 $(x,y)$ 都在该连通块中且它们在网格图中有公共边,那么它们就是连通的;同样,如果 $(x,y-1)$ 和 $(x,y)$ 都在该连通块中且它们在网格图中有公共边,那么它们也是连通的。如果 $(x,y)$ 与左边和上边的点都不连通,那么说明这个点不属于该连通块。 考虑到每个点最多只有两个方向需要检查,因此时间复杂度为 $O(n^2 k)$。不过,我们可以使用类似于矩阵乘法的思想,将这个过程优化到 $O(k^3)$ 的时间复杂度。 具体来说,我们可以设 $f_{i,j,k}$ 表示状态 $(i,j)$ 所代表的点在连通块中,且连通块的大小为 $k$ 的方案数。显然,对于一个合法的 $(i,j,k)$,我们可以考虑 $(i-1,j,k-1)$ 和 $(i,j-1,k-1)$ 这两个状态,然后把点 $(i,j)$ 加入到它们所代表的连通块中。因此,我们可以设计一个 $O(k^3)$ 的 DP 状态转移,计算 $f_{i,j,k}$。 具体来说,我们可以考虑枚举连通块所包含的最右边和最下边的格子的坐标 $(x,y)$,然后计算 $f_{x,y,k}$。对于一个合法的 $(x,y,k)$,我们可以考虑将 $(x,y)$ 所代表的点加入到 $(x-1,y,k-1)$ 和 $(x,y-1,k-1)$ 所代表的连通块中。不过,这里需要注意一个细节:如果 $(x-1,y)$ 和 $(x,y)$ 在网格图中没有相邻边,那么它们不能算作连通的。因此,我们需要特判这个情况。 最终,$f_{n,n,k}$ 就是大小为 $k$ 的连通块的个数,时间复杂度为 $O(n^2 k + k^3)$。 参考代码:

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值