F - Power Network(最大流)

A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0 <= p(u) <= p max(u) of power, may consume an amount 0 <= c(u) <= min(s(u),c max(u)) of power, and may deliver an amount d(u)=s(u)+p(u)-c(u) of power. The following restrictions apply: c(u)=0 for any power station, p(u)=0 for any consumer, and p(u)=c(u)=0 for any dispatcher. There is at most one power transport line (u,v) from a node u to a node v in the net; it transports an amount 0 <= l(u,v) <= l max(u,v) of power delivered by u to v. Let Con=Σ uc(u) be the power consumed in the net. The problem is to compute the maximum value of Con.

An example is in figure 1. The label x/y of power station u shows that p(u)=x and p max(u)=y. The label x/y of consumer u shows that c(u)=x and c max(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and l max(u,v)=y. The power consumed is Con=6. Notice that there are other possible states of the network but the value of Con cannot exceed 6.
Input
There are several data sets in the input. Each data set encodes a power network. It starts with four integers: 0 <= n <= 100 (nodes), 0 <= np <= n (power stations), 0 <= nc <= n (consumers), and 0 <= m <= n^2 (power transport lines). Follow m data triplets (u,v)z, where u and v are node identifiers (starting from 0) and 0 <= z <= 1000 is the value of l max(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of p max(u). The data set ends with nc doublets (u)z, where u is the identifier of a consumer and 0 <= z <= 10000 is the value of c max(u). All input numbers are integers. Except the (u,v)z triplets and the (u)z doublets, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.
Output
For each data set from the input, the program prints on the standard output the maximum amount of power that can be consumed in the corresponding network. Each result has an integral value and is printed from the beginning of a separate line.
Sample Input
2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7
(3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5
(0)5 (1)2 (3)2 (4)1 (5)4
Sample Output
15
6
题面有点难懂,直接看输入输出,先给出n,np,nc,m四个数字,分别代表节点数(从0开始)n,发电站数np,使用者数nc,电网数m。
接下来先给出m个电网,格式为(x,y)z,意思是节点x到y有一条输送量为z的电网,然后给出np个发电站,格式为(x)y,意思是节点x为一个发电站,电量为y,然后给出nc个使用者,格式为(x)y,意思是节点x为一个使用者,耗电量为y。
这题没给汇点,而源点有多个,可以自己设置一个源点S和汇点T,S=n+1,T=n+2,然后将源点与发电站建边,容量为发电量;使用者与汇点建边,容量为耗电量,再按照给出的电网建边,跑最大流即可。
dinic算法

#include<stdio.h>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e4+5;
const int maxm=1e5+5;
int n,m,S,T,ne,head[maxn],cur[maxn],depth[maxn],maxflow;
int np,nc;
queue<int> q;
struct edge
{
	int next,to,cost;
}e[maxm*2];
void init()
{
	memset(head,-1,sizeof(head));
	ne=0;
	maxflow=0;
}
void add(int u,int v,int w)
{
	e[ne].to=v;
	e[ne].cost=w;
	e[ne].next=head[u];
	head[u]=ne++;
	e[ne].to=u;
	e[ne].cost=0;
	e[ne].next=head[v];
	head[v]=ne++;
}
int bfs(int s,int t)
{
	while(!q.empty()) q.pop();
	memset(depth,-1,sizeof(depth));
	for(int i=0;i<=T;i++) cur[i]=head[i];
	depth[s]=0;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].next)
		{
			if(depth[e[i].to]==-1&&e[i].cost)
			{
				depth[e[i].to]=depth[u]+1;
				q.push(e[i].to);
			}
		}
	}
	if(depth[t]==-1) return -1;
	else return 1;
}
int dfs(int s,int t,int limit)
{
	if(!limit||s==t) return limit;
	int f,flow=0;
	for(int i=cur[s];i!=-1;i=e[i].next)
	{
		cur[s]=i;
		if(depth[e[i].to]==depth[s]+1&&(f=dfs(e[i].to,t,min(limit,e[i].cost))))
		{
			flow+=f;
			limit-=f;
			e[i].cost-=f;
			e[i^1].cost+=f;
			if(!limit) break;
		}
	}
	return flow;
}
void dinic(int s,int t)
{
	while(bfs(s,t)!=-1)
	{
		maxflow+=dfs(s,t,INF);
	}
}
int main()
{
	while(~scanf("%d %d %d %d",&n,&np,&nc,&m))
	{
		char A;
		init();
		S=n+1;
		T=n+2;
		for(int i=0;i<m;i++)
		{
			int x,y,z;
			cin>>A;
			cin>>x;
			cin>>A;
			cin>>y;
			cin>>A;
			cin>>z;
			add(x,y,z);
		}
		for(int i=0;i<np;i++)
		{
			int x,y;
			cin>>A;
			cin>>x;
			cin>>A;
			cin>>y;
			add(S,x,y);
		}
		for(int i=0;i<nc;i++)
		{
			int x,y;
			cin>>A;
			cin>>x;
			cin>>A;
			cin>>y;
			add(x,T,y);
		}
		dinic(S,T);
		printf("%d\n",maxflow);
	}
}

