【POJ2516】Minimum Cost

题意:有N个收购商、M个供应商、K种物品。对于每种物品,每个供应商的供应量和每个收购商的需求量已知、每个供应商与每个收购商之间运送该物品的运费已知。求满足收购商要求的前提下的最小运费。(n,M,K<=50,每种物品供求量<=3,单位运费<=100)

题解:怎么看怎么是最小费用流,但是刚学的KM算法,还是要用一下的~

由于各种物品间没有影响,所以可以将k种物品拆开;由于供求量≤3,所以可以将供应商和收购商都拆开,然后跑KM算法。

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n,m,N,M,K,now,ret,temp,ans;
int va[200],vb[200],pri[60][60][60],from[200],la[200],lb[200];
int sa[60][60],sb[60][60],ba[200],bb[200],v[200][200];
int dfs(int x)
{
va[x]=1;
for(int i=1;i<=M;i++)
{
if(vb[i]) continue;
if(la[x]+lb[i]-v[x][i]==0)
{
vb[i]=1;
if(!from[i]||dfs(from[i]))
{
from[i]=x;
return 1;
}
}
else temp=min(temp,la[x]+lb[i]-v[x][i]);
}
return 0;
}
int calc()
{
int i,j,ret=0;
N=M=0;
for(i=1;i<=n;i++)
while(sa[i][now]--)
ba[++N]=i;
for(i=1;i<=m;i++)
while(sb[i][now]--)
bb[++M]=i;
if(N>M) return -1;
memset(la,0x80,sizeof(la));
memset(lb,0,sizeof(lb));
for(i=1;i<=N;i++)
for(j=1;j<=M;j++)
v[i][j]=-pri[ba[i]][bb[j]][now],la[i]=max(la[i],v[i][j]);
memset(from,0,sizeof(from));
for(i=1;i<=N;i++)
{
while(1)
{
memset(va,0,sizeof(va));
memset(vb,0,sizeof(vb));
temp=1<<29;
if(dfs(i)) break;
if(temp==(1<<29)) return -1;
for(j=1;j<=N;j++) if(va[j]) la[j]-=temp;
for(j=1;j<=M;j++) if(vb[j]) lb[j]+=temp;
}
}
for(i=1;i<=N;i++) ret+=la[i];
for(i=1;i<=M;i++) ret+=lb[i];
return -ret;
}
void work()
{
int i,j,k;
for(i=1;i<=n;i++)
for(k=1;k<=K;k++)
scanf("%d",&sa[i][k]);
for(i=1;i<=m;i++)
for(k=1;k<=K;k++)
scanf("%d",&sb[i][k]);
for(k=1;k<=K;k++)
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&pri[i][j][k]);
ans=0;
for(now=1;now<=K;now++)
{
k=calc();
if(k==-1)
{
printf("-1\n");
return ;
}
ans+=k;
}
printf("%d\n",ans);
}
int main()
{
while(scanf("%d%d%d",&n,&m,&K)!=EOF)
{
if(n==0) return 0;
work();
}
}

【POJ2516】Minimum Cost的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【Leetcode】【Easy】Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  3. 【leetcode】Minimum Depth of Binary Tree

    题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

  4. 【leetcode】Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  5. 【leetcode】Minimum Window Substring (hard) ★

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  6. 【leetcode】Minimum Depth of Binary Tree (easy)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  7. 【hdu1394】Minimum Inversion Number

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

  8. 【leetcode】Minimum Path Sum(easy)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  9. 【leetcode】Minimum Size Subarray Sum(middle)

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

随机推荐

  1. python 2/3 joblib.dump() 和 joblib.load()

    在python2中加载python3训练和保存的模型时出错: ValueErrorTraceback (most recent call last) --> 237 clf = joblib.l ...

  2. 虚拟键盘 input 挡住

    https://mp.weixin.qq.com/s?__biz=MjM5MTA1MjAxMQ==&mid=2651226929&idx=1&sn=4587bf582dcb23 ...

  3. 利用docker-maven-plugin快速交测

    目的 由开发环境交测的时候,通过docker镜像简化环境搭建及项目部署的过程. 环境描述 项目开发环境: windowns7 在windowns7中通过VMware Workstation安装Cent ...

  4. XMPP 基础 (转载)

    技术博客 http://www.cnblogs.com/ChenYilong/  新浪微博 http://weibo.com/luohanchenyilong Fullscreen XMPP   基础 ...

  5. python—networkx:在一张图中画出多个子图

    通过plt.subplot能够在一张图中画出多个子图 #coding: utf-8 #!/usr/bin/env python """ Draw a graph with ...

  6. 每日英语:When Social Skills Are A Warning

    An uncle starts believing all your sarcastic comments. Or a kindhearted friend never understands any ...

  7. 容易造成JavaScript内存泄露几个方面

    高效的JavaScript Web应用必须流畅,快速.与用户交互的任何应用程序,都需要考虑如何确保内存有效使用,因为如果消耗过多,页面就会崩溃,迫使用户重新加载.而你只能躲在角落哭泣. 自动垃圾收集是 ...

  8. C#如何调用其他.config配置文件,就是2个乃至3个以上的config文件

    XmlDocument xDoc = new XmlDocument(); try { xDoc.Load(配置文件路径); XmlNode xNode; XmlElement xElem; xNod ...

  9. Linux(centOS)手动安装删除Apache+MySQL+PHP+Memcached原创无错版

    =================== 第一步:Apache安装 =================== 下载 去 http://www.apache.org 自己找最新的 国内有很多镜像点例如人人和 ...

  10. 大数据(10) - HBase的安装与使用

    HBaes介绍 HBase是什么? 数据库 非关系型数据库(Not-Only-SQL) NoSQL 强依赖于HDFS(基于HDFS) 按照BigTable论文思想开发而来 面向列来存储 可以用来存储: ...