1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
| #include <cstdio>
#include <queue>
#include <cctype>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
namespace fast_io {
//...
}using namespace fast_io;
const int MAX = 1e9;
const int MAXN = 1000,MAXM = 200000;
struct Edge{
int from,to;
int flow,cap;
int cost;
int nex;
}edge[MAXM];
int n,m,s,t,ecnt = 2;
int fir[MAXN],pree[MAXN];
ll dis[MAXN];int instack[MAXN];
queue<int> q;
void addedge(int a,int b,int c,int d){
//printf("%lld %lld %lld %lld\n",a,b,c,d);
edge[ecnt].from = a,edge[ecnt].to = b;
edge[ecnt].cap = c,edge[ecnt].flow = 0;
edge[ecnt].cost = d,edge[ecnt].nex = fir[a];
fir[a] = ecnt++;
edge[ecnt].from = b,edge[ecnt].to = a;
edge[ecnt].cap = 0,edge[ecnt].flow = 0;
edge[ecnt].cost = -d,edge[ecnt].nex = fir[b];
fir[b] = ecnt++;
}
bool spfa(){
while(!q.empty()) q.pop();
memset(dis,0x3f,sizeof(dis));
dis[s] = 0;q.push(s);
while(!q.empty()){
int nown = q.front();q.pop();
instack[nown] = 0;
for(int nowe = fir[nown];nowe;nowe = edge[nowe].nex){
Edge e = edge[nowe];
if(dis[e.to] > dis[nown] + e.cost && e.cap > e.flow){
dis[e.to] = dis[nown] + e.cost;
pree[e.to] = nowe;
if(instack[e.to] == 0){
instack[e.to] = 1;
q.push(e.to);
}
}
}
}
return dis[t] < 0x3f3f3f3f3f3f3f3f;
}
void argument(ll &sumf,ll &sumc){
int nown = t,nowe,delta = MAX;
while(nown!=s){
nowe = pree[nown];
delta = min(delta,edge[nowe].cap - edge[nowe].flow);
nown = edge[nowe].from;
}
nown = t;
while(nown!=s){
nowe = pree[nown];
edge[nowe].flow+=delta,edge[nowe^1].flow-=delta;
nown = edge[nowe].from;
}
sumf+=delta,sumc+=delta*dis[t];
}
void min_cost_flow(){
ll f = 0,c = 0;
while(spfa())
argument(f,c);
printf("%lld\n",c);
}
void init(){
read(m),read(n);
s = m+n+1,t = m+n+2;
int tmp = 0;
for(int i = 1;i<=n;i++){
read(tmp);
addedge(m+i,t,tmp,0);
}
for(int i = 1;i<=m;i++){
for(int j = 1;j<=n;j++){
read(tmp);
if(tmp)
addedge(i,m+j,MAX,0);
}
}
int b[10];
for(int i = 1;i<=m;i++){
read(tmp);
for(int j = 1;j<=tmp;j++)
read(b[j]);
b[0] = 0;b[tmp+1] = MAX;
for(int j = 1;j<=tmp+1;j++){
int w;read(w);
addedge(s,i,b[j]-b[j-1],w);
}
}
}
int main(){
init();
min_cost_flow();
return 0;
}
|