poj Power Network 1459 (多汇点多源点问题)

Power Network
Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 25547 Accepted: 13305

Description

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

Hint

The sample input contains two data sets. The first data set encodes a network with 2 nodes, power station 0 with pmax(0)=15 and consumer 1 with cmax(1)=20, and 2 power transport lines with lmax(0,1)=20 and lmax(1,0)=10. The maximum value of Con is 15. The second data set encodes the network from figure 1.
 
题目意思:给出n个节点(编号0到n-1),这些点里面有发电站(只负责发电),消耗站(只负责消耗电),转运站(只负责转运电)。现在给出每个发电站的最大发电量,消耗站的最大消耗量,转运站的最大转运量, 让你求出消耗站所能消耗的最大电量。
思路:建立超级源0节点        只连通所有发电站,发电站的最大发电量为这条边上的容量。
            建立超级汇n+1节点   只连通所有消耗站,消耗站的最大消耗量为这条边上的容量。 这样讲问题转化成求超级源到超级汇的 最大流问题。
注意:增边的时候,把编号自加一,因为超级源为0节点,而题目中给的节点是从0开始的。
 
//唉,和大神的代码差不多,大神的是94Ms,我的却1516Ms,找了一晚上,代码都改的一模一样了,还是1516Ms。。。(太无奈了)
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define N 110
#define INF 0x3f3f3f
using namespace std;
int vis[N];
int dis[N];
int head[N],edgenum;
int n,m,np,nc;
struct zz
{
	int from;
	int to;
	int cap;
	int flow;
	int next;
}edge[40010];
void add(int u,int v,int w)
{
	zz E={u,v,w,0,head[u]};
	edge[edgenum]=E;
	head[u]=edgenum++;
	zz EE={v,u,0,0,head[v]};
	edge[edgenum]=EE;
	head[v]=edgenum++;
}
void init()
{
	edgenum=0;
	memset(head,-1,sizeof(head));
}
void getmap()
{
	int u,v,w;
	while(m--)
	{
		scanf(" (%d,%d)%d",&u,&v,&w);
		add(u+1,v+1,w);
	}
	while(np--)
	{
		scanf(" (%d)%d",&v,&w);
		add(0,v+1,w);
	}
	while(nc--)
	{
		scanf(" (%d)%d",&u,&w);
		add(u+1,n+1,w);
	}
}
bool bfs(int s,int e)
{	
	memset(dis,-1,sizeof(dis));
	memset(vis,0,sizeof(dis));
	queue<int>q;
	while(!q.empty())
		q.pop();
	q.push(s);	
	dis[s]=0;
	vis[s]=1;	
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=edge[i].next)
		{
			zz v=edge[i];
			if(!vis[v.to]&&v.cap>v.flow)
			{
				dis[v.to]=dis[u]+1;
				vis[v.to]=1;
				if(v.to==e)
					return true;
				q.push(v.to);
			}
		}
	}
	return false;
}
int dfs(int x,int a,int e)
{
	if(x==e||a==0)
		return a;
	int flow=0,f;
	for(int i=head[x];i!=-1;i=edge[i].next)
	{
		zz &v=edge[i];
		if(dis[v.to]==dis[x]+1&&(f=dfs(v.to,min(a,v.cap-v.flow),e))>0)
		{
			v.flow+=f;
			edge[i^1].flow-=f;
			flow+=f;
			a-=f;
			if(a==0)
				break;
		}
	}
	return flow;
}
int maxflow(int s,int e)
{
	int flow=0;
	while(bfs(s,e))
	{
		flow+=dfs(s,INF,e);
	}
	return flow;
}
int main()
{
	while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF)
	{
		/*edgenum=0;
		memset(head,-1,sizeof(head));
		int u,v,w;
		while(m--)
		{
			scanf(" (%d,%d)%d",&u,&v,&w);
			add(u+1,v+1,w);
		}
		while(np--)
		{
			scanf(" (%d)%d",&v,&w);
			add(0,v+1,w);
		}
		while(nc--)
		{
			scanf(" (%d)%d",&u,&w);
			add(u+1,n+1,w);
		}*/
		init();
		getmap();
		printf("%d\n",maxflow(0,n+1));
	}
	return 0;
}
//唉,,,终于找到了
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 110
#define INF 1000000000+10  
#include<queue>
using namespace std;
int dis[N];
int vis[N];
int c[N];
int head[N],edgenum;
int n,m,np,nc;
struct zz
{
	int from;
	int to;
	int cap;
	int flow;
	int next;
}edge[N<<8];
void add(int u,int v,int w)
{
	zz E={u,v,w,0,head[u]};
	edge[edgenum]=E;
	head[u]=edgenum++;
	zz EE={v,u,0,0,head[v]};
	edge[edgenum]=EE;
	head[v]=edgenum++;
}
bool bfs(int s,int e)
{	
	memset(dis,-1,sizeof(dis));
	memset(vis,0,sizeof(vis));
	queue<int>q;
	while(!q.empty())
		q.pop();
	q.push(s);
	dis[s]=0;
	vis[s]=1;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=edge[i].next)
		{
			zz v=edge[i];
			if(!vis[v.to]&&v.cap>v.flow)
			{
				dis[v.to]=dis[u]+1;
				vis[v.to]=1;
				if(v.to==e)
					return true;
				q.push(v.to);
			}
		}
	}
	return false;
}
int dfs(int x,int a,int e)
{
	if(x==e||a==0)
		return a;
	int flow=0,f;
	for(int &i=c[x];i!=-1;i=edge[i].next)
	{
		zz &v=edge[i];
		if(dis[v.to]==dis[x]+1&&(f=dfs(v.to,min(a,v.cap-v.flow),e))>0)
		{
			v.flow+=f;
			edge[i^1].flow-=f;
			flow+=f;
			a-=f;
			if(a==0)
				break;
		}
	}
	return flow;
}
int maxflow(int s,int e)
{
	int flow=0;
	while(bfs(s,e))
	{
		memcpy(c,head,sizeof(head));//好坑,这块没有用c[], 时间扩大十几倍。。。还是懂的太少 
		flow+=dfs(s,INF,e);
	}
	return flow;
}
int main()
{
	while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF)
	{
		edgenum=0;
		memset(head,-1,sizeof(head));
		int u,v,w;
		while(m--)
		{
			scanf(" (%d,%d)%d",&u,&v,&w);
			add(u+1,v+1,w);
		}
		while(np--)
		{
			scanf(" (%d)%d",&v,&w);
			add(0,v+1,w);
		}
		while(nc--)
		{
			scanf(" (%d)%d",&u,&w);
			add(u+1,n+1,w);
		}
		printf("%d\n",maxflow(0,n+1));
	}
	return 0;
}


Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值