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(最小费用流)的更多相关文章

  1. POJ 2516 Minimum Cost 最小费用流 难度:1

    Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 13511   Accepted: 4628 Des ...

  2. 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个 ...

  3. POJ 2516 Minimum Cost (网络流,最小费用流)

    POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...

  4. Poj 2516 Minimum Cost (最小花费最大流)

    题目链接: Poj  2516  Minimum Cost 题目描述: 有n个商店,m个仓储,每个商店和仓库都有k种货物.嘛!现在n个商店要开始向m个仓库发出订单了,订单信息为当前商店对每种货物的需求 ...

  5. POJ 2516 Minimum Cost (最小费用最大流)

    POJ 2516 Minimum Cost 链接:http://poj.org/problem?id=2516 题意:有M个仓库.N个商人.K种物品.先输入N,M.K.然后输入N行K个数,每一行代表一 ...

  6. POJ 2516 Minimum Cost(最小费用流)

    Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...

  7. POJ 2516 Minimum Cost (费用流)

    题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...

  8. POJ - 2516 Minimum Cost 每次要跑K次费用流

    传送门:poj.org/problem?id=2516 题意: 有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的.问为了满足不同买家的订单的最小的花费. 思路: 设立一个源 ...

  9. POJ 2516 Minimum Cost(拆点+KM完备匹配)

    题目链接:http://poj.org/problem?id=2516 题目大意: 第一行是N,M,K 接下来N行:第i行有K个数字表示第i个卖场对K种商品的需求情况 接下来M行:第j行有K个数字表示 ...

  10. POJ 2516 Minimum Cost [最小费用最大流]

    题意略: 思路: 这题比较坑的地方是把每种货物单独建图分开算就ok了. #include<stdio.h> #include<queue> #define MAXN 500 # ...

随机推荐

  1. HTML 几种特别分割线特效

    一.基本线条 二.特效(效果并不是孤立的,可相互组合)1.两头渐变透明:<HR style="FILTER: alpha(opacity=100,finishopacity=0,sty ...

  2. [转]nls_lang设置

    1. NLS_LANG 参数组成NLS_LANG参数由以下部分组成:NLS_LANG=<Language>_<Territory>.<Clients Characters ...

  3. windows安装django

    Window 下安装 Django 如果你还未安装Python环境需要先下载Python安装包. 1.Python 下载地址:https://www.python.org/downloads/ 2.D ...

  4. 【转】jquery iframe取得元素与自适应高度

    今天没事来总结一下iframe在jquery中怎么操作的,下面我来给各位介绍jquery 获取iframe子/父页面的元素及iframe在jquery高度自适应实现方法,各位朋友可参考. jquery ...

  5. angular+ckeditor最后上传的最后一张图片不会被添加(bug)

    做法一: angularJs+ckeditor 一.页面 <textarea ckeditor required name="topicContent" ng-model=& ...

  6. cacti监控apache和nginx的配置

    一.监控apache1.下载http://forums.cacti.net/about25227.html&highlight=apachestats2.其中的ss_apache_stats. ...

  7. mybatis 分页

    http://www.cnblogs.com/xiaoxinwt/p/5329840.html

  8. 详解依赖注入(DI)和Ioc容器

    简单的来说,关键技术就是:注册器模式. 场景需求 我们知道写一个类的时候,类本身是有个目的的,类里面有很多方法,每个方法搞定一些事情:我们叫这个类为主类. 另外这个主类会依赖一些其他类的帮忙,我们叫这 ...

  9. MySQL 表与字段编码格式报错

    MySQL 表与字段编码格式报错 一.数据库,表,字段编码格式都为latin1(iso-8859-1) .当数据保存到数据库后,中文显示乱码. 解决办法: 1.在访问数据库连接串中添加编码格式: &l ...

  10. Maven-009-Nexus 用户密码加密(安全必须)

    信息数据大爆发的时代,我们关心什么?没错,数据安全!数据安全!数据安全!(重要事情说三遍,哈哈哈...) 之前我们存放在 maven settings.xml 文件中的 Nexus 私服用户密码都是明 ...