poj 2516 最小费用最大流
来源:互联网 发布:中国出境游数据 编辑:程序博客网 时间:2024/05/22 01:39
- 最佳匹配的题,m个仓库供应k种商品给n个商家,m*n条运输代价互异,求满足商家需求下的最小运输费用
- 显然,如果某种商品的供货量比需求大,肯定是无法达到要求的,所以开始要判别是否可以得到最佳匹配
- 这个题非常有启发意义,刚开始把k种商品一并考虑,tle了,想来也是,这样X集合中的点为m*k,Y中集合为n*k;
- 执行最小费用流这个复杂度为O(n^3)的算法在这个题目是不可以行的
- 考虑到k种商品是相互独立的,所以见k次图,每次图中的点为n+m+2个,这样就可以在时限内出答案了
- */
- #include <stdio.h>
- #include <iostream>
- #include <string.h>
- #include<queue>
- #include<cmath>
- using namespace std;
- const int M=5010,ME=5000000;
- const int INF=0x3f3fffff;
- //******************************
- int Head[M],Next[ME],Num[ME],Flow[ME],Cap[ME],Cost[ME],Q[M],InQ[M],Len[M],pre_edge[M];
- class MaxFlow
- {
- public:
- void clear()
- {
- memset(Head,-1,sizeof(Head));
- memset(Flow,0,sizeof(Flow));
- }
- void addedge(int u,int v,int cap,int cost)
- {
- Next[top] = Head[u];
- Num[top] = v;
- Cap[top] = cap;
- Cost[top] = cost;
- Head[u] = top++;
- Next[top] = Head[v];
- Num[top] = u;
- Cap[top] = 0;
- Cost[top] = -cost;
- Head[v] = top++;
- }
- int solve(int s,int t) //返回最终的cost
- {
- int cost = 0;
- while(SPFA(s,t))
- {
- int cur = t,minflow = INF;
- while(cur != s)
- {
- if(minflow > Cap[pre_edge[cur]]-Flow[pre_edge[cur]])
- minflow = Cap[pre_edge[cur]]-Flow[pre_edge[cur]];
- cur = Num[pre_edge[cur] ^ 1];
- }
- cur = t ;
- while(cur != s)
- {
- Flow[pre_edge[cur]] += minflow;
- Flow[pre_edge[cur] ^ 1] -= minflow;
- cost += minflow * Cost[pre_edge[cur]];
- cur = Num[pre_edge[cur] ^ 1];
- }
- }
- return cost;
- }
- private:
- bool SPFA(int s,int t)
- {
- fill(Len,Len+M,INF);
- Len[s]=0;
- int head = -1,tail = -1,cur;
- Q[++head] = s;
- while(head != tail)
- {
- ++tail;
- if(tail >= M) tail = 0 ;
- cur = Q[tail];
- for(int i = Head[cur];i != -1;i = Next[i])
- {
- if(Cap[i]>Flow[i] && Len[Num[i]] > Len[cur] + Cost[i])
- {
- Len[Num[i]] = Len[cur] + Cost[i];
- pre_edge[Num[i]] = i;
- if(!InQ[Num[i]])
- {
- InQ[Num[i]]=true;
- ++head;
- if(head >= M) head = 0;
- Q[head] = Num[i];
- }
- }
- }
- InQ[cur]=false;
- }
- return Len[t] != INF;
- }
- int top;
- }my;
- //******************************
- int need[55][55],canneed[55];
- int suply[55][55],cansuply[55];
- int trans[55][55][55];
- int main()
- {
- int n,m,k;
- while(scanf("%d%d%d",&n,&m,&k),n+m+k)
- {
- memset(canneed,0,sizeof(canneed));
- memset(cansuply,0,sizeof(cansuply));
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=k;j++)
- {
- scanf("%d",&need[i][j]);
- canneed[j]+=need[i][j];// 第i个客户j商品的订货量
- }
- }
- for(int i=1;i<=m;i++)
- {
- for(int j=1;j<=k;j++)
- {
- scanf("%d",&suply[i][j]);
- cansuply[j]+=suply[i][j];//第i个仓库提供的j商品的量
- }
- }
- for(int i=1;i<=k;i++)
- {
- for(int j=1;j<=n;j++)
- {
- for(int f=1;f<=m;f++)
- {
- scanf("%d",&trans[i][j][f]);//把单位商品i从仓库f运到商户j所需的费用
- }
- }
- }
- int flag=1;
- for(int i=1;i<=k;i++)
- if(cansuply[i]<canneed[i])
- {
- flag=0;
- break;
- }
- if(!flag){puts("-1");continue;}
- int ans=0;
- for(int f=1;f<=k;f++)
- {
- my.clear();
- for(int i=1;i<=m;i++)
- {
- my.addedge(0,i,suply[i][f],0);
- for(int g=1;g<=n;g++)
- {
- my.addedge(i,m+g,suply[i][f],trans[f][g][i]);
- }
- }
- for(int i=1;i<=n;i++)
- {
- my.addedge(m+i,m+n+1,need[i][f],0);
- }
- ans+=my.solve(0,m+n+1);
- }
- printf("%d\n",ans);
- }
- return 0;
- }
0 0
- poj 2516 最小费用最大流
- 【最小费用最大流】POJ 2516【unsolved】
- POJ 2516(最小费用最大流)
- poj 2516 最小费用最大流
- poj 2516 最小费用最大流
- poj 2516 最小费用最大流
- poj 2516(最小费用最大流)
- POJ-2516-最小费用最大流
- POJ 2516 Minimum Cost 【MCMF:最小费用最大流】
- POJ 2516 Minimum Cost 最小费用最大流
- POJ 2516 Minimum Cost (最小费用最大流,KM解法)
- poj 2516 Minimum Cost--最小费用最大流
- poj-2516-Minimum Cost-最小费用最大流
- POJ 2516 Minimum Cost (最小费用最大流)
- poj 2516 Minimum Cost(最小费用最大流)
- poj 2516 Minimum Cost (最小费用最大流)
- POJ 2516 Minimum Cost 最小费用最大流
- poj 2516 Minimum Cost(最小费用最大流)
- EM算法嗅探
- Sencha Touch入门:Sencha Touch开发环境搭建及使用 Sencha Cmd 自动创建项目框架
- python整理笔记2(初学者需注意--自己曾遇到的问题)
- SqlServer 列数据比对 转载整理
- C++ Primer Plus第六版编程练习12.1解答
- poj 2516 最小费用最大流
- Leetcode第三题_Longest Substring Without Repeating Characters
- 【C语言】DigitSum(n) (递归函数)
- 小白也刷题-----leetcode去除数组中指定元素
- java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory的解决
- 盘点有哪些国家自主研发自己的操作系统
- C++ Primer Plus第六版编程练习12.2解答
- Struts2中拦截器的配置
- 双链表的实现及其功能大全!!!