其实题意很明确,最小费用最大流, 但是我这2货就建图就太二了, 我把所有的情况都弄到一个图里面。 总的点数有5000个,加上这么多的边,果断TLE。。。

后面知道第k个的情况是独立的,所以可以分成K次建图。 然后每次的点为100, 这样复杂度会小很多很多。 以后注意了,不要那么二的全部都在一起。 要坚持点边越少越好。

Minimum Cost
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 11552   Accepted: 3899

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 11 1 1
3
2
200 0 0

Sample Output

4
-1

Source

POJ Monthly--2005.07.31, Wang Yijie
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define N 5050
#define INF 0x3ffffffstruct node
{int to,next,w,c;
}edge[N*100];int n,m,k;
int s,t;
int sn[N][N],sm[N][N];
int cnt,pre[N];
int que[N*1000];
int point[N],pedge[N];
int mxf;void add_edge(int u,int v,int w,int c)
{edge[cnt].to=v;edge[cnt].w=w;edge[cnt].c=c;edge[cnt].next = pre[u];pre[u]=cnt++;
}int spfa()
{int qd=0,qf=1;int dis[N];int mark[N];memset(point,-1,sizeof(point));memset(pedge,-1,sizeof(pedge));for(int i=0;i<=t;i++){dis[i]=INF;mark[i]=0;}dis[s]=0;que[0]=s;mark[s]=1;while(qf>qd){int cur=que[qd++];mark[cur]=0;for(int p=pre[cur];p!=-1;p = edge[p].next){int v=edge[p].to;int c=edge[p].c;int w=edge[p].w;if(w==0) continue;if( dis[v]>dis[cur]+c ){dis[v]=dis[cur]+c;point[v]=cur;pedge[v]=p;if(mark[v]==0){mark[v]=1;que[qf++]=v;}}}}if( dis[t] == INF ) return 0;else return 1;
}int fuc()
{int sum=0;int mi=INF;int tmp=t;while(tmp!=s){int p=pedge[tmp];if(edge[p].w<mi) mi=edge[p].w;tmp=point[tmp];}tmp=t;mxf += mi;while(tmp!=s){int p=pedge[tmp];edge[p].w -= mi;edge[p^1].w += mi;sum += mi*edge[p].c;tmp=point[tmp];}return sum;
}int main()
{int ans;while(scanf("%d%d%d",&n,&m,&k)&&(n+m+k)){int sum=0;mxf=0;ans=0;for(int i=1;i<=n;i++)for(int j=1;j<=k;j++){scanf("%d",&sn[i][j]);ans += sn[i][j];}for(int i=1;i<=m;i++)for(int j=1;j<=k;j++)scanf("%d",&sm[i][j]);for(int i=1;i<=k;i++){cnt=0;memset(pre,-1,sizeof(pre));s=0;t=n+m+1;for(int j=1;j<=n;j++){add_edge(s,j,sn[j][i],0);add_edge(j,s,0,0);}for(int j=1;j<=m;j++){add_edge(n+j,t,sm[j][i],0);add_edge(t,n+j,0,0);}for(int j=1;j<=n;j++)for(int i1=1;i1<=m;i1++){int tmp;scanf("%d",&tmp);add_edge(j,n+i1,INF,tmp);add_edge(n+i1,j,0,-tmp);}while(spfa()){sum += fuc();}}if( mxf != ans ) printf("-1\n");else printf("%d\n",sum);}return 0;
}

转载于:https://www.cnblogs.com/chenhuan001/archive/2013/03/10/2952374.html

