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 # ...
随机推荐
- 特殊集合 Stack Queue Hashtable
//Stack 干草堆集合 栈集合 先进后出 Stack st = new Stack(); //实例化 初始化 st.Push(2); //添加元素 st.Push(6); s ...
- GitHub上删除项目
1. 在自己的repositories中,选择要删除的项目,左击. 2. 选择 [Settings] 3. 选择 [Delete ** ] 4. 在弹出框中 输入 要删除的项目名,点击下方的按钮 ...
- Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds 解决方法
Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds. If the server requires ...
- bzoj3732: Network--kruskal最小生成树+LCA
这是一道写起来比较顺手的题目 没有各种奇怪的细节,基本就是Kruskal和倍增LCA的模板.. 题目大意:对于一个无向带权图,询问两点之间一条路,使得这条路上的最长边最小,输出最小最长边的的值 那么既 ...
- 2016HUAS暑假集训训练2 L - Points on Cycle
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/L 这是一道很有意思的题,就是给定一个以原点为圆心的圆,然后给定 一个点 求最大三 ...
- docker pipework
#!/bin/bash #auto install docker and Create VM #Define PATH Varablies IPADDR=`ifconfig |grep "B ...
- html只允许输入的数据校验,只允许输入字母汉字数字等
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- mysql导数据库用到的语句
将字段格式为2013-08-09 13:22:55转换为时间戳 UPDATE `AttendClass` SET `regdate` = unix_timestamp(regDate2) WHERE ...
- Ubuntu下编译第一个C程序的成功运行
1. 打开控制台:使用快捷键 Ctrl + Alt + T: 2. 安装gcc为C语言编译器,g++为C++ 语言编译器 sudo apt-get install g++. 3. 编辑好hello ...
- php课程---php使用PDO方法详解(转)
本文详细分析了php使用PDO方法.分享给大家供大家参考.具体分析如下: PDO::exec:返回的是int类型,表示影响结果的条数. 代码如下: PDOStatement::execute 返回的是 ...