POJ 2516:Minimum Cost(最小费用流)
https://vjudge.net/problem/11079/origin
题意:有N个商店和M个供应商和K种物品,每个商店每种物品有一个需求数,每个供应商每种物品有一个供应量,供应商到商店之间的运输需要花费,如果供不应求输出-1,否则输出最小花费。
思路:比较明显的最小费用流。想法大概都是源点和供应商连一条容量为供应量,花费为0的边,商店和汇点之间连一条容量为需求量,花费为0的边,供应商和商店之间连一条容量为INF,花费为题意给出的花费的边。建图的话一开始是直接对于每一个商店每一种物品和每一个供应商每一种物品都看做一个点,这样的点数是n*k+m*k+两个源点,超级大的图,就TLE了。看了下别人的思路,每一种商品是独立的,那么对于每一种商品建一次图,这样的点数是n + m + 两个源点,然后跑 k 次。优化了N多。。。。太菜鸡了。。。。还有数组开的不能太小,开55的时候TLE,105就AC了。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
#define N 105
typedef long long LL;
struct Edge {
int u, v, cap, cost;
Edge () {}
Edge (int u, int v, int cap, int cost) : u(u), v(v), cap(cap), cost(cost) {}
} edge[N*N];
int tot, pre[N], vis[N], dis[N], S, T;
int shop[N][N], sup[N][N], tolshop[N], tolsup[N], cost[N][N][N];
vector<int> G[N];
void Add(int u, int v, int cap, int cost) {
edge[tot] = Edge(u, v, cap, cost);
G[u].push_back(tot++);
edge[tot] = Edge(v, u, , -cost);
G[v].push_back(tot++);
} int SPFA() {
queue<int> que;
// puts("SPFA");
memset(dis, INF, sizeof(dis));
memset(vis, , sizeof(vis));
dis[S] = ; que.push(S);
vis[S] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
vis[u] = ;
for(int i = ; i < G[u].size(); i++) {
Edge& e = edge[G[u][i]];
if(dis[u] + e.cost < dis[e.v] && e.cap > ) { // 先松弛在判断是否在队里
dis[e.v] = dis[u] + e.cost;
pre[e.v] = G[u][i];
if(vis[e.v]) continue;
que.push(e.v);
vis[e.v] = ;
}
}
}
return dis[T] < INF;
} void MFMC(int &maxflow, int &cost) {
int u = T, flow = INF;
while(u != S) {
Edge& e = edge[pre[u]];
if(e.cap < flow) flow = e.cap;
u = e.u;
} u = T;
while(u != S) {
Edge& e1 = edge[pre[u]];
Edge& e2 = edge[pre[u]^];
e1.cap -= flow; e2.cap += flow;
cost += flow * e1.cost;
u = e1.u;
}
maxflow += flow;
} int main() {
int n, m, k;
while(~scanf("%d%d%d", &n, &m, &k), n + m + k) {
S = ; T = n + m + ;
memset(tolsup, , sizeof(tolsup));
memset(tolshop, , sizeof(tolshop));
for(int i = ; i <= n; i++) {
for(int j = ; j <= k; j++) {
scanf("%d", &shop[i][j]);
tolshop[j] += shop[i][j];
}
}
for(int i = ; i <= m; i++) {
for(int j = ; j <= k; j++) {
scanf("%d", &sup[i][j]);
tolsup[j] += sup[i][j];
}
}
for(int x = ; x <= k; x++) {
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
scanf("%d", &cost[x][i][j]);
}
}
}
int flag = ;
int maxflow = , mincost = ;
for(int x = ; x <= k; x++) {
if(tolshop[x] > tolsup[x]) {
flag = ; break;
}
tot = ;
for(int i = S; i <= T; i++) G[i].clear();
for(int i = ; i <= n; i++) {
Add(i, T, shop[i][x], );
}
for(int i = ; i <= m; i++) {
Add(S, i + n, sup[i][x], );
}
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
Add(j + n, i, sup[j][x], cost[x][i][j]);
}
}
while(SPFA()) MFMC(maxflow, mincost);
}
if(flag) printf("%d\n", mincost);
else puts("-1");
}
return ;
}
POJ 2516:Minimum Cost(最小费用流)的更多相关文章
- POJ 2516 Minimum Cost 最小费用流 难度:1
Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 13511 Accepted: 4628 Des ...
- POJ 2516 Minimum Cost 最小费用流
题目: 给出n*kk的矩阵,格子a[i][k]表示第i个客户需要第k种货物a[i][k]单位. 给出m*kk的矩阵,格子b[j][k]表示第j个供应商可以提供第k种货物b[j][k]单位. 再给出k个 ...
- POJ 2516 Minimum Cost (网络流,最小费用流)
POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...
- Poj 2516 Minimum Cost (最小花费最大流)
题目链接: Poj 2516 Minimum Cost 题目描述: 有n个商店,m个仓储,每个商店和仓库都有k种货物.嘛!现在n个商店要开始向m个仓库发出订单了,订单信息为当前商店对每种货物的需求 ...
- POJ 2516 Minimum Cost (最小费用最大流)
POJ 2516 Minimum Cost 链接:http://poj.org/problem?id=2516 题意:有M个仓库.N个商人.K种物品.先输入N,M.K.然后输入N行K个数,每一行代表一 ...
- POJ 2516 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 ...
- POJ - 2516 Minimum Cost 每次要跑K次费用流
传送门:poj.org/problem?id=2516 题意: 有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的.问为了满足不同买家的订单的最小的花费. 思路: 设立一个源 ...
- POJ 2516 Minimum Cost(拆点+KM完备匹配)
题目链接:http://poj.org/problem?id=2516 题目大意: 第一行是N,M,K 接下来N行:第i行有K个数字表示第i个卖场对K种商品的需求情况 接下来M行:第j行有K个数字表示 ...
- POJ 2516 Minimum Cost [最小费用最大流]
题意略: 思路: 这题比较坑的地方是把每种货物单独建图分开算就ok了. #include<stdio.h> #include<queue> #define MAXN 500 # ...
随机推荐
- 模拟ATM机银行系统
淄博汉企Java基础考核项目 模拟银行自助终端系统 一. 本系统模拟银行用户使用ATM机开户.查询.存款.取款功能,要求使用java语言编程实现. 说明: 1. 对于数据输入异常,可使用java异常处 ...
- bzoj1222: [HNOI2001]产品加工--DP
DP神题orz dp[i]表示机器1工作i小时,机器2工作dp[i]小时 那么对于每个任务: 选1:dp[i]=dp[i-a]; 选2:dp[i]=dp[i]+b; 选1+2:dp[i]=dp[i-c ...
- java web用于保持状态的4种方法
方法一:网址重写 通过在url地址后面添加若干的token作为查询字符串来实现.token的值一般为 键=值 url?key1=value1&key2=value2&...&k ...
- XML于JSON
XML:可拓展的标记语言(跨平台数据表现)用于保存数据 XML:标记需要关闭 :单根性 .NET中DOM常用对象: XmlDocument :一个XML文档 XmlNode:xml中的单个节点 Xml ...
- Ajax Post 与 Get 实例
Ajax的POST实例,index.html <html> <head> <script type="text/javascript"> fun ...
- 配置perl-cgi的运行环境,由于Active Perl安装在d:\perl
Apache 1.3.22 for Win32+PHP 4.0.6+Active Perl 5.006001+Zend Optimizer v1.1.0+mod_gzip 1.3.19.1a+MySQ ...
- Ogre代码学习之1——Ogre中地形lod的基础:deltaHeight的计算
Ogre的地形系统中的重要概念:高度差,英文HeightDeltas,表示某个完整细节中的顶点,在某个它被隐去的lod中被插值之后的高度和原始高度(即高度图中的高度)之差. DeltaHeight = ...
- Go-Agent原理分析及FQ介绍
作为一个程序员,相信大家是极度依赖google/stackoverflow/github的,可是国内有强大的GFW存在,以至于编程少了很多乐趣. 最近闹GFW狂潮,很多Chrome插件被封,连Shad ...
- WPF部署问题 解决:The application requires that the assembly...be installed in the GAC
vs-->引用-->找到问题类库-->邮件属性--->特定版本-->false done
- Python常用模块之sys
Python常用模块之sys sys模块提供了一系列有关Python运行环境的变量和函数. 常见用法 sys.argv 可以用sys.argv获取当前正在执行的命令行参数的参数列表(list). 变量 ...