博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1459 Power Network 最大流 dinic模板
阅读量:6902 次
发布时间:2019-06-27

本文共 6912 字,大约阅读时间需要 23 分钟。

Power Network
Time Limit: 2000MS   Memory Limit: 32768K
Total Submissions: 18609   Accepted: 9833

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)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.

Source

 
 
最大流dinic模板,邻接表的形式
EK模板不够强大,今天总结了下dinic模板。
/*最大流模板dinic算法*/#include
#include
#include
#include
using namespace std;const int INF=0x3f3f3f3f;const int MAXN=150;//点数的最大值const int MAXM=20500;//边数的最大值struct Node{ int from,to,next; int cap;}edge[MAXM];int tol;int dep[MAXN];//dep为点的层次int head[MAXN];int n;void init(){ tol=0; memset(head,-1,sizeof(head));}void addedge(int u,int v,int w)//第一条变下标必须为偶数{ edge[tol].from=u; edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u]; head[u]=tol++; edge[tol].from=v; edge[tol].to=u; edge[tol].cap=0; edge[tol].next=head[v]; head[v]=tol++;}int BFS(int start,int end){ int que[MAXN]; int front,rear; front=rear=0; memset(dep,-1,sizeof(dep)); que[rear++]=start; dep[start]=0; while(front!=rear) { int u=que[front++]; if(front==MAXN)front=0; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(edge[i].cap>0&&dep[v]==-1) { dep[v]=dep[u]+1; que[rear++]=v; if(rear>=MAXN)rear=0; if(v==end)return 1; } } } return 0;}int dinic(int start,int end){ int res=0; int top; int stack[MAXN];//stack为栈,存储当前增广路 int cur[MAXN];//存储当前点的后继 while(BFS(start,end)) { memcpy(cur,head,sizeof(head)); int u=start; top=0; while(1) { if(u==end) { int min=INF; int loc; for(int i=0;i
edge[stack[i]].cap) { min=edge[stack[i]].cap; loc=i; } for(int i=0;i

 

 

另外再附上一份SAP的高效模板:

/*最大流模板sap*/#include
#include
#include
#include
using namespace std;const int MAXN=150;//点数的最大值const int MAXM=20500;//边数的最大值const int INF=0x3f3f3f3f;struct Node{ int from,to,next; int cap;}edge[MAXM];int tol;int head[MAXN];int dep[MAXN];int gap[MAXN];//gap[x]=y :说明残留网络中dep[i]==x的个数为yint n;void init(){ tol=0; memset(head,-1,sizeof(head));}void addedge(int u,int v,int w){ edge[tol].from=u; edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u]; head[u]=tol++; edge[tol].from=v; edge[tol].to=u; edge[tol].cap=0; edge[tol].next=head[v]; head[v]=tol++;}void BFS(int start,int end){ memset(dep,-1,sizeof(dep)); memset(gap,0,sizeof(gap)); gap[0]=1; int que[MAXN]; int front,rear; front=rear=0; dep[end]=0; que[rear++]=end; while(front!=rear) { int u=que[front++]; if(front==MAXN)front=0; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(edge[i].cap!=0||dep[v]!=-1)continue; que[rear++]=v; if(rear>=MAXN)rear=0; dep[v]=dep[u]+1; ++gap[dep[v]]; } }}int SAP(int start,int end){ int res=0; BFS(start,end); int cur[MAXN]; int S[MAXN]; int top=0; memcpy(cur,head,sizeof(head)); int u=start; int i; while(dep[start]
edge[S[i]].cap) { temp=edge[S[i]].cap; inser=i; } for(i=0;i
dep[edge[i].to]) { min=dep[edge[i].to]; cur[u]=i; } } --gap[dep[u]]; dep[u]=min+1; ++gap[dep[u]]; if(u!=start)u=edge[S[--top]].from; } } return res;}int main()//多源多汇点,在前面加个源点,后面加个汇点,转成单源单汇点{ // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int start,end; int np,nc,m; int u,v,z; while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF) { init(); while(m--) { while(getchar()!='('); scanf("%d,%d)%d",&u,&v,&z); u++;v++; addedge(u,v,z); } while(np--) { while(getchar()!='('); scanf("%d)%d",&u,&z); u++; addedge(0,u,z); } while(nc--) { while(getchar()!='('); scanf("%d)%d",&u,&z); u++; addedge(u,n+1,z); } start=0; end=n+1; n+=2;//n一定是点的总数,这是使用SAP模板需要注意的 int ans=SAP(start,end); printf("%d\n",ans); } return 0;}

 

转载地址:http://capdl.baihongyu.com/

你可能感兴趣的文章
MySQL
查看>>
k3cloud开发环境引入dll与net源代码
查看>>
网络安全系列之四十 在Linux中设置SET位权限
查看>>
SCCM OSD部署排错
查看>>
十道非常好的shell脚本试题
查看>>
app项目案例一手机浏览器
查看>>
linuxmint安装配置
查看>>
java 中 isEmpty和isBlank区别
查看>>
申请SSL证书怎样验证域名所有权
查看>>
麒麟开源堡垒机集中管控平台软件简介
查看>>
第十一单元练习
查看>>
从零开始的linux 第十六章
查看>>
EOS内存RAM是如何买卖的
查看>>
微服务架构中zuul的两种隔离机制实验
查看>>
电子合同将取代纸质合同吗?
查看>>
官宣丨“创客集结号”成功接入“广东省教育资源公共服务平台”!
查看>>
oracle教程之DML语句与undo
查看>>
mock.js与json schema
查看>>
转:Java properties | FileNotFoundException: properties (系统找不到指定的文件。)
查看>>
Cpp中流继承关系
查看>>