poj 2516(最小费用最大流)相关推荐

  1. poj 2516 最小费用最大流

    最佳匹配的题,m个仓库供应k种商品给n个商家,m*n条运输代价互异,求满足商家需求下的最小运输费用 显然,如果某种商品的供货量比需求大,肯定是无法达到要求的,所以开始要判别是否可以得到最佳匹配 这个题 ...

  2. POJ 2135 最小费用最大流

    思路: 源->1连费用0 流量2 其它的边 费用w 流量1 n->汇 费用0 流量2 最小费用流 搞定~ //By SiriusRen #include <queue> #in ...

  3. POJ - 2516 Minimum Cost 最小费用最大流

    题目链接 题意:给n,m,k表示商店数,储存店数,种类数 然后给n*k表示每个水果店需求每种种类的数量: 表示成 need[i][j] 再给m*k表示每个储存店每种种类数量: 表示成store[i][ ...

  4. POJ - 2516 Minimum Cost(最小费用最大流)

    题目链接:点击查看 题目大意:给出n个买家,m个货点,以及k种货物,接下来按照顺序依次给出一个n*k的矩阵,表示每个买家对于每种货物的需求,一个m*k的矩阵,表示每个货点能供给货物的数量,k个n*m的 ...

  5. POJ 2135 Farm Tour (最小费用最大流)

    Description 给出一张\(N\)个点\(M\)条边的带权无向图,结点编号从\(1\)到\(N\),求从\(1\)到\(N\)再到\(1\)的最短路,每条边最多走一次. Input 第一行给出 ...

  6. POJ - 2175 Evacuation Plan(最小费用最大流+消圈定理)

    题目链接:点击查看 题目大意:给出n个建筑物和m个避难所,每个建筑物中的人需要到避难所中去避难,规定花费是建筑物和避难所的曼哈顿距离+1,现在给出一种路线方案,问这个方案是不是最优的,如果不是,输出比 ...

  7. POJ - 2135 Farm Tour(最小费用最大流)

    题目链接:点击查看 题目大意:给出一个由n个点和m条边组成的无向图,每条边都有权值,求点1->点n->点1的最短路,且要求每条路至多经过一次,并使得途径的权值和最小 题目分析:虽然题目要求 ...

  8. POJ - 2195 Going Home(二分图最小权匹配+KM+思维建边/最小费用最大流)

    题目链接:点击查看 题目大意:给出一个n*m的地图,现在有一定数量的小人要回到屋子里去,题目保证图中的小人和屋子的数量是一致的,小人回到小屋的距离是两个点坐标的曼哈顿距离,每个小屋只能容纳一个小人,现 ...

  9. POJ 2195 Going Home 最小费用最大流

    这题目同时也可以用KM()算法做,最求小权值匹配而已,权值设置为负数就行,具体KM算法参照:http://blog.csdn.net/cnh294141800/article/details/1895 ...

最新文章

  1. C++实现CString和string的互相转换(转)
  2. Codeforces Round #665 (Div. 2) Maximum Distributed Tree(树上贪心)
  3. linux 后台运行jar包命令,Linux 运行jar包命令(Cent OS 7后台运行jar包)
  4. mssql 插入数据缓慢_SQLPro Studio for Mac(数据库管理器)
  5. Xcode出现( linker command failed with exit code 1)错误总结
  6. 多任务学习有用的资料
  7. python如何连续打印_Python实现批量打印功能
  8. [c++][语言语法]函数模板和模板函数 及参数类型的运行时判断
  9. layer中嵌套的页面如何操作父页面_layui框架中layer父子页面交互详细解说
  10. JAXB vs XStream
  11. 【剑指offer】面试题49:丑数
  12. 编程书籍阅读随谈(第一篇)
  13. 广义表头尾链表存储结构_单向循环链表的存储结构和操作
  14. 围观人类弹琴后,Facebook的AI学会了假装演奏
  15. Android Studio 查询RecyclerView最新依赖方法
  16. ListControl
  17. 【电脑运用及修理】连接不到网站服务器解决方案【win10】
  18. ArduinoUNO实战-第十二章-累加器实验
  19. MFC添加afx_msg点击事件
  20. 红米5a android 版本,#MIUI#关于红米手机4高配版 Android版本适配的说明【miui9吧】_百度贴吧...

热门文章

  1. 在虚拟机上安装Kali Linux的简易指南
  2. jqueryui手风琴_jQueryUI手风琴插件
  3. scala 抽象方法_Scala抽象类型
  4. python 变量命名空间_Python命名空间– Python变量范围
  5. angularjs 实例_AngularJS服务示例教程
  6. jaxb-xjc.jar_jaxb2-maven-plugin XJC示例,用于从XSD生成Java类
  7. Java数组– java.util.Arrays
  8. java 文件分割器_Java分割器
  9. 产品运营必备的素质有哪些
  10. WinForm XML