网络流24题之T10 餐巾计划问题
来源:互联网 发布:zip解压缩linux 编辑:程序博客网 时间:2024/04/30 15:26
这题。。写完了错了,找了快半小时,发现是inq和q搞差了,还有丢了一个p数组。。
#include <cstdio>#include <iostream>#include <cstdlib>#include <algorithm>#include <cstring>using namespace std ;const int oo=1000000000;const int maxn=2010;typedef struct EDGE{ int front,next; int cap,flow; int cost;}E;E edge[25010];int len=-1;struct READIN { int p,f,n,m,s;}R;int n,s,t,tot;int head[maxn],next[25010];int q[maxn],inq[maxn];int dist[maxn],p[maxn],a[maxn];int S,T,allp;void E_add (int s,int t,int c,int f){ edge[++len].front=s; edge[len].next=t; edge[len].cap=c; edge[len].cost=f; next[len]=head[s]; head[s]=len; edge[++len].front=t; edge[len].next=s; edge[len].cost=-f; next[len]=head[t]; head[t]=len;}void init (){ freopen ("prog810.in","r",stdin); freopen ("prog810.out","w",stdout); scanf ("%d %d %d %d %d %d",&n,&R.p,&R.m,&R.f,&R.n,&R.s); memset (head,-1,sizeof(head)); S=0; T=2*n+1; allp=T; int i,j; for (i=1;i<=n;i++){ scanf ("%d",&j); E_add (S,i,j,0); E_add (i+n,T,j,0); E_add (S,i+n,oo,R.p); if (i+1<=n) E_add (i,i+1,oo,0); if (i+R.m<=n) E_add (i,i+n+R.m,oo,R.f); if (i+R.n<=n) E_add (i,i+n+R.n,oo,R.s); }}bool spfa (int &flow,int &cost){ int heads=0,tail=0; int now,i; for (i=0;i<=allp;i++) dist[i]=oo; memset (inq,0,sizeof(inq)); dist[S]=0; inq[S]=1;p[S]=0;a[S]=oo; q[tail++]=S; while (heads!=tail){ now=q[heads++]; if (heads==2000) heads=0; inq[now]=0; for (i=head[now];i>=0;i=next[i]){ E &e=edge[i]; if (e.cap>e.flow&&dist[e.next]>dist[now]+e.cost){ dist[e.next]=dist[now]+e.cost; p[e.next]=i; a[e.next]=min (a[now],e.cap-e.flow); if (!inq[e.next]){ q[tail++]=e.next; if (tail==2000) tail=0; inq[e.next]=1; } } } } if (dist[T]==oo) return false; flow+=a[T]; cost+=dist[T]*a[T]; now=T; while (now!=S){ edge[p[now]].flow+=a[T]; edge[p[now]^1].flow-=a[T]; now=edge[p[now]].front; } return true;}void work (){ int flow=0,cost=0; while (spfa (flow,cost)); printf ("%d\n",cost);}int main (){ init (); work (); return 0;}
0 0
- 网络流24题之T10 餐巾计划问题
- 餐巾计划问题[网络流24题之10]
- 网络流与线性规划24题 之 餐巾计划问题
- 【网络流24题】餐巾计划问题
- [网络流24题]餐巾计划问题
- 网络流24题 餐巾计划问题
- 网络流24题:餐巾计划问题
- [网络流 24 题] 餐巾计划问题
- 网络流24题 餐巾计划问题 zkw费用流
- 线性规划与网络流24题 10餐巾计划问题
- 网络流与线性规划24题10餐巾计划问题
- 线性规划与网络流24题 10餐巾计划问题
- [网络流24题 #10]餐巾计划问题
- code vs [网络流24题] 餐巾计划问题
- [线性规划与网络流24题] 餐巾计划问题
- 【学术篇】网络流24题--餐巾计划问题
- 【codevs1237】[网络流24题]餐巾计划问题
- 网络流24题10. 餐巾计划问题
- 一、一分钟认识Nginx
- Android异步下载网络图片
- 关于DontDestroyOnLoad与单例一起的使用
- wchar_t类型
- 多种风格的图片加载进度条(Fresco、UIL、Glided多种风格加载)
- 网络流24题之T10 餐巾计划问题
- Android bugreport工具分析和使用
- idea maven 程序包org.openqa.selenium不存在
- 学习领域驱动之前的准备——事件驱动
- OpenStack Ocata版本不可不知的51个新特性
- iPhone手机使用:手机上面的App Store突然变成英文(iTunes也是英文),然后把英文还原成中文的方法
- ajax跨域
- Linux 程序之环境变量
- Spring mvc Swagger2 API 集成记录