题意:有n个商店,每个商店有k种货物,每个货物需要a[n][k]个,有m个仓库,每个仓库也有k种货物,每个货物有b[m][k]个,然后k个矩阵,每个矩阵都是n*m的,第i行第j列表示从仓库j到商店i每单位k货物的花费,问你最小的花费满足商店,不行输出-1;

解题思路:刚开始以为是拆点费用流,然后会超时。。。后面看别人是直接对每一种货物都建图跑费用流,这样就行了,建一个汇点和源点,源点连向仓库,仓库连向商店,商店连向汇点;

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=100500;
const int inf=0x3f3f3f3f;
struct Edge
{
int next;
int to;
int w;
int cost;
}edge[maxn];
int head[maxn],dist[maxn],pre[maxn],path[maxn];
int cnt,x,y,w,n,m,k;
int Start,End;
int a[205][205],b[205][205],c[205][205][205];
int need[205],sup[205];
void add(int u,int v,int w,int cost)
{
// cout<<u<<" "<<v<<" "<<w<<" "<<cost<<endl;
edge[cnt].next=head[u];edge[cnt].to=v;
edge[cnt].w=w;edge[cnt].cost=cost;head[u]=cnt++;
//建回边
edge[cnt].next=head[v];edge[cnt].to=u;
edge[cnt].w=0;edge[cnt].cost=-cost;head[v]=cnt++;
}
bool spfa(int s,int t)
{
memset(pre,-1,sizeof(pre));
memset(dist,inf,sizeof(dist));
dist[s]=0;
queue<int>q;
q.push(s);
while(!q.empty())//不能有环,建图的时候也要注意
{
int u=q.front();q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].w>0&&dist[u]+edge[i].cost<dist[v])//这条路径存在且能被优化
{
dist[v]=dist[u]+edge[i].cost;
pre[v]=u;path[v]=i;q.push(v);
}
}
}
if(pre[t]==-1)
return false;
return true;
}
int mincost(int s,int t)
{
int cost=0;int flow=0;
while(spfa(s,t))
{
int tempflow=inf;
for(int u=t;u!=s;u=pre[u])//找最小的流量
{
if(edge[path[u]].w<tempflow)
tempflow=edge[path[u]].w;
}
flow+=tempflow;//每增广一次能得到的流量;
cost+=dist[t]*tempflow;//花费
//cost+=dist[t];
for(int u=t;u!=s;u=pre[u])
{
edge[path[u]].w-=tempflow;
edge[path[u]^1].w+=tempflow;
}
//cout<<cost<<endl;
}
return cost;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
memset(need,0,sizeof(need));
memset(sup,0,sizeof(sup));
Start=0;End=n+m+1;
int flag=0;
if(n==0&&m==0&&k==0)
break;
for(int i=1;i<=n;i++)//商店需要的
for(int j=1;j<=k;j++)
{
scanf("%d",&a[i][j]);
need[j]+=a[i][j];
}
for(int i=1;i<=m;i++)//仓库提供的
for(int j=1;j<=k;j++)
{
scanf("%d",&b[i][j]);
sup[j]+=b[i][j];
}
for(int i=1;i<=k;i++)//最小花费
for(int j=1;j<=n;j++)
for(int l=1;l<=m;l++)
scanf("%d",&c[i][j][l]);
for(int i=1;i<=k;i++)
{
if(sup[i]<need[i])
{
flag=1;
}
}
if(flag)
{
printf("-1\n");continue;
}
int ans=0;
for(int i=1;i<=k;i++)
{
memset(head,-1,sizeof(head));cnt=0;
for(int j=1;j<=m;j++)
add(Start,j,b[j][i],0);
for(int j=1;j<=n;j++)
add(j+m,End,a[j][i],0); for(int j=1;j<=n;j++)
for(int l=1;l<=m;l++)
{
add(l,j+m,inf,c[i][j][l]);
}
ans+=mincost(Start,End);
}
printf("%d\n",ans);
}
}

  

