【POJ2516】Minimum Cost
【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的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【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 ...
- 【leetcode】Minimum Depth of Binary Tree
题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【hdu1394】Minimum Inversion Number
Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...
- 【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 ...
- 【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 ...
随机推荐
- hive partition 分区使用
一.背景 1.在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据,因此建表时引入了partition概念. 2.分区表指的是在创建表 ...
- Java Persistence with MyBatis 小结1
数据持久层做的工作是1)将从数据库中查询到的数据生成需要的java对象:2)将 Java 对象中的数据通过 SQL 持久化到数据库中. MyBatis 通过抽象底层的 JDBC 代码,自动化 SQL ...
- Spring mvc注解方式使用事务回滚
项目名:1ma1ma jdbc.xml <bean id="dataSource" class="org.apache.commons.dbcp.BasicDat ...
- sql查看所有表大小的方法
sql查看所有表大小的方法. 代码: declare @id int ) declare @pages int declare @dbname sysname ,) ,) ,) create tabl ...
- delphi无边框可拖动窗体
unit UFrmModless; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, ...
- tornado异步web请求
1.为什么要使用异步web服务使用异步非阻塞请求,并发处理更高效. 2.同步与异步请求比较同步请求时,web服务器进程是阻塞的,也就是说当一个请求被处理时,服务器进程会被挂起直至请求完成. 异步请求时 ...
- java 清除 bom
参考工具 http://akini.mbnet.fi/java/unicodereader/ Utf8BomRemover 清除bom的方法 package cn.com.do1.component ...
- C++基本功之Operator
废话不多说,这次讲的是 Operator overload. 关于operator, 在 < The C++ Programing Language > 里的描述,可以用做overload ...
- golang 环境bash 以及shell
standard_init_linux.go:178: exec user process caused "no such file or directory" 2018年04月2 ...
- alert的美化,并且随滚动条滚动
onclick="sAlert('${vo.courseName}');" <script type="text/javascript" language ...