【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. c语言中函数参数入栈的顺序是什么?为什么

    看到面试题C语言中函数参数的入栈顺序如何? 自己不知道,边上网找资料.下面是详细解释 #include <stdio.h> void foo(int x, int y, int z){   ...

  2. blender, knife工具

    点击Knife按钮开始切割,MLB在边上确认分割点,return键完成切割,MRB取消切割(这些在窗口下面的信息提示栏中都有写,如图中如示).

  3. mysqldump全量备份+mysqlbinlog二进制日志增量备份

    日常的数据备份及恢复测试,是DBA工作重中之重的事情,所以要做好备份及测试,日常的备份常见有mysqldump+binlog备份.xtrabackup+binlog备份,无论那一种,几乎都少不了对bi ...

  4. 在iOS App中增加完整的照片多选功能

    转自:http://blog.csdn.net/jasonblog/article/details/8141850 主要参考了ELCImagePickerController,不过由于UI展现上需要定 ...

  5. xUtils工具实现下载功能

    private String download_url="http://192.168.2.8:80/DownZip/*****.zip";//下载的路径 public  Stri ...

  6. 自己定义TextView 调用ttf格式字体

    方法一:自己定义TextView 调用ttf格式字体 <strong>将ttf格式文件存放在assets/fonts/下</strong> 注:PC系统字体存放在C:\Wind ...

  7. 今天遇到个PHP不知原因的报内部错误

    今天遇到个PHP不知原因的报内部错误 纠结了很久想尽了办法,1.apache日志 2.错误级别 ,还差点就把自己写的那个破烂不堪的日志系统加上去了 纠结了很久还是无果,在最终,最终发现了 原来是类命名 ...

  8. [misc]如何在嵌入式平台使用printf功能

    转自:http://www.cnblogs.com/liu_xf/archive/2011/04/14/2015726.html 摘要: 当我们在调试代码时,通常需要将程序中的某个变量打印至PC机上, ...

  9. 使用submit异步提交,阻止表单默认提交

    <form id="addForm" onSubmit="return false;"> <input type="submit&q ...

  10. ES6学习笔记(1,let和const)

    在介绍let和const之前我们先复习一下相关的知识点. 关于函数作用域 开发过程中,在ES6(ECMA2015)标准推出之前,声明变量的方式一直都是var,而变量的作用域一般也只在函数内部,即函数作 ...