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 # ...
随机推荐
- jQuery的常用事件
1.$(document).ready() $(document).ready()是jQuery中响应JavaScript内置的onload事件并执行任务的一种典型方式.它和onload具有类似的效果 ...
- centos7 服务管理
服务脚本位置: /usr/lib/systemd/system (开机不登录就能够运行的服务) /usr/lib/systemd/user (用户登录后才能运行的服务) 服务脚本示例: [ ...
- crontab的坑
1. 命令 全路径 (eg:which mysql) 2.执行脚本 (脚本中加上#!/bin/bash) eg: /bin/bash script.sh 3. 输出信息(>>) 使用全 ...
- c++ map 的基本操作
Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数: map<stri ...
- mztree使用示例
mztree使用:http://www.myexception.cn/open-source/1014169.html jquery的treeview使用:http://www.cnblogs.com ...
- 64位虚拟机安装64位ubuntu出现问题
virtualbox 出现this kernel requires an an x86-64 cpu 错误 如题,但是主机是win8 64位,使用virtualbox安装ubuntu-12.04.3- ...
- Java面试题大全(二)
41.是否可以继承String类? String类是final类故不可以继承. 42.swtich是否能作用在byte上,是否能作用在long上,是否能作用在String上? switch(expr1 ...
- SQL Server如何添加登录名
1.电脑上如果安装有SQL Server,我们在开始里面打开SQL Server Management Studio,或者以桌面的快捷方式等打开SQL Server. 2.首先以Windows身份验证 ...
- 怎么启动或停止mysql服务
在linux下, 启动mysql用 service mysql start 停止用 service mysql stop 在windows下, 启动用 net start mysql 停止 ...
- 网站部署后Parser Error Message: Could not load type 的解决方案
asp.net 的Webproject 项目是在64bit机上开发,默认选项发布后,部署到32bit的服务器上,出现Parser Error Message: Could not load type的 ...