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

来源:互联网 发布:手机免费注册淘宝店铺 编辑:程序博客网 时间:2024/05/23 19:10
Power Network
Time Limit: 2000MS Memory Limit: 32768KTotal 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) <= pmax(u) of power, may consume an amount 0 <= c(u) <= min(s(u),cmax(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) <= lmax(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 pmax(u)=y. The label x/y of consumer u shows that c(u)=x and cmax(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and lmax(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 lmax(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of pmax(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 cmax(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)207 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

156

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 0x3f3f3fusing 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;}


0 0
原创粉丝点击