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 ...
随机推荐
- rpm命令怎么指定安装位置
rpm -ivh --prefix=路径 FILE.rpm
- AcWing 338. 计数问题
#include <iostream> #include <algorithm> #include <vector> using namespace std; ; ...
- python前后台交互相关配置
全局配置:全局样式.配置文件 在js下创建setting.js ,配置全局的settings.js import settings from '@/assets/js/settings' Vue.pr ...
- 浏览器的主要构成High Level Structure
浏览器的主要组件包括: 1. 用户界面- 包括地址栏.后退/前进按钮.书签目录等,也就是你所看到的除了用来显示你所请求页面的主窗口之外的其他部分 2. 浏览器引擎- 用来查询及操作渲染 ...
- nodejs后台运行的方法
nohup node ***.js & 这种方法可以,但存在你无法查询日志等问题 在SSH里另一个有效的方法是screen命令. [转]http://www.9usb.net/201002/l ...
- C#中通过SendARP读取MAC地址
C#中通过SendARP读取MAC地址: using System.Runtime.InteropServices; publicstaticstring GetMacBySendARP(string ...
- 题解 P5587 【打字练习】
P5587 打字练习 想发一篇较为简洁易懂的题解,代码看起来长,实际上还是很好理解的,而且很多对称着写就行了 一道字符串签到题,比赛的时候小蒟蒻调了一个小时都没调出来一直RE,坑点还是不少的(主要是我 ...
- css transform 2D3D转换
2D转换 translate 移动 <style> div{ width: 100px; height: 100px; } .box{ border: 1px dashed red; fl ...
- Maven安装与学习
一.安装 1.下载http://maven.apache.org/download.cgi 2.选择zip格式 安装完后解压到某一位置(E:\xitong\major\apache-maven-3.6 ...
- Day3 集合
数组与集合的区别 数组可以看作是一种集合,但是数组初始化后大小不可变:数组只能按索引顺序存取. https://www.cnblogs.com/tiandi/p/10641773.html Java标 ...