POJ2516 Minimum Cost
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=;
const int inf=1e9;
int cap[maxn][maxn];
int flow[maxn][maxn];
int cost[maxn][maxn];
int n;
int f;//最大流
int c;//最小费用
int st,ed;
bool visit[maxn];
int pre[maxn],dist[maxn];
bool spfa () {
queue<int> q;
for (int u=;u<=n;u++) {
if (u==st) {
q.push(u);
dist[u]=;
visit[u]=true;
}
else {
dist[u]=inf;
visit[u]=false;
}
}
while (!q.empty()) {
int u=q.front();
visit[u]=false;
q.pop();
for (int v=;v<=n;v++) {
if (cap[u][v]>flow[u][v]&&dist[v]>dist[u]+cost[u][v]) {
dist[v]=dist[u]+cost[u][v];
pre[v]=u;
if (!visit[v]) {
visit[v]=true;
q.push(v);
}
}
}
}
if (dist[ed]>=inf) return false;
return true;
}
void minCostMaxflow () {
memset (flow,,sizeof(flow));
c=f=;
while (spfa()) {
int Min=inf;
for (int u=ed;u!=st;u=pre[u])
Min=min(Min,cap[pre[u]][u]-flow[pre[u]][u]);
for (int u=ed;u!=st;u=pre[u]) {
flow[pre[u]][u]+=Min;
flow[u][pre[u]]-=Min;
}
c+=dist[ed]*Min;
f+=Min;
}
}
int order[maxn][maxn];
int supply[maxn][maxn];
int tolorder[maxn];
int main() {
int N,M;
int K;
while(scanf("%d%d%d",&N,&M,&K)!=EOF) {
if(N==&&M==&&K==)break;
st=;
n=N+M+;
ed=n;
memset(cap,,sizeof(cap));
memset(cost,,sizeof(cost));
for(int i=;i<N;i++)
for(int j=;j<K;j++)
scanf("%d",&order[i][j]);
for(int i=;i<M;i++)
for(int j=;j<K;j++)
scanf("%d",&supply[i][j]);
for(int i=;i<K;i++) {
tolorder[i]=;
for(int j=;j<N;j++)
tolorder[i]+=order[j][i];
}
for(int i=;i<=M;i++)
for(int j=M+;j<=N+M;j++)
cap[i][j]=inf;
bool flag=true;
int ans=;
for(int i=;i<K;i++) {
for(int j=M+;j<=M+N;j++)
for(int k=;k<=M;k++) {
scanf("%d",&cost[k][j]);
cost[j][k]=-cost[k][j];
}
if(!flag)continue;
for(int j=;j<=M;j++)
cap[st][j]=supply[j-][i];
for(int j=;j<=N;j++)
cap[j+M][ed]=order[j-][i];
minCostMaxflow();
if(f<tolorder[i])flag=false;
else ans+=c;
}
if(!flag)printf("-1\n");
else printf("%d\n",ans);
}
return ;
}
POJ2516 Minimum Cost的更多相关文章
- POJ2516 Minimum Cost —— 最小费用最大流
题目链接:https://vjudge.net/problem/POJ-2516 Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Tota ...
- POJ2516:Minimum Cost(最小费用最大流)
Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19088 Accepted: 6740 题目链 ...
- poj-2516.minimum cost(k次费用流)
Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19883 Accepted: 7055 Des ...
- POJ2516 Minimum Cost(最小费用最大流)
一开始我把每个店主都拆成k个点,然后建图..然后TLE.. 看题解= =哦,愚钝了,k个商品是独立的,可以分别跑k次最小费用最大流,结果就是k次总和.. #include<cstdio> ...
- POJ2516 Minimum Cost【最小费用最大流】
题意: 有N个客户,M个仓库,和K种货物.已知每个客户需要每种货物的数量,每个仓库存储每种货物的数量,每个仓库运输各种货物去各个客户的单位费用.判断所有的仓库能否满足所有客户的需求,如果可以,求出最少 ...
- 【POJ2516】Minimum Cost
[POJ2516]Minimum Cost 题意:有N个收购商.M个供应商.K种物品.对于每种物品,每个供应商的供应量和每个收购商的需求量已知.每个供应商与每个收购商之间运送该物品的运费已知.求满足收 ...
- POJ 2516 Minimum Cost (网络流,最小费用流)
POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...
- Minimum Cost(最小费用最大流)
Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...
- POJ 2516 Minimum Cost (费用流)
题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...
随机推荐
- TOYS(计算几何-入门)
题目 ‘^’代表叉乘 ‘•’代表点乘 点积:a•b=ax*bx+ay*by 叉积:a^b=ax*by-bx*ay
- c++primer练习题
2.7 输出地址和姓名(1 #include <iostream> using namespace std; int main() { int 距离; cout << &quo ...
- Java 字符串、数值与16进制相互转化
字符串.数值与16进制相互转化 首先创建一个工具类: package c; public class DataUtils { /* * 字节数组转16进制字符串 */ public static St ...
- Rabbitmq consumer端超时报错
0x01 应用场景: 使用rabbitmq的exchange模式,type为direct,消费端不需要向生产端返回结果no_ack=True 其中某个consumer任务耗时较长(5min以上),结果 ...
- Codeforces Global Round 3:B. Born This Way
Born This Way原文链接:[传送门] 题目大意:潇洒哥想乘坐飞机从A地到达C地,但是没有直达的航班,在A地和B地之间有一个可以中转的航班B,潇洒哥想早点到达C地(有航班就坐),但是很不幸他得 ...
- js实现页面下拉后展示导航,以及点击导航自动滑动到相关页面
//监控,下拉750px后展示导航 $(window).scroll(function(){ var $this = $(this); var targetTop = $(this).scrollTo ...
- 题解【洛谷P5436】【XR-2】缘分
题目背景 世间万物都置身于缘分编织的大网中.缘分未到,虽历经千劫,却不能相遇.缘分到了,在草原上都能等到一艘船.--<一禅小和尚> 题目描述 一禅希望知道他和师父之间的缘分大小.可是如何才 ...
- 对于使用secureFX上传文件到centos7 的时候,以及不同的用户解压文件,对于文件操作权限的实验
本以为以一个用户胡如root登录了SecureFx,之后选择了root的家目录下的一个software目录,之后上传 以root用户远程登录LINUX系统 查看文件 之后再验证普通用户zhaijh登录 ...
- tomcat安装成功以后进行测试步骤:
tomcat安装成功以后进行测试步骤: 编写测试页面: 进入Tomcat安装对应路径: E:\Tomcat\apache-tomcat-8.5.45\webapps\ROOT 创建:test.jsp ...
- SoapUI学习(1)- WebService基础
转载:http://luyongxin88.blog.163.com/blog/static/92558072011320101454287/ WebService 它是一种构建应用程序的普遍模型,可 ...