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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 | #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f3f3f3f3f;
namespace cflow{ // 原始对偶算法
const int maxn=5005,maxm=50005;
int n,s,t;
inline int oth(int x){
return ((x-1)^1)+1;
}
struct edge{
int u,v; ll w,c; int nxt;
edge(){}
edge(int u_,int v_,ll w_,ll c_,int nxt_)
{ u=u_,v=v_,w=w_,c=c_,nxt=nxt_; }
}e[2*maxm];
int fst[maxn],tp=0;
void addedge(int u,int v,ll w,ll c){
e[++tp]=edge(u,v,w,c,fst[u]);
fst[u]=tp;
}
void addflow(int u,int v,ll w,ll c){
addedge(u,v,w,c);
addedge(v,u,0,-c);
}
ll dis[maxn],h[maxn],mxw[maxn];int cnt[maxn],frm[maxn],fre[maxn];
bool vis[maxn];
void spfa(){
static queue<int> q;
while(!q.empty()){ q.pop(); }
memset(dis,0x3f,sizeof(dis));
memset(cnt,0,sizeof(cnt));
memset(vis,0,sizeof(vis));
dis[s]=0;vis[s]=1;q.push(s);cnt[s]++;
while(!q.empty()){
int u=q.front();q.pop();vis[u]=0;
if(cnt[u]>n+1){
break;
}
for(int i=fst[u];i!=0;i=e[i].nxt){
int v=e[i].v;
ll c=e[i].c;
if(e[i].w==0){ continue; }
if(dis[v]>dis[u]+c){
dis[v]=dis[u]+c;
if(!vis[v]){ vis[v]=1;q.push(v);cnt[v]++; }
}
}
}
}
void clear(){
memset(cflow::fst,0,sizeof(cflow::fst));cflow::tp=0;
}
void init(int n_,int s_,int t_){
clear();
n=n_,s=s_,t=t_;
}
struct pr{
ll dis;int u;pr(){}pr(int u_,ll dis_){u=u_,dis=dis_;}
};
bool operator<(pr a,pr b){
return (a.dis!=b.dis)?(a.dis>b.dis):(a.u>b.u);
}
bool dijkstra(){
static priority_queue<pr> q;
while(!q.empty()){ q.pop(); }
memset(dis,0x3f,sizeof(dis));
dis[s]=0;mxw[s]=inf;q.push(pr(s,0));
while(!q.empty()){
int u=q.top().u;q.pop();
for(int i=fst[u];i!=0;i=e[i].nxt){
int v=e[i].v;
ll c=e[i].c+h[u]-h[v],w=e[i].w;
if(w && (dis[v]>dis[u]+c)){
dis[v]=dis[u]+c;
mxw[v]=min(mxw[u],w);
frm[v]=u;
fre[v]=i;
q.push(pr(v,dis[v]));
}
}
}
return dis[t]!=inf;
}
ll mincost,maxflow;
void MCMF(){
spfa();
memcpy(h,dis,sizeof(h));
mincost=maxflow=0;
while(dijkstra()){
ll flow=mxw[t];
maxflow+=flow;
mincost+=flow*(dis[t]-h[s]+h[t]);
for(int i=t;i!=s;i=frm[i]){
e[fre[i]].w-=flow;
e[oth(fre[i])].w+=flow;
}
for(int i=1;i<=n;i++){
h[i]+=dis[i];
}
}
}
}
int a,b,p,q;
inline int id(int i,int j){
return q*(i-1)+j+2;
}
int main(){
scanf("%d%d%d%d",&a,&b,&p,&q);p++,q++; // 注意有 (p+1)*(q+1) 个点
cflow::init(p*q+100,1,2); // 以 1 为源点,2 为汇点
for(int i=1;i<=p;i++){ // 向东路径
for(int j=1;j<q;j++){
ll t;scanf("%lld",&t);
cflow::addflow(id(i,j),id(i,j+1),1,-t); // 费用取相反数转换为最小费用最大流
cflow::addflow(id(i,j),id(i,j+1),inf,0); //注意这条边不是反向边
// 加反向边在 addflow 里
}
}
for(int j=1;j<=q;j++){ // 向北路径
for(int i=1;i<p;i++){
ll t;scanf("%lld",&t);
cflow::addflow(id(i,j),id(i+1,j),1,-t);
cflow::addflow(id(i,j),id(i+1,j),inf,0);
}
}
for(int i=1;i<=a;i++){
ll k;int x,y;
scanf("%lld%d%d",&k,&x,&y);
cflow::addflow(1,id(x+1,y+1),k,0); // 出发点
}
for(int i=1;i<=b;i++){
ll k;int x,y;
scanf("%lld%d%d",&k,&x,&y);
cflow::addflow(id(x+1,y+1),2,k,0); // 目的地
}
cflow::MCMF();
printf("%lld\n",-cflow::mincost); // 费用取了相反数
return 0;
}
|