Meaning of the questions:
There \ (m \) warehouse and \ (n-\) retailers, the \ (I \) warehouse to the section \ (J \) retailers takes \ (v [i] [j ] \) yuan. Now we need to supply the same amount of harvest and warehouse retailers, asking minimum cost and maximum cost.
analysis:
Quite the classic model of minimum cost maximum flow. Because you want to ensure the same supply and harvest, which represents the flow balance, so we can make a super source \ (sp \) with even a flow rate corresponding to the warehouse \ (a_i \) , cost \ (0 \) side, while allowing the corresponding retailers with super Meeting point \ (ep \) even a flow \ (b_i \) , cost \ (0 \) side. For warehouses and retailers, not even a traffic between warehouses and retailers we only need an infinite cost of \ (v [i] [j ] \) side.
For minimum cost, we only need to run a minimum cost maximum flow in the above figure.
As for the issue of a maximum cost, we only need to change the above graph warehouses and retailers even a flow of infinite, cost \ (- v [i] [ j] \) side, and finally in the new map while running on a minimum cost maximum flow, while the final cost of the minimum number that is the opposite answer.
Code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
const int maxm = 20005;
int head[maxn],cnt=0;
int dis[maxn],vis[maxn],sp,ep,maxflow,cost;
const int INF=0x3f3f3f3f;
struct Node{
int to,next,val,cost;
}q[maxm<<1];
void init(){
memset(head,-1,sizeof(head));
cnt=2;
maxflow=cost=0;
}
void addedge(int from,int to,int val,int cost){
q[cnt].to=to;
q[cnt].next=head[from];
q[cnt].val=val;
q[cnt].cost=cost;
head[from]=cnt++;
}
void add_edge(int from,int to,int val,int cost){
addedge(from,to,val,cost);
addedge(to,from,0,-cost);
}
bool spfa(){
memset(vis,0,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
dis[sp]=0;
vis[sp]=1;
queue<int>que;
que.push(sp);
while(!que.empty()) {
int x = que.front();
que.pop();
vis[x]=0;
for(int i=head[x];i!=-1;i=q[i].next){
int to=q[i].to;
if(dis[to]>dis[x]+q[i].cost&&q[i].val){
dis[to]=dis[x]+q[i].cost;
if(!vis[to]){
que.push(to);
vis[to]=1;
}
}
}
}
return dis[ep]!=0x3f3f3f3f;
}
int dfs(int x,int flow){
if(x==ep){
vis[ep]=1;
maxflow+=flow;
return flow;
}
int used=0;
vis[x]=1;
for(int i=head[x];i!=-1;i=q[i].next){
int to=q[i].to;
if((vis[to]==0||to==ep)&&q[i].val!=0&&dis[to]==dis[x]+q[i].cost){
int minflow=dfs(to,min(flow-used,q[i].val));
if(minflow!=0){
cost+=q[i].cost*minflow;
q[i].val-=minflow;
q[i^1].val+=minflow;
used+=minflow;
}
if(used==flow) break;
}
}
return used;
}
int mincostmaxflow(){
while(spfa()){
vis[ep]=1;
while(vis[ep]){
memset(vis,0,sizeof(vis));
dfs(sp,INF);
}
}
return maxflow;
}
int a[maxn],b[maxn],v[maxn][maxn];
int main()
{
int n,m;
init();
scanf("%d%d",&n,&m);
sp=n+m+1,ep=n+m+2;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
add_edge(sp,i,a[i],0);
}
for(int i=1;i<=m;i++){
scanf("%d",&b[i]);
add_edge(i+n,ep,b[i],0);
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&v[i][j]);
add_edge(i,j+n,INF,v[i][j]);
}
}
mincostmaxflow();
printf("%d\n",cost);
init();
for(int i=1;i<=n;i++){
add_edge(sp,i,a[i],0);
}
for(int i=1;i<=m;i++){
add_edge(i+n,ep,b[i],0);
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
add_edge(i,j+n,INF,-v[i][j]);
}
}
mincostmaxflow();
printf("%d\n",-cost);
return 0;
}