跳到正文

个人记录

P4015 运输问题

斯德哥尔摩约 6 分钟
原文
文章信息与版本

文章信息

发布
2018年8月4日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 6 分钟

版本

  1. v1当前

P4015 运输问题

最小费用最大流和最大费用最大流。

建图:

源点向每一个仓库连接一条流量为a[i]a[i],费用为00的边。

仓库ii向销售点jj连接一条流量为无穷大,费用为c[i][j]c[i][j]。

销售点jj向汇点连接一条流量为b[i]b[i],费用为00的边

跑一个最小费用最大流,轻松完成第一个任务。

第二个任务只要清空图,然后重新连接为费用是−c[i]-c[i]的边即可。

附代码:

C++
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#define MAXN 410
#define MAX 999999999
using namespace std;
int n,m,c=2,d=2,s,t,maxflow=0,mincost=0,maxcost=0;
int ahead[MAXN],bhead[MAXN],deep[MAXN],flow[MAXN],fa[MAXN],path[MAXN];
bool vis[MAXN];
struct node{
	int next,to,w,cost;
}a[MAXN*MAXN<<1],b[MAXN*MAXN<<1];
inline int read(){
	int date=0,w=1;char c=0;
	while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
	while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
	return date*w;
}
inline int relax1(int u,int v,int i,int w,int cost){
	if(path[v]>path[u]+cost){
		path[v]=path[u]+cost;
		fa[v]=u;
		deep[v]=i;
		flow[v]=min(flow[u],w);
		return 1;
	}
	return 0;
}
inline int relax2(int u,int v,int i,int w,int cost){
	if(path[v]<path[u]+cost){
		path[v]=path[u]+cost;
		fa[v]=u;
		deep[v]=i;
		flow[v]=min(flow[u],w);
		return 1;
	}
	return 0;
}
inline void add(int u,int v,int w,int cost){
	a[c].to=v;a[c].w=w;a[c].cost=cost;
	a[c].next=ahead[u];
	ahead[u]=c++;
	a[c].to=u;a[c].w=0;a[c].cost=-cost;
	a[c].next=ahead[v];
	ahead[v]=c++;
	b[d].to=v;b[d].w=w;b[d].cost=cost;
	b[d].next=bhead[u];
	bhead[u]=d++;
	b[d].to=u;b[d].w=0;b[d].cost=-cost;
	b[d].next=bhead[v];
	bhead[v]=d++;
}
bool spfa1(){
	int u,v;
	queue<int> q;
	for(int i=1;i<=t;i++){path[i]=MAX;flow[i]=0;vis[i]=false;fa[i]=-1;}
	path[s]=0;
	vis[s]=true;
	fa[s]=0;
	flow[s]=MAX;
	q.push(s);
	while(!q.empty()){
		u=q.front();
		q.pop();
		vis[u]=false;
		for(int i=ahead[u];i;i=a[i].next){
			v=a[i].to;
			if(a[i].w&&relax1(u,v,i,a[i].w,a[i].cost)&&!vis[v]){
				vis[v]=true;
				q.push(v);
			}
		}
	}
	if(path[t]==MAX)return false;
	return true;
}
bool spfa2(){
	int u,v;
	queue<int> q;
	for(int i=1;i<=t;i++){path[i]=-MAX;flow[i]=0;vis[i]=false;fa[i]=-1;}
	path[s]=0;
	vis[s]=true;
	fa[s]=0;
	flow[s]=MAX;
	q.push(s);
	while(!q.empty()){
		u=q.front();
		q.pop();
		vis[u]=false;
		for(int i=bhead[u];i;i=b[i].next){
			v=b[i].to;
			if(b[i].w&&relax2(u,v,i,b[i].w,b[i].cost)&&!vis[v]){
				vis[v]=true;
				q.push(v);
			}
		}
	}
	if(path[t]==-MAX)return false;
	return true;
}
void EK(){
	while(spfa1()){
		for(int i=t;i!=s;i=fa[i]){
			a[deep[i]].w-=flow[t];
			a[deep[i]^1].w+=flow[t];
		}
		maxflow+=flow[t];
		mincost+=flow[t]*path[t];
	}
	while(spfa2()){
		for(int i=t;i!=s;i=fa[i]){
			b[deep[i]].w-=flow[t];
			b[deep[i]^1].w+=flow[t];
		}
		maxcost+=flow[t]*path[t];
	}
}
int main(){
	int w,cost;
	m=read();n=read();
	s=0;t=2*n+2*m+1;
	for(int i=1;i<=m;i++){
		w=read();
		add(i,i+m,w,0);
		add(s,i,MAX,0);
	}
	for(int i=1;i<=n;i++){
		w=read();
		add(i+2*m,i+2*m+n,w,0);
		add(i+2*m+n,t,MAX,0);
	}
	for(int i=1;i<=m;i++)
	for(int j=1;j<=n;j++){
		cost=read();
		add(i+m,j+2*m,MAX,cost);
	}
	EK();
	printf("%d\n%d\n",mincost,maxcost);
	return 0;
}
原文