poj-2516(最小费用流)的更多相关文章

  1. POJ 2516 最小费用流

    依然最小费用最大流模板题 建边麻烦了些 #include <cstdio> #include <cstring> #include <iostream> #incl ...

  2. POJ 2516 Minimum Cost (网络流,最小费用流)

    POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...

  3. Poj 2516 Minimum Cost (最小花费最大流)

    题目链接: Poj  2516  Minimum Cost 题目描述: 有n个商店,m个仓储,每个商店和仓库都有k种货物.嘛!现在n个商店要开始向m个仓库发出订单了,订单信息为当前商店对每种货物的需求 ...

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

    POJ 2516 Minimum Cost 链接:http://poj.org/problem?id=2516 题意:有M个仓库.N个商人.K种物品.先输入N,M.K.然后输入N行K个数,每一行代表一 ...

  5. POJ - 2516 Minimum Cost 每次要跑K次费用流

    传送门:poj.org/problem?id=2516 题意: 有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的.问为了满足不同买家的订单的最小的花费. 思路: 设立一个源 ...

  6. POJ 2516:Minimum Cost(最小费用流)

    https://vjudge.net/problem/11079/origin 题意:有N个商店和M个供应商和K种物品,每个商店每种物品有一个需求数,每个供应商每种物品有一个供应量,供应商到商店之间的 ...

  7. 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个 ...

  8. POJ 2516 Minimum Cost 最小费用流 难度:1

    Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 13511   Accepted: 4628 Des ...

  9. POJ 2516 Minimum Cost(最小费用流)

    Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...

  10. POJ 2516 Minimum Cost(拆点+KM完备匹配)

    题目链接:http://poj.org/problem?id=2516 题目大意: 第一行是N,M,K 接下来N行:第i行有K个数字表示第i个卖场对K种商品的需求情况 接下来M行:第j行有K个数字表示 ...

随机推荐

  1. javaweb项目创建和虚拟主机配置

    首先点击File-àNew-àWeb [roject-à在Projcet Name里写项目名-à点击finish-à会出来一个框,选择NO,一个javaweb项目就创建好了.具体请看下图! 配置服务器 ...

  2. (二)阿里云ECS Linux服务器外网无法连接MySQL解决方法(报错2003- Can't connect MySQL Server on 'x.x.x.x'(10038))(自己亲身遇到的问题是防火墙的问题已经解决)

    我的服务器买的是阿里云ECS linux系统.为了更好的操作数据库,我希望可以用navicat for mysql管理我的数据库. 当我按照正常的模式去链接mysql的时候, 报错提示: - Can' ...

  3. Selenium自动化-CSS元素定位

    接下来,开始讲解 CSS元素定位. CSS定位速度快,功能多,但是不能向上查找,比 xpath好用,是本人认为最好用的定位方式   大致用法总结: 具体使用仿上篇博客.http://www.cnblo ...

  4. win10的react native 开发环境搭建,使用Android模拟器

    1.打开cmd的管理员模式,win+X,选择命令提示符(管理员)即可,运行如下命令: @"%SystemRoot%\System32\WindowsPowerShell\v1.0\power ...

  5. QT获取本机IP和Mac地址

    #include <QNetworkInterface> #include <QList> void MainWindow::getIPPath() { QString str ...

  6. MongoDB副本集功能及节点属性梳理

    副本集的主要功能 副本集是MongoDB高可用的基础,其主要作用 归纳为以下几点: (1)高可用,防止设备(服务器.网络)故障.提供自动FailOver功能. (2)无需配置高可用性虚拟节点:无论是S ...

  7. ASP.MVC学习资源总结

    自己动手写一个简单的MVC框架(第一版) 自己动手写一个简单的MVC框架(第二版) ASP.Net请求处理机制初步探索之旅 - Part 1 前奏 ASP.Net请求处理机制初步探索之旅 - Part ...

  8. c/c++ linux 进程 fork wait函数

    linux 进程 fork wait函数 fork:创建子进程 wait:父进程等待子进程结束,并销毁子进程,如果父进程不调用wait函数,子进程就会一直留在linux内核中,变成了僵尸进程. for ...

  9. Linix基本命令

    基本命令关机:shutdown -h halt init 0 poweroff重启:shutdown -r reboot init 6pwd:查看工作目录ls:查看指定目录的内容-l:列表显示-a:显 ...

  10. XML详解二XML的解析与创建

    XML用来传输和存储数据,如何解析获取到的XML文本呢? 一.解析XML 创建demo.xml文件: <?xml version="1.0" encoding="U ...