The Windy's - POJ 3686 KM算法
2015-07-07 20:31
393 查看
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
Sample Output
题意:有n个物品,m个制造车间,每个物品在一个车间内制造有相应的时间,问所有商品从开始到***完成的时间的和的平均值最小是多少。
思路:用KM算法,一边是n个物品,一边是n*m的矩阵,表示a个物品在第j个车间里是第倒数i个完成的。对应的权值是cost[a][j]*i,可以这么理解:它在倒数第i个完成后,对后面的i-1个物品等待其花费的时间加入这个权值当中。
AC代码如下:
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 4408 | Accepted: 1874 |
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个制造车间,每个物品在一个车间内制造有相应的时间,问所有商品从开始到***完成的时间的和的平均值最小是多少。
思路:用KM算法,一边是n个物品,一边是n*m的矩阵,表示a个物品在第j个车间里是第倒数i个完成的。对应的权值是cost[a][j]*i,可以这么理解:它在倒数第i个完成后,对后面的i-1个物品等待其花费的时间加入这个权值当中。
AC代码如下:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int T,t,n,m,w[60][2600],cost[60][60],nx,ny,INF=1e9; int lx[60],ly[2600],linky[2600],slack[2600]; bool visx[60],visy[2600]; bool Find(int x) { int y,t; visx[x]=1; for(y=1;y<=ny;y++) if(!visy[y]) { t=lx[x]+ly[y]-w[x][y]; if(t==0) { visy[y]=1; if(linky[y]==-1 || Find(linky[y])) { linky[y]=x; return 1; } } else if(slack[y]>t) slack[y]=t; } return false; } int KM() { int i,j,k,x,y,d,ans=0; memset(linky,-1,sizeof(linky)); memset(ly,0,sizeof(ly)); for(i=1;i<=nx;i++) { lx[i]=-INF; for(j=1;j<=ny;j++) lx[i]=max(lx[i],w[i][j]); } for(i=1;i<=nx;i++) { for(j=1;j<=ny;j++) slack[j]=INF; while(true) { memset(visx,0,sizeof(visx)); memset(visy,0,sizeof(visy)); if(Find(i)) break; d=INF; for(j=1;j<=ny;j++) if(!visy[j] && d>slack[j]) d=slack[j]; for(j=1;j<=nx;j++) if(visx[j]) lx[j]-=d; for(j=1;j<=ny;j++) if(visy[j]) ly[j]+=d; else slack[j]-=d; } } for(j=1;j<=ny;j++) if(linky[j]>-1) ans+=w[linky[j]][j]; return ans; } int main() { int i,j,k,ans; scanf("%d",&T); for(t=1;t<=T;t++) { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) for(j=1;j<=m;j++) scanf("%d",&cost[i][j]); nx=n;ny=n*m; for(i=1;i<=n;i++) for(j=1;j<=n;j++) for(k=1;k<=m;k++) w[i][(j-1)*m+k]=-cost[i][k]*j; ans=KM(); printf("%.6f\n",-1.0*ans/n); } }
相关文章推荐
- 第4章 更多的 bash shell命令 - df、du、sort、grep、zip、tar等
- Binary Search Tree Iterator
- 密码学基础知识(六)Hash函数与消息认证
- 总结eclipse中安装maven插件
- 自己练习写的Procedure,两种方法遍历cursor,代码已验证
- select、poll、epoll之间的区别总结
- 世纪互联云和华为共同打造的数据中心是一个很好的一步标志!
- Python快速教程 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明。谢谢! 怎么能快速地掌握Python?这是和朋友闲聊时谈起
- 啊哈!图的广度优先遍历
- 如何在Linux下实现设备的配置(上)
- JSP整理总结--JSP指令、动作标签
- php 上传文件失败$_FILES tmp_name 为空
- matlab神经网络 -基础入门 视频1
- mysql 字符串转换为日期 日期格式化
- TCP协议与UDP协议的区别
- HADOOP基本操作命令
- How do I reset Windows Update components?
- C#调用WebService实例和开发
- hdu4055 dp
- NYOJ 457 大小写互换