ISAP算法

#include<stdio.h>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e4+5;
const int maxm=1e5+5;
int head[maxn],cur[maxn],dep[maxn],ne,pre[maxn],num[maxn],n,m,S,T,maxflow;
int nc,np;
queue<int> q;
struct edge
{
	int next,to,cost;
}e[maxm*2];
void add(int u,int v,int w)
{
	e[ne].to=v;
	e[ne].cost=w;
	e[ne].next=head[u];
	head[u]=ne++;
	e[ne].to=u;
	e[ne].cost=0;
	e[ne].next=head[v];
	head[v]=ne++;
}
void bfs(int t)
{
	while(!q.empty()) q.pop();
	for(int i=0;i<=T;i++) cur[i]=head[i];
	for(int i=0;i<=T;i++) dep[i]=T+1;
	dep[t]=0;
	q.push(t);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].next)
		{
			if(dep[e[i].to]==T+1&&e[i^1].cost)
			{
				dep[e[i].to]=dep[u]+1;
				q.push(e[i].to);
			}
		}
	}
}
int addflow(int s,int t)
{
	int ans=INF,u=t;
	while(u!=s)
	{
		ans=min(ans,e[pre[u]].cost);
		u=e[pre[u]^1].to;
	}
	u=t;
	while(u!=s)
	{
		e[pre[u]].cost-=ans;
		e[pre[u]^1].cost+=ans; 
		u=e[pre[u]^1].to;
	}
	return ans;
}
void ISAP(int s,int t)
{
	int u=s;
	bfs(t);
	for(int i=0;i<=T;i++) num[dep[i]]++;
	while(dep[s]<T+1)
	{
		if(u==t) 
		{
			maxflow+=addflow(s,t);
			u=s;
		}
		int flag=0;
		for(int &i=cur[u];i!=-1;i=e[i].next)
		{
			if(dep[u]==dep[e[i].to]+1&&e[i].cost)
			{
				flag=1;
				pre[e[i].to]=i;
				u=e[i].to;
				break;
			}
		}
		if(!flag)
		{
			int minn=T;
			for(int i=head[u];i!=-1;i=e[i].next)
			{
				if(e[i].cost)
				{
					minn=min(minn,dep[e[i].to]);
				}
			}
			if((--num[dep[u]])==0) break;
			dep[u]=minn+1;
			num[dep[u]]++;
			cur[u]=head[u];
			if(u!=s) u=e[pre[u]^1].to;
		}
	}
}

void init()
{
	memset(head,-1,sizeof(head));
	ne=0;
	memset(num,0,sizeof(num));
	maxflow=0;
}
int main()
{
	while(~scanf("%d %d %d %d",&n,&np,&nc,&m))
	{
		char A;
		init();
		S=n+1;
		T=n+2;
		for(int i=0;i<m;i++)
		{
			int x,y,z;
			cin>>A;
			cin>>x;
			cin>>A;
			cin>>y;
			cin>>A;
			cin>>z;
			add(x,y,z);
		}
		for(int i=0;i<np;i++)
		{
			int x,y;
			cin>>A;
			cin>>x;
			cin>>A;
			cin>>y;
			add(S,x,y);
		}
		for(int i=0;i<nc;i++)
		{
			int x,y;
			cin>>A;
			cin>>x;
			cin>>A;
			cin>>y;
			add(x,T,y);
		}
		ISAP(S,T);
		printf("%d\n",maxflow);
	}
}

猜你喜欢

转载自blog.csdn.net/zufe_cst/article/details/86497914