[CODEVS1914]运输问题(费用流)

题目:

我是超链接

题解:

创造源点,向每个仓库连容量a[i],费用为0,;创造汇点,向每个商店连容量b[i],费用为0;二分图中间再连边

重新建图,因为上一个图remind啊什么数组早就面目全非

要特别注意不管是最大费用流还是最小费用流,都是建立在最大流的基础上,这是毋庸置疑的,那么如果我们要最大费用,就只需要把距离找到最大距离就好了

代码:

#include 
#include 
#include 
#include 
#define INF 1e9  
#define N 1000  
using namespace std;  
int nxt[N*4+5],point[N*4+5],v[N*4+5],c[N*4+5],tot=-1,remind[N*4+5],last[N*4+5],a[N*4+5],b[N*4+5],dis[N*4+5],mincost=0,maxcost=0,w[N*4][N*4];  
bool vis[N*4+5];  
void clear()
{
	tot=-1;
	memset(point,-1,sizeof(point));
	memset(nxt,-1,sizeof(nxt));
}
void addline(int x,int y,int cap,int cc)
{
	++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remind[tot]=cap; c[tot]=cc;
	++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; remind[tot]=0; c[tot]=-cc;
}
int addflow(int s,int t)
{
	int now=t,ans=INF;
	while (now!=s)
	{
		ans=min(ans,remind[last[now]]);
		now=v[last[now]^1];
	}
	now=t;
	while (now!=s)
	{
		remind[last[now]]-=ans;
		remind[last[now]^1]+=ans;
		now=v[last[now]^1];
	}
	return ans;
}
bool minbfs(int s,int t)
{
	queueq;
	memset(dis,0x7f,sizeof(dis));
	memset(vis,0,sizeof(vis));
	q.push(s);
	dis[s]=0;
	while (!q.empty())
	{
		int x=q.front(); q.pop(); vis[x]=0;
		for (int i=point[x];i!=-1;i=nxt[i])
		  if (dis[v[i]]>dis[x]+c[i] && remind[i])
		  {
		  	last[v[i]]=i;
		  	dis[v[i]]=dis[x]+c[i];
		  	if (!vis[v[i]]) vis[v[i]]=1,q.push(v[i]);
		  }
	}
	if (dis[t]>INF) return 0;
	int flow=addflow(s,t);
	mincost+=flow*dis[t];
	return 1;
}
bool maxbfs(int s,int t)
{
	queueq;
	memset(dis,128,sizeof(dis));
	memset(vis,0,sizeof(vis));
	q.push(s);
	dis[s]=0;
	while (!q.empty())
	{
		int x=q.front(); q.pop(); vis[x]=0;
		for (int i=point[x];i!=-1;i=nxt[i])
		  if (dis[v[i]]


你可能感兴趣的:(网络流)