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.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, K (0 < N, M, K < 50), which are described above. The next N lines give the shopkeepers’ orders, with each line containing K integers (there integers are belong to [0, 3]), which represents the amount of goods each shopkeeper needs. The next M lines give the supply places’ storage, with each line containing K integers (there integers are also belong to [0, 3]), which represents the amount of goods stored in that supply place.
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
For each test case, if Dearboy can satisfy all the needs of all the shopkeepers, print in one line an integer, which is the minimum cost; otherwise just output “-1”.
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
应该对k个商品分别建图,不然边数太大,会TLE.
spfa版:
#include<iostream>
#include <string.h>
#include<stdio.h>
#include<vector>
#include <queue>
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;
inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
const int N = 110, M = 2e4 + 10;
namespace EK {
int s, t, head[N], ver[M], Next[M], tot = 1, pre[N], v[N];
int mxf, ans, edge[M], c[M], d[N], now[N];
inline void add(int x, int y, int z, int w) {
ver[++tot] = y, Next[tot] = head[x], edge[tot] = z, c[tot] = w, head[x] = tot;
ver[++tot] = x, Next[tot] = head[y], edge[tot] = 0, c[tot] = -w, head[y] = tot;
}
inline bool spfa() {
ms(v), memset(d, 0x3f, sizeof(d));
deque<int> q;
q.push_back(s), v[s] = true, d[s] = 0, now[s] = INF;
while (!q.empty()) {
int x = q.front();
q.pop_front(), v[x] = false;
for (register int i = head[x]; i; i = Next[i]) {
int y = ver[i];
int z = edge[i], w = c[i];
if (!z || d[y] <= d[x] + w) continue;
d[y] = d[x] + w, now[y] = min(now[x], z), pre[y] = i;
if (!v[y]) {
(q.empty() || d[y] > d[q.front()]) ? q.push_back(y) : q.push_front(y);
v[y] = true;
}
}
}
return d[t] != INF;
}
inline void upd() {
mxf += now[t], ans += d[t] * now[t];
int x = t;
while (x != s)
edge[pre[x]] -= now[t], edge[pre[x] ^ 1] += now[t], x = ver[pre[x] ^ 1];
}
inline int solve() {
while (spfa())upd();
return ans;
}
inline void init() {
tot = 1, mxf = ans = 0;
ms(head);
}
}
int n, m, k;
vi ned[55], sup[55];
int main() {
while (n = qr(), m = qr(), k = qr(), n || m || k) {
EK::s == 0, EK::t = n + m + 1;
repi(i, 1, n) {
ned[i].clear();
repi(j, 1, k)ned[i].pb(qr());
}
repi(i, 1, m) {
sup[i].clear();
repi(j, 1, k)sup[i].pb(qr());
}
int ans = 0;
repi(i, 0, k - 1) {
int ssup = 0, sned = 0;
repi(j, 1, n)sned += ned[j][i];
repi(j, 1, m)ssup += sup[j][i];
if (ssup < sned) {
ans = -1;
break;
}
}
repi(i, 0, k - 1) {
int sum = 0;
if (ans != -1) {
EK::init();
repi(j, 1, n)EK::add(j, EK::t, ned[j][i], 0), sum += ned[j][i];
repi(j, 1, m)EK::add(EK::s, n + j, sup[j][i], 0);
repi(j, 1, n)repi(l, 1, m)EK::add(l + n, j, INF, qr());
EK::solve();
EK::mxf == sum ? (ans += EK::ans) : (ans = -1);
} else
repi(j, 1, n)repi(l, 1, m)qr();
}
pi(ans);
}
return 0;
}
Dijkstra版:
#include<iostream>
#include <string.h>
#include<stdio.h>
#include<vector>
#include <queue>
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;
inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
const int N = 110, M = 2e4 + 10;
namespace EK {
int s, t, head[N], ver[M], Next[M], tot = 1, pre[N], v[N];
int mxf, ans, edge[M], c[M], d[N], now[N], h[N], n;
inline void add(int x, int y, int z, int w) {
ver[++tot] = y, Next[tot] = head[x], edge[tot] = z, c[tot] = w, head[x] = tot;
ver[++tot] = x, Next[tot] = head[y], edge[tot] = 0, c[tot] = -w, head[y] = tot;
}
inline bool dijkstra() {
ms(v), memset(d, 0x3f, sizeof(d));
priority_queue<pii > q;
q.push({0, s}), d[s] = 0, now[s] = INF;
while (!q.empty()) {
int x = q.top().se;
q.pop();
if (v[x])continue;
v[x] = true;
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i], z = edge[i], w = c[i];
if (!z || d[y] <= d[x] + w + h[x] - h[y]) continue;
d[y] = d[x] + w + h[x] - h[y], now[y] = min(now[x], z), pre[y] = i;
q.push({-d[y], y});
}
}
return d[t] != INF;
}
inline void upd() {
for (register int i = 0; i <= n; i++)h[i] += (d[i] == INF ? 0 : d[i]);
mxf += now[t], ans += h[t] * now[t];
int x = t;
while (x != s)
edge[pre[x]] -= now[t], edge[pre[x] ^ 1] += now[t], x = ver[pre[x] ^ 1];
}
inline int solve() {
while (dijkstra())upd();
return ans;
}
inline void init() {
tot = 1, mxf = ans = 0;
ms(head), ms(h);
}
}
int n, m, k;
vi ned[55], sup[55];
int main() {
while (n = qr(), m = qr(), k = qr(), n || m || k) {
EK::n = n + m + 1, EK::s == 0, EK::t = n + m + 1;
repi(i, 1, n) {
ned[i].clear();
repi(j, 1, k)ned[i].pb(qr());
}
repi(i, 1, m) {
sup[i].clear();
repi(j, 1, k)sup[i].pb(qr());
}
int ans = 0;
repi(i, 0, k - 1) {
int ssup = 0, sned = 0;
repi(j, 1, n)sned += ned[j][i];
repi(j, 1, m)ssup += sup[j][i];
if (ssup < sned) {
ans = -1;
break;
}
}
repi(i, 0, k - 1) {
int sum = 0;
if (ans != -1) {
EK::init();
repi(j, 1, n)EK::add(j, EK::t, ned[j][i], 0), sum += ned[j][i];
repi(j, 1, m)EK::add(EK::s, n + j, sup[j][i], 0);
repi(j, 1, n)repi(l, 1, m)EK::add(l + n, j, INF, qr());
EK::solve();
EK::mxf == sum ? (ans += EK::ans) : (ans = -1);
} else
repi(j, 1, n)repi(l, 1, m)qr();
}
pi(ans);
}
return 0;
}