【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 ...
随机推荐
- vim在vps内的终端内支持molokai
vps的终端内默认的颜色数好像很低.对molokai的支持一直不好. 后查找后得知:vim终端方式默认为16色,而molokai为256配色方案 我以为这是硬件问题,没有解决办法,一直到有一天,我在配 ...
- 二叉排序树及其C代码
1.二叉排序树的定义 二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree).其定义为:二叉排序树或者是空树, 或者是满足例如以下性质的二叉树 ...
- cocos2d-x与ISO内存管理(转)
一,IOS与图片内存 在IOS上,图片会被自动缩放到2的N次方大小.比如一张1024*1025的图片,占用的内存与一张1024*2048的图片是一致的.图片占用内存大小的计算的公式是:长*宽*4.这样 ...
- 18. Subsets II【medium】
Given a list of numbers that may has duplicate numbers, return all possible subsets Notice Each elem ...
- linux中使用lftp上传下载文件
lftp是linux中一款ftp服务器相比windows中的ftp显得要复杂不少了,下面我来总结一下lftp文件上传,文件下载,及文件查找等等相关命令吧. lftp连接的几种方法,最常用的是lftp ...
- Xcode常见问题
今天真机测试的时候,突然出现了这个错误: not have an architecture that “Administrator”的 iPhone (3) can execute. 原因是我刚刚修 ...
- python-爬图小样
python-爬某页面图 注意:python3+版本与python2有一定区别,需要注意多点. #! /usr/bin/env python3.5.4 # coding=utf-8 # 爬百度某贴吧页 ...
- 抹掉Scala的糖衣(14) -- Update Method
欢迎关注我的新博客地址:http://cuipengfei.me/ 在Scala中,名字叫做update的方法是有特殊作用的. 比如: 1 2 3 val scores = new scala.col ...
- IOS 中微信 网页授权报 key[也就是code]失效 解决办法
枪魂微信平台ios手机点击返回 网页授权失败,报key失效.已经解决,原因是授权key只能使用一次,再次使用就会失效. 解决办法:第一次从菜单中进行授权时,用session记录key和open_id. ...
- 在Javascript弹出窗口中输入换行符
private void showMessage(string strMsg) { Page.RegisterStartupScript("scriptStr", "&l ...