最小费用流:
输入n,m,k三个数,分别代表店商数目、供应商数目和商品的种类数。
接下来是一个n*k的矩阵,第i行第j个数表示店商i需要商品j的数目。
再是一个m*k的矩阵,第i 行第j个数表示供应商i有商品j的数目。
最后是k个n*m的矩阵,第x个矩阵的第i行第j个数表示供应商j向店商i提供一个x类商品的运费。
如果能够满足所有的店商的需求的话输出最小的运费和,不然输出-1。
一眼的感觉就是最小费用最大流了。
每种商品的供应关系建立一个图,求其最小费用,不过在此之前先判断下是否每种商品的供应总量是否满足其需求总量。
建图就是加上一个超级源点和一个超级汇点。
s->供应商->店商->t。0->[1,m]->[m+1,m+n]->m+n+1。
/***************************************** Author :Crazy_AC(JamesQi) Time :2015 File Name : *****************************************/ // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <iomanip> #include <sstream> #include <string> #include <stack> #include <queue> #include <deque> #include <vector> #include <map> #include <set> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <climits> using namespace std; #define MEM(x,y) memset(x, y,sizeof x) #define pk push_back typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> ii; typedef pair<ii,int> iii; const double eps = 1e-10; const int inf = 1 << 30; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; const int maxn = 100; int n, m, k; int need[maxn][maxn]; int supply[maxn][maxn]; int Cost[maxn][maxn][maxn];//Cost[k][i][j],j->i int w[maxn*3][maxn*3]; int kneed[maxn], ksupp[maxn]; int cap[maxn*3][maxn*3], pre[maxn*3], dis[maxn*3]; bool vis[maxn*3]; void Input() { for (int i = 1;i <= n;++i) for (int kk = 1;kk <= k;++kk) scanf("%d",&need[i][kk]); for (int j = 1;j <= m;++j) for (int kk = 1;kk <= k;++kk) scanf("%d",&supply[j][kk]); for (int kk = 1;kk <= k;++kk) for (int i = 1;i <= n;++i) for (int j = 1;j <= m;++j) scanf("%d",&Cost[kk][i][j]); for (int kk = 1;kk <= k;++kk) { kneed[kk] = 0; for (int i = 1;i <= n;++i) kneed[kk] += need[i][kk]; ksupp[kk] = 0; for (int j = 1;j <= m;++j) ksupp[kk] += supply[j][kk]; } return ; } int s, t, num; void Initial(int kind) { memset(pre, 0,sizeof pre); memset(cap, 0,sizeof cap); memset(w, 0,sizeof w); for (int j = 1;j <= m;++j) cap[s][j] = supply[j][kind]; for (int i = 1;i <= n;++i) cap[i+m][t] = need[i][kind]; for (int i = 1;i <= n;++i) { for (int j = 1;j <= m;++j) { w[j][i+m] = Cost[kind][i][j]; w[i+m][j] = -w[j][i+m]; cap[j][i+m] = supply[j][kind]; } } return ; } const bool check(int kind) { return kneed[kind] <= ksupp[kind]; } bool Err; bool spfa() { memset(dis, INF,sizeof dis); memset(vis, false,sizeof vis); dis[s] = 0; vis[s] = true; queue<int> que; que.push(s); while(!que.empty()) { int u = que.front(); que.pop(); vis[u] = false; for (int v = s;v <= t;++v) { if (cap[u][v] && dis[v] > dis[u] + w[u][v]) { dis[v] = dis[u] + w[u][v]; pre[v] = u; if (!vis[v]) { vis[v] = true; que.push(v); } } } } return dis[t] < INF; } int MinCost, MinTotalCost; void Addflow(int kind) { int maxflow = INF; for (int i = t;i != s;i = pre[i]) maxflow = min(maxflow, cap[pre[i]][i]); for (int i = t;i != s;i = pre[i]) { cap[pre[i]][i] -= maxflow; cap[i][pre[i]] += maxflow; MinCost += maxflow*w[pre[i]][i]; } return ; } void Computs() { Err = false; s = 0, t = n + m + 1, num = n + m + 2; MinTotalCost = 0; for (int kind = 1;kind <= k;++kind) { Initial(kind); if (!check(kind)) { Err = true; return ; } MinCost = 0; spfa(); while(spfa()) { Addflow(kind); } MinTotalCost += MinCost; } } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); while(cin >>n>>m>>k && (n+m+k)) { Input(); Computs(); if (Err) printf("-1\n"); else printf("%d\n", MinTotalCost); } return 0; }