题意:有m个机器生产n个糖果,每个机器同一时间只能生产一种糖果,而且每种糖果必须在规定的时间范围内生产,机器从生产一种糖果转换为生产另一种糖果有一个时间转换,每种糖果生产时可能有额外开销。具体的还是自己读一遍题吧。。
思路:借鉴大牛的思路。根据题意,每一种糖要不就是某种机器第一个生产出来的,要不就是在其他糖生产之后生产出来的,而这两种花费有所不同,因此一种糖可以看做是被机器生产出来的或者被另一种糖生产出来,将糖拆点,一种代表生产糖的糖,一种代表要被生产的。具体的代码中有体现。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define REP( i, a, b ) for( int i = a; i < b; i++ )
#define FOR( i, a, b ) for( int i = a; i <= b; i++ )
#define CLR( a, x ) memset( a, x, sizeof a )
#define CPY( a, x ) memcpy( a, x, sizeof a )
const int maxn = 300 + 10;
const int maxe = 50000;
const int INF = 0x3f3f3f3f;
struct Edge{
int v, c, f, w;
int next;
Edge() {}
Edge(int v, int c, int f, int w, int next) : v(v), c(c), f(f), w(w), next(next) {}
};
struct MCMF{
int n, s, t;
int d[maxn], cur[maxn], f[maxn];
int Head[maxn], cntE;
int Q[maxn], head, tail, inq[maxn];
int flow;
long long cost;
Edge edge[maxe];
void Init(int n){
this -> n = n;
cntE = 0;
CLR(Head, -1);
}
void Add(int u, int v, int c, int w){
edge[cntE] = Edge(v, c, 0, w, Head[u]);
Head[u] = cntE++;
edge[cntE] = Edge(u, 0, 0, -w, Head[v]);
Head[v] = cntE++;
}
int Spfa(){
head = tail = 0;
CLR(d, INF);
CLR(inq, 0);
Q[tail++] = s;
f[s] = INF;
cur[s] = -1;
d[s] = 0;
while(head != tail){
int u = Q[head++];
if(head == maxn) head = 0;
inq[u] = 0;
for(int i = Head[u]; ~i; i = edge[i].next){
int v = edge[i].v;
if(edge[i].c > edge[i].f && d[v] > d[u] + edge[i].w){
f[v] = min(f[u], edge[i].c - edge[i].f);
d[v] = d[u] + edge[i].w;
cur[v] = i;
if(!inq[v]){
if(d[v] < d[Q[head]]){
if(head == 0) head = maxn;
Q[--head] = v;
}
else{
Q[tail++] = v;
if(tail == maxn) tail = 0;
}
inq[v] = 1;
}
}
}
}
if(d[t] == INF) return 0;
flow += f[t];
cost += (long long)f[t] * (long long)d[t];
for(int i = cur[t]; ~i; i = cur[edge[i^1].v]){
edge[i].f += f[t];
edge[i^1].f -= f[t];
}
return 1;
}
long long Mcmf(int s, int t){
this -> s = s;
this -> t = t;
flow = 0;
cost = 0;
while(Spfa());
return cost;
}
}solver;
int n, m, K;
int s[maxn], t[maxn];
int c[maxn][maxn], d[maxn][maxn];
int e[maxn][maxn], f[maxn][maxn];
void solve(){
FOR(i, 1, n) scanf("%d%d", &s[i], &t[i]);
FOR(i, 1, n) FOR(j, 1, m) scanf("%d", &c[i][j]);
FOR(i, 1, n) FOR(j, 1, m) scanf("%d", &d[i][j]);
FOR(i, 1, n) FOR(j, 1, n) scanf("%d", &e[i][j]);
FOR(i, 1, n) FOR(j, 1, n) scanf("%d", &f[i][j]);
int S = 0, T = 2 * n + m + 1;
solver.Init(T + 1);
FOR(i, 1, n) solver.Add(S, i, 1, 0);
FOR(i, 1, m) solver.Add(S, i+2*n, 1, 0);
FOR(i, 1, n) solver.Add(i + n, T, 1, 0);
FOR(i, 1, n) FOR(j, 1, n) if(i != j){
if(t[i] + e[i][j] < t[j]){
int C = max(0, t[i] + e[i][j] - s[j]) * K;
solver.Add(i, j+n, 1, C + f[i][j]);
}
}
FOR(i, 1, m) FOR(j, 1, n){
if(c[j][i] < t[j]){
int C = max(0, c[j][i] - s[j]) * K;
solver.Add(i+2*n, j+n, 1, C + d[j][i]);
}
}
long long ans = solver.Mcmf(S, T);
if(solver.flow == n)
printf("%I64d\n", ans);
else
printf("-1\n");
}
int main()
{
while(scanf("%d%d%d", &n, &m, &K) && n) solve();
return 0;
}