首页 技术 正文
技术 2022年11月8日
0 收藏 984 点赞 2,022 浏览 3364 个字

链接:

https://vjudge.net/problem/POJ-2516

题意:

Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (marked from 1 to M), each provides K different kinds of goods (marked from 1 to K). Once shopkeepers order goods, Dearboy should arrange which supply place provide how much amount of goods to shopkeepers to cut down the total cost of transport.

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.

思路:

建图, 但是不能对每个商品同时建图,每个商品矩阵分别建图,同时不满足条件就不要跑费用流了..会T.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;const int MAXN = 50+10;
const int INF = 1e9;struct Edge
{
int from, to, flow, cap, cost;
Edge(int from, int to, int flow, int cap, int cost)
{
this->from = from;
this->to = to;
this->flow = flow;
this->cap = cap;
this->cost = cost;
}
};vector<Edge> edges;
vector<int> G[MAXN*MAXN*MAXN];
int Sh[MAXN][MAXN];
int Wo[MAXN][MAXN];
int SumN[MAXN];
int a[MAXN*MAXN];
int Vis[MAXN*MAXN*MAXN], Dis[MAXN*MAXN*MAXN], Pre[MAXN*MAXN*MAXN];
int n, m, k;
int s, t;void AddEdge(int from, int to, int cap, int cost)
{
edges.push_back(Edge(from, to, 0, cap, cost));
edges.push_back(Edge(to, from, 0, 0, -cost));
int len = edges.size();
G[from].push_back(len-2);
G[to].push_back(len-1);
}bool SPFA()
{
memset(Dis, MINF, sizeof(Dis));
memset(Vis, 0, sizeof(Vis));
queue<int> que;
Dis[s] = 0;
Vis[s] = 1;
que.push(s);
a[s] = INF;
while (!que.empty())
{
// for (int i = s;i <= t;i++)
// cout << Dis[i] << ' ' ;
// cout << endl;
int u = que.front();
// cout << u << endl;
que.pop();
Vis[u] = 0;
for (int i = 0;i < G[u].size();i++)
{
Edge &e = edges[G[u][i]];
if (e.cap > e.flow && Dis[e.to] > Dis[u]+e.cost)
{
Dis[e.to] = Dis[u]+e.cost;
Pre[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap-e.flow);
if (!Vis[e.to])
{
que.push(e.to);
Vis[e.to] = 1;
}
}
}
}
if (Dis[t] != MINF)
return true;
return false;
}int CostFlow(int &Flow)
{
int cost = 0;
while (SPFA())
{
// cout << 1 << endl;
// int Min = INF;
// for (int i = t;i != s;i = edges[Pre[i]].from)
// Min = min(Min, edges[Pre[i]].cap-edges[Pre[i]].flow);
// cout << Min << endl;
for (int i = t;i != s;i = edges[Pre[i]].from)
{
edges[Pre[i]].flow += a[t];
edges[Pre[i]^1].flow -= a[t];
// Edge &e = edges[Pre[i]], &ee = edges[Pre[i]^1];
// cout << e.from << ' ' << e.to << ' ' << e.flow << ' ' << e.cap << endl;
// cout << ee.from << ' ' << ee.to << ' ' << ee.flow << ' ' << ee.cap << endl;
// cout << endl;
}
cost += a[t]*Dis[t];
Flow += a[t];
}
return cost;
}void Init()
{
for (int i = 0;i <= n+m+1;i++)
G[i].clear();
edges.clear();
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
while (~scanf("%d %d %d", &n, &m, &k) && (n+m+k))
{
s = 0, t = n+m+1;
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= k;j++)
scanf("%d", &Sh[i][j]);
}
memset(SumN, 0, sizeof(SumN));
for (int i = 1;i <= m;i++)
{
for (int j = 1;j <= k;j++)
scanf("%d", &Wo[i][j]), SumN[j] += Wo[i][j];
}
int v;
//shop = n*k
//wo = n*k+m*k
int res = 0, sumflow = 0;
bool ok = true;
for (int i = 1;i <= k;i++)
{
Init();
int tmp = 0;
for (int j = 1;j <= n;j++)
{
AddEdge(s, j, Sh[j][i], 0);
tmp += Sh[j][i];
}
if (tmp > SumN[i])
ok = false;
for (int j = 1;j <= n;j++)
{
for (int z = 1;z <= m;z++)
{
scanf("%d", &v);
AddEdge(j, n+z, INF, v);
}
}
for (int j = 1;j <= m;j++)
AddEdge(n+j, t, Wo[j][i], 0);
if (ok)
res += CostFlow(sumflow);
}
if (!ok)
puts("-1");
else
printf("%d\n", res);
} return 0;
}
上一篇: curl 详解
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294