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. Codeforces Round #192 (Div. 2)

    吐槽一下,这次的CF好简单啊. 可是我为什么这么粗心这么大意这么弱.把心沉下来,想想你到底想做什么! A 题意:O(-1) 思路:O(-1) #include <iostream> #in ...

  2. C++中嵌入Lua脚本环境搭建

    第一步(环境准备工作): 工具: ●LuaForWindows_v5.1.4-46.exe傻瓜式安装. 作用:此工具可以在windows环境下编译运行Lua脚本程序.安装完成后会有两个图标:Lua和S ...

  3. div自定义的滚动条 (竖直导航条)

    <style type="text/css"> .scrollBar { width: 10px; background-color: #daa520; positio ...

  4. Next

    https://code.google.com/p/ik-analyzer/downloads/list  IK Analyzer

  5. 关于Random类产生随机数的一些问题

    package test2; import java.util.Random; /** * @author cy * * @date 2015年7月28日 上午8:47:52 * * @Descrip ...

  6. 杭电ACM 1013 Digital Root

    #include<stdio.h>#include<stdlib.h>#include<string.h>int main(){char s[100000];int ...

  7. mysql开启慢查询

    linux下: 一.在mysql中查询是否开启了慢查询mysql>SHOW VARIABLES  LIKE '%slow%'; Variable_name     Valuelog_slow_q ...

  8. PHP文件操作系统----主要的文件操作函数

    一.文件操作系统概述 1.概述: php中的文件操作系统主要是对文件和目录的操作.文件在windows系统下分为3种不同:文件.目录.未知,在linux/unix系统下分为7种不同:block.cha ...

  9. Android RecyclerView的基本使用

    Android RecyclerView 在去年的Google I/O大会上就推出来了,以前经常使用的ListView 继承的是AbsListView,而RecyclerView则直接继承 ViewG ...

  10. LinkedBlockingQueue的put,add跟offer的区别

    LinkedBlockingQueue的put,add和offer的区别 最近在学习<<Java并发编程实践>>,有很多java.util.concurrent包下的新类.Li ...