poj 3686 The Windy's

The Windy's
Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Appoint description: 

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<stdio.h>
#include <vector>
#include <string.h>
#include<iostream>
#include <queue>
#define max_V 10000
#define max_N 100
#define max_M 100
#define INF 0x3f3f3f3f
using namespace std;
//最小费用流模板,研究图一个多周了,每个题都是手打模板
//每个题的代码都是100行+,我好厉害啊!真佩服我自己

//学图的时候,顺便熟练应用了vector and queue 无敌好寂寞

struct edge{int to,cap,cost,rev;};//终点 容量 费用 反向边
int V;
vector <edge>G[max_V];//图的邻接表表示
int dist[max_V];//最短距离
int prevv[max_V],preve[max_V];//最短路中的前驱节点和对应的边

//向图中增加一条from到to容量为cost的边
void add_edge(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});
}

//求解从s到t流量为f的最小费用流
//如果不能再增广则返回-1
int min_cost_flow(int s,int t,int f)
{
    int res=0;
    while(f>0)
    {
        fill(dist,dist+V,INF);
        dist[s]=0;
        bool updata=1;
        while(updata)
        {
            updata=0;
            for(int v=0;v<V;v++)
            {
                if(dist[v]==INF)continue;
                for(int i=0;i<G[v].size();i++)
                {
                    edge &e=G[v][i];
                    if(e.cap>0&&dist[e.to]>dist[v]+e.cost)
                    {
                        dist[e.to]=dist[v]+e.cost;
                        prevv[e.to]=v;
                        preve[e.to]=i;
                        updata=1;
                    }
                }
            }
        }

    if(dist[t]==INF)//不能再增广
    {
        return -1;
    }

    //沿s到t的最短路尽量增广
    int d=f;
    for(int v=t;v!=s;v=prevv[v])
    {
        d=min(d,G[prevv[v]][preve[v]].cap);
    }
    f-=d;
    res+=d*dist[t];
    for(int v=t;v!=s;v=prevv[v])
    {
        edge &e=G[prevv[v]][preve[v]];
        e.cap-=d;
        G[v][e.rev].cap+=d;
    }
    }
    return res;

}

int N,M;
int Z[max_N][max_M];

void solve()
{
    int s=N+N*M,t=s+1;
    V=t+1;
    for(int i=0;i<N;i++)
        add_edge(s,i,1,0);
    for(int j=0;j<M;j++)
    {
        for(int k=0;k<N;k++)
        {
            add_edge(N+j*N+k,t,1,0);
        for(int i=0;i<N;i++)
        {
            add_edge(i,N+j*N+k,1,(k+1)*Z[i][j]);
        }
        }

    }
    printf("%.6f\n",(double)min_cost_flow(s,t,N)/N);
    for(int i=0;i<=V;i++)
        G[i].clear();
}

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",&Z[i][j]);
 solve();
 }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值