POJ 2516 Minimum Cost 最小费用流
题目:
给出n*kk的矩阵,格子a[i][k]表示第i个客户需要第k种货物a[i][k]单位。
给出m*kk的矩阵,格子b[j][k]表示第j个供应商可以提供第k种货物b[j][k]单位。
再给出k个n*m的矩阵,格子c[k][i][j]表示第k种货物由j供应商提供给客户i的话,每单位运费为c[k][i][j]。
问最小费用。
分析:
刚开始时,虽然考虑到每种货物其实是不相关的,但是想到在跑费用流时应该没多大影响,所以直接建图,跑最小费用流,TLE了。。。
后来对于每种货物单独来考虑,即建图之后跑一次最小费用流,进行k次求费用流之后,判断总流是否等于需求的货物数量。这样建图,节点数明显减少了很多,实际效果非常明显。
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ char IN;
inline void Int(int &x){
while(!isdigit(IN=getchar()));
x = IN-'0';
while(isdigit(IN=getchar()))
x = x*10+IN-'0';
} /******** program ********************/ const int MAXN = 205;
const int MAXM = 100005;
const int INF = 1e9;
const int X = 52; int pre[MAXN],dis[MAXN];
int po[MAXN],tol;
bool use[MAXN];
int q[MAXM],head,tail;
int n,m,vs,vt,ans; int a[X][X],b[X][X],c[X][X][X];
int kk,flow; struct node{
int y,f,cost,next;
}edge[MAXM]; void Add(int x,int y,int f,int cost){
edge[++tol].y = y;
edge[tol].f = f;
edge[tol].cost = cost;
edge[tol].next = po[x];
po[x] = tol;
} void add(int x,int y,int f,int cost){
Add(x,y,f,cost);
Add(y,x,0,-cost);
} bool spfa(){
memset(use,false,sizeof(use));
rep1(i,vt)
dis[i] = INF;
dis[vs] = 0;
head = tail = 0;
q[tail++] = vs;
pre[vs] = 0;
while(head<tail){
int x = q[head++];
use[x] = false;
for(int i=po[x];i;i=edge[i].next){
int y = edge[i].y;
if(edge[i].f>0&&edge[i].cost+dis[x]<dis[y]){
dis[y] = dis[x]+edge[i].cost;
pre[y] = i;
if(!use[y]){
use[y] = true;
q[tail++] = y;
}
}
}
}
if(dis[vt]==INF)
return false; int aug = INF;
for(int i=pre[vt];i;i=pre[edge[i^1].y])
aug = min(aug,edge[i].f);
for(int i=pre[vt];i;i=pre[edge[i^1].y]){
edge[i].f -= aug;
edge[i^1].f += aug;
}
ans += dis[vt]*aug;
return true;
} inline void fareFlow(int k){
Clear(po);
tol = 1;
vs = MAXN-3;
vt = vs+1;
rep1(i,n)if(a[i][k])
add( vs,i,a[i][k],0 );
rep1(j,m)if(b[j][k]){
add( j+n,vt,b[j][k],0 );
rep1(i,n)
add(i,j+n,b[j][k],c[k][i][j]);
}
while(spfa())
;
for(int i=po[vt];i;i=edge[i].next)
if(edge[i].f>0)
flow += edge[i].f;
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif while(true){
Int(n);Int(m);Int(kk);
if(!n&&!m&&!kk)return 0; int tot = 0;
rep1(i,n)
rep1(k,kk){
Int(a[i][k]);
tot += a[i][k];
}
rep1(j,m)
rep1(k,kk)
Int(b[j][k]);
rep1(k,kk)
rep1(i,n)
rep1(j,m)
Int(c[k][i][j]); flow = 0;
ans = 0;
rep1(k,kk)
fareFlow(k); printf("%d\n",tot==flow?ans:-1);
} return 0;
}
POJ 2516 Minimum Cost 最小费用流的更多相关文章
- POJ 2516 Minimum Cost 最小费用流 难度:1
Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 13511 Accepted: 4628 Des ...
- POJ 2516 Minimum Cost (网络流,最小费用流)
POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...
- Poj 2516 Minimum Cost (最小花费最大流)
题目链接: Poj 2516 Minimum Cost 题目描述: 有n个商店,m个仓储,每个商店和仓库都有k种货物.嘛!现在n个商店要开始向m个仓库发出订单了,订单信息为当前商店对每种货物的需求 ...
- POJ 2516 Minimum Cost (最小费用最大流)
POJ 2516 Minimum Cost 链接:http://poj.org/problem?id=2516 题意:有M个仓库.N个商人.K种物品.先输入N,M.K.然后输入N行K个数,每一行代表一 ...
- POJ 2516 Minimum Cost(最小费用流)
Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...
- POJ 2516 Minimum Cost (费用流)
题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...
- POJ - 2516 Minimum Cost 每次要跑K次费用流
传送门:poj.org/problem?id=2516 题意: 有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的.问为了满足不同买家的订单的最小的花费. 思路: 设立一个源 ...
- POJ 2516 Minimum Cost(拆点+KM完备匹配)
题目链接:http://poj.org/problem?id=2516 题目大意: 第一行是N,M,K 接下来N行:第i行有K个数字表示第i个卖场对K种商品的需求情况 接下来M行:第j行有K个数字表示 ...
- POJ 2516 Minimum Cost [最小费用最大流]
题意略: 思路: 这题比较坑的地方是把每种货物单独建图分开算就ok了. #include<stdio.h> #include<queue> #define MAXN 500 # ...
随机推荐
- Spring中的设计模式
[Spring中的设计模式] http://www.uml.org.cn/j2ee/201301074.asp [详解设计模式在Spring中的应用] [http://www.geek521.c ...
- bootStrap modal无法滚动处理
bug:在大显示器上,模态框无法滚动,改变浏览器窗口大小,模态框可以滚动. 处理:模态框显示后,执行resize.或者直接调用handleUpdate 'shown.bs.modal #orderDe ...
- 表单input按钮在各浏览器之间的兼容性
从网上看了这篇关于表单input按钮的浏览器兼容性问题,总结的还不错,所以copy下来学习下. input按钮在各个浏览器之间的兼容性问题,看下边这段代码: input.item { backgrou ...
- 设置文字在div中垂直居中,使用line-height
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-02-19) line-height 行间距 详细使用方法:http://www.w3school.com.cn/css/pr ...
- Cocos2dx Widget button透明区域过滤
小伟哥 遇到一个命题: button透明区域过滤.当点击一个建筑button.花的时候不得不想一些方法把点击透明区域过滤掉. 让点击也没有效果滴啦. 開始搜索了半天才有所思路. 在网络上非常多贴代码的 ...
- C# DataGridView中合并单元格
/// 合并GridView列中相同的行 /// /// GridView对象 /// 需要合并的列 public static void GroupRows(GridView GridView1, ...
- BZOJ 2038 [2009国家集训队]小Z的袜子 莫队
2038: [2009国家集训队]小Z的袜子(hose) 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Descriptionw ...
- 免费LInux主机资源
一.m-net.arbornet.org注冊 (1)telnet m-net.arbornet.org vista系统默认是关闭telnet的(由于不安全),须要开启.cmd->telnet(2 ...
- Gridview中绑定DropDownList
1.页面代码 <asp:TemplateField HeaderText="等级"> ...
- Java HttpClient使用小结
httpclient是apache的一个项目:http://hc.apache.org/ 文档比較完好:http://hc.apache.org/httpcomponents-client-ga/tu ...