博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 三部曲 1Minimum Cost 最小费用最大流EK算法
阅读量:6693 次
发布时间:2019-06-25

本文共 4720 字,大约阅读时间需要 15 分钟。

Problem Description
Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (marked from 1 to M), each provides K different kinds of goods (marked from 1 to K). Once shopkeepers order goods, Dearboy should arrange which supply place provide how much amount of goods to shopkeepers to cut down the total cost of transport. 
It's known that the cost to transport one unit goods for different kinds from different supply places to different shopkeepers may be different. Given each supply places' storage of K kinds of goods, N shopkeepers' order of K kinds of goods and the cost to transport goods for different kinds from different supply places to different shopkeepers, you should tell how to arrange the goods supply to minimize the total cost of transport.
 

 

Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, K (0 < N, M, K < 50), which are described above. The next N lines give the shopkeepers' orders, with each line containing K integers (there integers are belong to [0, 3]), which represents the amount of goods each shopkeeper needs. The next M lines give the supply places' storage, with each line containing K integers (there integers are also belong to [0, 3]), which represents the amount of goods stored in that supply place. 
Then come K integer matrices (each with the size N * M), the integer (this integer is belong to (0, 100)) at the i-th row, j-th column in the k-th matrix represents the cost to transport one unit of k-th goods from the j-th supply place to the i-th shopkeeper. 
The input is terminated with three "0"s. This test case should not be processed.
 

 

Output
For each test case, if Dearboy can satisfy all the needs of all the shopkeepers, print in one line an integer, which is the minimum cost; otherwise just output "-1".
 

 

Sample Input
1 3 3 1 1 1 0 1 1 1 2 2 1 0 1 1 2 3 1 1 1 2 1 1 1 1 1 3 2 20 0 0 0
 

 

Sample Output
4 -1
***************************************************************************************************************************
最小费用最大流EK算法  注:初始化很重要
***************************************************************************************************************************
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #define inf 0x7fffffff 9 using namespace std; 10 int a[61][61],b[61][61],c[61][61][61]; 11 int pre[201],cost[101][201]; 12 int dis[222],cap[121][221]; 13 int vis[211]; 14 int que[1000011]; 15 int n,m,k,i,j,src; 16 int end1,s; 17 int sum1[161],sum2[161]; 18 19 int bfs()//注意用STL超时 20 { 21 for(int it=0;it<=end1;it++) 22 dis[it]=inf; 23 memset(vis,0,sizeof(vis)); 24 int base,top; 25 base=top=0; 26 que[top++]=src; 27 dis[src]=0; 28 vis[src]=1; 29 pre[src]=0; 30 while(base
0) 37 if(dis[it]>(dis[fs]+cost[fs][it]))//此处找到最小费用 38 { 39 pre[it]=fs; 40 dis[it]=dis[fs]+cost[fs][it]; 41 if(!vis[it]) 42 { 43 que[top++]=it; 44 vis[it]=1; 45 } 46 } 47 } 48 } 49 if(dis[end1]==inf) 50 return -1; 51 return 1; 52 53 } 54 int EK()//算出最小费用最大流 55 { 56 int jt,kt; 57 int sm=0; 58 int min1=inf; 59 while(1) 60 { 61 kt=bfs(); 62 if(kt==-1) 63 break; 64 for(int it=end1;it!=0;it=pre[it]) 65 { 66 jt=pre[it]; 67 if(min1>cap[jt][it]) 68 min1=cap[jt][it]; 69 } 70 for(int it=end1;it!=0;it=pre[it]) 71 { 72 jt=pre[it]; 73 sm+=cost[jt][it]*min1; 74 cap[jt][it]-=min1; 75 cap[it][jt]+=min1; 76 } 77 } 78 return sm; 79 80 } 81 int main() 82 { 83 while(scanf("%d%d%d",&n,&m,&k)!=EOF) 84 { 85 if(n==0&&m==0&&k==0) 86 break; 87 for(i=1;i<=n;i++) 88 for(j=1;j<=k;j++) 89 { 90 scanf("%d",&a[i][j]); 91 } 92 for(i=1;i<=m;i++) 93 for(j=1;j<=k;j++) 94 { 95 scanf("%d",&b[i][j]); 96 } 97 98 99 for(i=1;i<=k;i++)100 for(j=1;j<=n;j++)101 for(int gs=1;gs<=m;gs++)102 scanf("%d",&c[i][j][gs]);103 for(i=1;i<=k;i++)104 {105 int s1=0,s2=0;106 for(j=1;j<=n;j++)107 s1+=a[j][i];108 for(j=1;j<=m;j++)109 s2+=b[j][i];110 if(s1>s2)111 break;112 }113 if(i!=(k+1))114 {115 printf("-1\n");116 continue;117 }118 s=0;119 for(i=1;i<=k;i++)120 {121 memset(cost,0,sizeof(cost));122 memset(cap,0,sizeof(cap));123 for(j=1;j<=m;j++)124 cap[0][j]=b[j][i];125 for(j=1;j<=m;j++)126 for(int ds=1;ds<=n;ds++)127 {128 cap[j][m+ds]=b[j][i];129 cost[j][m+ds]=c[i][ds][j];130 cost[m+ds][j]=-1*c[i][ds][j];131 }132 for(j=1;j<=n;j++)133 cap[m+j][m+n+1]=a[j][i];134 src=0;135 end1=m+n+1;136 s+=EK();137 }138 printf("%d\n",s);139 140 }141 return 0;142 }
View Code

EK

转载于:https://www.cnblogs.com/sdau--codeants/p/3352644.html

你可能感兴趣的文章
从零开始开发微信小程序(三):微信小程序绑定系统账号并授权登录之微信端...
查看>>
[Mysql]——通过例子理解事务的4种隔离级别
查看>>
Java集合体系总结 Set、List、Map、Queue
查看>>
蓝牙心率检测仪涉及到的主要硬件组成
查看>>
easymybatis中字段自动填充
查看>>
Java源码解读扫盲【Integer】
查看>>
Cookie中存储中文异常
查看>>
java 电子商务云平台b2b b2c o2o springmvc+mybatis+spring cloud+spring boot
查看>>
世界杯的狂欢也是黑灰产的狂欢?
查看>>
Eclipse插件系列:jetty插件
查看>>
Ubuntu 下安装ibus日语输入法
查看>>
进击的java(5) spring mvc 文件上传下载
查看>>
Mybatis打印sql语句
查看>>
JSON对象转换成Byte(字节)数组
查看>>
iOS 模拟器卸载
查看>>
yml 配置语法
查看>>
php parse_url()函数
查看>>
如何通过ad收集计算机硬件信息
查看>>
如何通过ad组策略让domain users用户可以远程桌面?
查看>>
[置顶] jquery实现回旋滚动效果
查看>>