#include "cstdio"
#include "cstring"
const int N = 1111;//点
const int M = 111111;//边
const int inf = 1000000000;
int min(int a,int b){return a<b ? a:b;}
int max(int a,int b){return a>b ? a:b;}
struct Node{//边,点f到点t,流量为c,费用为w
int f, t, c, w;
}e[M];
int next1[M], point[N], dis[N], q[N], pre[N], ne;//ne为已添加的边数,next,point为邻接表,dis为花费,pre为父亲节点
bool u[N];
void init(){
memset(point, -1, sizeof(point));
ne = 0;
}
void add_edge(int f, int t, int d1, int d2, int w){//f到t的一条边,流量为d1,反向流量d2,花费w,反向边花费-w(可以反悔)
e[ne].f = f, e[ne].t = t, e[ne].c = d1, e[ne].w = w;
next1[ne] = point[f], point[f] = ne++;
e[ne].f = t, e[ne].t = f, e[ne].c = d2, e[ne].w = -w;
next1[ne] = point[t], point[t] = ne++;
}
bool spfa(int s, int t, int n){
int i, tmp, l, r;
memset(pre, -1, sizeof(pre));
for(i = 0; i < n; ++i)
dis[i] = inf;
dis[s] = 0;
q[0] = s;
l = 0, r = 1;
u[s] = true;
while(l != r) {
tmp = q[l];
l = (l + 1) % (n + 1);
u[tmp] = false;
for(i = point[tmp]; i != -1; i = next1[i]) {
if(e[i].c && dis[e[i].t] > dis[tmp] + e[i].w) {
dis[e[i].t] = dis[tmp] + e[i].w;
pre[e[i].t] = i;
if(!u[e[i].t]) {
u[e[i].t] = true;
q[r] = e[i].t;
r = (r + 1) % (n + 1);
}
}
}
}
if(pre[t] == -1)
return false;
return true;
}
void MCMF(int s, int t, int n, int &flow, int &cost){//起点s,终点t,点数n,最大流flow,最小花费cost
int tmp, arg;
flow = cost = 0;
while(spfa(s, t, n)) {
arg = inf, tmp = t;
while(tmp != s) {
arg = min(arg, e[pre[tmp]].c);
tmp = e[pre[tmp]].f;
}
tmp = t;
while(tmp != s) {
e[pre[tmp]].c -= arg;
e[pre[tmp] ^ 1].c += arg;
tmp = e[pre[tmp]].f;
}
flow += arg;
cost += arg * dis[t];
}
}
int n,m,k;
int s[111],t[111];
int C[111][111],D[111][111],E[111][111],F[111][111];
void input(){
for(int i=1;i<=n;i++) scanf("%d%d",&s[i],&t[i]);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) scanf("%d",&C[i][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) scanf("%d",&D[i][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++) scanf("%d",&E[i][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++) scanf("%d",&F[i][j]);
}
void solve(){
int tem;
init();
for(int i=m+2*n+1;i<=m+3*n;i++) add_edge(i,m+3*n+1,1,0,0);//ok
for(int i=1;i<=m;i++) add_edge(0,i,1,0,0);//ok
for(int i=m+1;i<=m+n;i++) add_edge(0,i,1,0,0);//ok
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(C[j][i]<t[j]){
tem=D[j][i]+max(0,C[j][i]-s[j])*k;
add_edge(i,j+m+n,1,0,tem);
}
}
}
//ok
for(int i=m+n+1;i<=m+2*n;i++) add_edge(i,i+n,1,0,0);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(t[i]+E[i][j]<t[j]&&i!=j){
tem=max(0,t[i]+E[i][j]-s[j])*k+F[i][j];
add_edge(m+i,m+2*n+j,1,0,tem);
}
}
}
int flow,cost;
MCMF(0,m+3*n+1,m+3*n+3,flow,cost);
if(flow==n) printf("%d\n",cost);
else printf("-1\n");
}
int main(){
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
if(n==0&&m==0&&k==0) break;
input();
solve();
}
return 0;
}
HDU 4780 Candy Factory
最新推荐文章于 2018-11-19 22:00:00 发布