标签:
3 2 1 4 7 2 4 8 9 4 4 3 3 3 3 2 8 12 3 14 6 -1 1 1 1 -1 1 1 1 -1 -1 5 5 5 -1 5 5 5 -1 1 1 2 1 5 5 5 -1 -1 0 0 0
11 -1
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int INF = 0x3f3f3f3f; 4 const int maxn = 510; 5 struct arc { 6 int to,flow,cost,next; 7 arc(int x = 0,int y = 0,int z = 0,int nxt = -1) { 8 to = x; 9 flow = y; 10 cost = z; 11 next = nxt; 12 } 13 } e[maxn*maxn]; 14 int head[maxn],p[maxn],d[maxn],S,T,tot,n,m,k; 15 void add(int u,int v,int flow,int cost) { 16 e[tot] = arc(v,flow,cost,head[u]); 17 head[u] = tot++; 18 e[tot] = arc(u,0,-cost,head[v]); 19 head[v] = tot++; 20 } 21 queue<int>q; 22 bool in[maxn]; 23 bool spfa() { 24 while(!q.empty()) q.pop(); 25 memset(p,-1,sizeof p); 26 memset(d,0x3f,sizeof d); 27 d[S] = 0; 28 q.push(S); 29 while(!q.empty()) { 30 int u = q.front(); 31 q.pop(); 32 in[u] = false; 33 for(int i = head[u]; ~i; i = e[i].next) { 34 if(e[i].flow && d[e[i].to] > d[u] + e[i].cost) { 35 d[e[i].to] = d[u] + e[i].cost; 36 p[e[i].to] = i; 37 if(!in[e[i].to]) { 38 in[e[i].to] = true; 39 q.push(e[i].to); 40 } 41 } 42 } 43 } 44 return p[T] > -1; 45 } 46 void solve() { 47 int ret = 0,flow = 0; 48 while(spfa()) { 49 int minF = INF; 50 for(int i = p[T]; ~i; i = p[e[i^1].to]) 51 minF = min(minF,e[i].flow); 52 for(int i = p[T]; ~i; i = p[e[i^1].to]) { 53 e[i].flow -= minF; 54 e[i^1].flow += minF; 55 } 56 ret += minF*d[T]; 57 flow += minF; 58 } 59 printf("%d\n",flow < n?-1:ret); 60 } 61 int ss[maxn],tt[maxn],C[maxn][maxn],D[maxn][maxn],E[maxn][maxn],F[maxn][maxn]; 62 void Read() { 63 memset(head,-1,sizeof head); 64 for(int i = tot = 0; i < n; ++i) 65 scanf("%d%d",ss + i,tt + i); 66 for(int i = 0; i < n; ++i) 67 for(int j = 0; j < m; ++j) 68 scanf("%d",C[i] + j); 69 for(int i = 0; i < n; ++i) 70 for(int j = 0; j < m; ++j) 71 scanf("%d",D[i] + j); 72 for(int i = 0; i < n; ++i) 73 for(int j = 0; j < n; ++j) 74 scanf("%d",E[i] + j); 75 for(int i = 0; i < n; ++i) 76 for(int j = 0; j < n; ++j) 77 scanf("%d",F[i] + j); 78 } 79 int main() { 80 while(scanf("%d%d%d",&n,&m,&k),n||m||k) { 81 Read(); 82 S = 2*n + m; 83 T = S + 1; 84 for(int i = 0; i < n; ++i) { 85 add(S,i,1,0); 86 add(i + n,T,1,0); 87 } 88 for(int i = 0; i < m; ++i) 89 add(n + n + i,T,1,0); 90 for(int i = 0; i < n; ++i) 91 for(int j = 0; j < m; ++j) { 92 if(C[i][j] >= tt[i]) continue; 93 int cost = D[i][j]; 94 if(C[i][j] > ss[i]) cost += (C[i][j] - ss[i])*k; 95 add(i,2*n + j,1,cost); 96 } 97 for(int i = 0; i < n; ++i) 98 for(int j = 0; j < n; ++j) { 99 if(i == j) continue; 100 int ctime = tt[i] + E[i][j]; 101 if(ctime >= tt[j]) continue; 102 int cost = F[i][j]; 103 if(ctime > ss[j]) cost += k*(ctime - ss[j]); 104 add(j,i + n,1,cost); 105 } 106 solve(); 107 } 108 return 0; 109 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4852203.html