POJ2516 Minimum Cost —— 最小费用最大流
题目链接:https://vjudge.net/problem/POJ-2516
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 17650 | Accepted: 6205 |
Description
It's known that the cost to transport one unit goods for different kinds from different supply places to different shopkeepers may be different. Given each supply places' storage of K kinds of goods, N shopkeepers' order of K kinds of goods and the cost to transport goods for different kinds from different supply places to different shopkeepers, you should tell how to arrange the goods supply to minimize the total cost of transport.
Input
Then come K integer matrices (each with the size N * M), the integer (this integer is belong to (0, 100)) at the i-th row, j-th column in the k-th matrix represents the cost to transport one unit of k-th goods from the j-th supply place to the i-th shopkeeper.
The input is terminated with three "0"s. This test case should not be processed.
Output
Sample Input
1 3 3
1 1 1
0 1 1
1 2 2
1 0 1
1 2 3
1 1 1
2 1 1 1 1 1
3
2
20 0 0 0
Sample Output
4
-1
Source
题意:
有n个商店,m个仓库,k中商品。每个商店对每种商品都有特定需求量,且每个仓库中,每种商品都有其特定的存量。且已知对于某一种商品G,从仓库A运送一件商品G到商店B的运费。问:能否满足所有商店的供货需求?如果能满足,求出最小总运费?
题解:
最小费用最大流问题。可知每一种商品是相互独立的,因此我们可以单独求出每种商品总的最小运费,然后加起来,当然前提条件是能满足需求。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e4+;
const int MAXN = 1e2+; struct Edge
{
int to, next, cap, flow, cost;
}edge[MAXM<<];
int tot, head[MAXN];
int pre[MAXN], dis[MAXN];
bool vis[MAXN];
int N; void init(int n)
{
N = n;
tot = ;
memset(head, -, sizeof(head));
} void add(int u, int v, int cap, int cost)
{
edge[tot].to = v; edge[tot].cap = cap; edge[tot].cost = cost;
edge[tot].flow = ; edge[tot].next = head[u]; head[u] = tot++;
edge[tot].to = u; edge[tot].cap = ; edge[tot].cost = -cost;
edge[tot].flow = ; edge[tot].next = head[v]; head[v] = tot++;
} bool spfa(int s, int t)
{
queue<int>q;
for(int i = ; i<N; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -;
} dis[s] = ;
vis[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap>edge[i].flow && dis[v]>dis[u]+edge[i].cost)
{
dis[v] = dis[u]+edge[i].cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t]==-) return false;
return true;
} int minCostMaxFlow(int s, int t, int &cost)
{
int flow = ;
cost = ;
while(spfa(s,t))
{
int Min = INF;
for(int i = pre[t]; i!=-; i = pre[edge[i^].to])
{
if(Min>edge[i].cap-edge[i].flow)
Min = edge[i].cap-edge[i].flow;
}
for(int i = pre[t]; i!=-; i = pre[edge[i^].to])
{
edge[i].flow += Min;
edge[i^].flow -= Min;
cost += edge[i].cost*Min;
}
flow += Min;
}
return flow;
} int need[][], storage[][], fee[][][];
int main()
{
int n, m, k;
while(scanf("%d%d%d",&n,&m,&k)&&(n||m||k))
{
for(int i = ; i<=n; i++)
for(int j = ; j<=k; j++)
scanf("%d", &need[i][j]); for(int i = ; i<=m; i++)
for(int j = ; j<=k; j++)
scanf("%d", &storage[i][j]); for(int i = ; i<=k; i++)
for(int j = ; j<=n; j++)
for(int t = ; t<=m; t++)
scanf("%d", &fee[i][j][t]); int cost = ;
for(int item = ; item<=k; item++)
{
int whole = ;
for(int i = ; i<=n; i++)
whole += need[i][item]; int start = , end = n+m+;
init(n+m+);
for(int i = ; i<=m; i++)
{
add(start, i, storage[i][item], );
for(int j = ; j<=n; j++)
// if(storage[i][item]>=need[j][item]) //不能加此条判断
add(i, m+j, storage[i][item], fee[item][j][i]);
} for(int i = ; i<=n; i++)
add(m+i, end, need[i][item], ); int cost_item;
int offered = minCostMaxFlow(start, end, cost_item);
if(offered<whole)
{
cost = -;
break;
}
else cost += cost_item;
}
printf("%d\n", cost);
}
}
最新文章
- Linux实战教学笔记06:Linux系统基础优化
- CSS3 Media Queries 实现响应式设计
- 完全卸载mysql步骤
- 在Linux中创建静态库和动态库
- @IBDesignable和@IBInspectable
- Poj3468-A Simple Problem with Integers(伸展树练练手)
- java集合--Queue用法
- 基于visual Studio2013解决C语言竞赛题之1013字符串查找
- laravel-1 安装.配置
- Linux系统 awk sed R脚本 python脚本传入变量
- angularJs $mdDialog和$uibModal弹框关闭传值
- BZOJ_3048_[Usaco2013 Jan]Cow Lineup _双指针
- python语法_文件操作
- mysql中的多表查询
- java Scanner和Random的Demo
- 针对Jigsaw勒索软件的解锁工具
- window下配置Apache2服务器
- TMS320VC5509驱动TLV32AIC23
- 让 Node.js 支持 ES6 的语法
- js调绝对定位的top