POJ 3686 The Windy's 最小权值匹配

来源:互联网 发布:东北人假义气 知乎 编辑:程序博客网 时间:2024/09/21 06:33
点击打开链接
The Windy's
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 3788 Accepted: 1630

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

33 4100 100 100 199 99 99 198 98 98 13 41 100 100 10099 1 99 9998 98 1 983 41 100 100 1001 99 99 9998 1 98 98

Sample Output

2.0000001.0000001.333333

有n个任务需要m台机器去做,每台机器一次只能做一个任务,而且每个任务必须经过这m台机器才能完成,先给你每个任务在每台机器的时间,问最少的平均时间是多少。
完成所有的任务总时间是实际时间+等待时间,设完成1,2,3....n的时间分别为t1,t2,t3...tn,那么总时间就是t=t1+(t1+t2)+(t1+t2+t3)+......+(t1+t2+...+tn)=t1*n+t2*(n-1)+t3*(n-2)+tn
第k个任务在倒数第j台机器处理tk*j。
每个机器可以处理n个任务,将每个机器拆成n个点,1~n分别代表某个任务在这个机器上倒数第几个被加工的,对于每个任务i,机器j中拆的每个点k,连接一条-map[i][j]*k权值的边。
//36044K579MS#include<stdio.h>#include<string.h>#include<algorithm>#define M 3007#define inf 0x3f3f3fusing namespace std;int g[M][M],map[M][M];int lx[M],ly[M];int slack[M],match[M];bool visx[M],visy[M];int n,m;void build(){    for(int i=1; i<=n; i++)        for(int j=1; j<=m; j++)            for(int k=1; k<=n; k++)                g[i][(j-1)*n+k]=-map[i][j]*k;    m=n*m;}bool dfs(int cur){    visx[cur]=true;    for(int y=1; y<=m; y++)    {        if(!visy[y]&&lx[cur]+ly[y]==g[cur][y])        {            visy[y]=true;            if(match[y]==-1||dfs(match[y]))            {                match[y]=cur;                return true;            }        }    }    return false;}int KM(){    memset(match,-1,sizeof(match));    memset(ly,0,sizeof(ly));    for(int i=1; i<=n; i++)    {        lx[i]=-inf;        for(int j=1; j<=m; j++)            lx[i]=max(lx[i],g[i][j]);    }    for(int x=1; x<=n; x++)    {        while(true)        {            memset(visx,false,sizeof(visx));            memset(visy,false,sizeof(visy));            if(dfs(x))break;            int d=inf;            for(int j=1; j<=n; j++)                if(visx[j])                {                    for(int k=1; k<=m; k++)                    {                        if(!visy[k]&&d>lx[j]+ly[k]-g[j][k])                        {                            d=lx[j]+ly[k]-g[j][k];                        }                    }                }            for(int i=1; i<=n; i++)                if(visx[i])                    lx[i]-=d;            for(int i=1; i<=m; i++)                if(visy[i])                    ly[i]+=d;        }    }    int result=0;    for(int i=1; i<=m; i++)    {        if(match[i]!=-1&&g[match[i]][i]!=-inf)            result+=g[match[i]][i];    }    return result;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        memset(g,0,sizeof(g));        for(int i=1; i<=n; i++)            for(int j=1; j<=m; j++)                scanf("%d",&map[i][j]);        build();        int ans=KM();        printf("%.6f\n",-1.0*(double)ans/n);    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小米5s无限重启怎么办 小米5s外屏坏了怎么办 小米5s内屏碎了怎么办 小米4充电没反应怎么办 小米5手机变卡了怎么办 小米5变卡了怎么办 小米手机充电无反应怎么办 小米6相机卡顿怎么办 华为手机玩游戏发热怎么办 华为手机变慢了怎么办 华为p10手机变慢怎么办 华为手机账户密码忘记了怎么办 QQ浏览器无法加载插件怎么办 电脑开了机黑屏怎么办 扫描仪打不开运单扫描怎么办 打印机不支持64位系统怎么办 xp系统dnf闪退怎么办 w10电脑所有程序都打不开怎么办 安卓手机太卡怎么办 系统装到f盘了怎么办 虚拟机占c盘内存怎么办 外机连无线虚拟机显示受限怎么办 使用msdn下载解压后怎么办 路由80端口被占用怎么办 c盘拒绝粘贴文件怎么办 oracle数据库密码忘了怎么办 电脑开机时不显示用户名怎么办? xp系统忘记开机密码怎么办 电脑开机密码忘了怎么办 c盘满了怎么办win10 win10电脑开机密码忘了怎么办 win10的开机密码忘了怎么办 u盘中了exe病毒怎么办 眼睛长个麦粒豆怎么办 苹果手机sdk授权失败怎么办 小米5王者荣耀卡怎么办 华为p9手机电池不耐用怎么办 华为g9青春版耗电快怎么办 华为手机摄像头坏了怎么办 华为p10摄像头玻璃划痕怎么办? 华为g9手机音量小怎么办