ZOJ 2314 Reactor Cooling

前端之家收集整理的这篇文章主要介绍了ZOJ 2314 Reactor Cooling前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

无源无汇上下界网络流裸题

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <queue>
  7. #include <vector>
  8. using namespace std;
  9.  
  10. #define N 210
  11. #define INF 1000000000
  12.  
  13. int n,m,S,T,Ans;
  14. struct Edge{
  15. int to,next;
  16. int value;
  17. }edge[N*N*2];
  18. int head[N],tot=1;
  19. int dis[N],cur[N],Free[N],low[N*N];
  20. queue<int> Q;
  21.  
  22. inline int read()
  23. {
  24. int x=0,f=1;char ch=getchar();
  25. while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
  26. while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
  27. return x*f;
  28. }
  29.  
  30. void Addedge(int u,int v,int w)
  31. {
  32. tot++;edge[tot].next=head[u];edge[tot].value=w;edge[tot].to=v;head[u]=tot;
  33. tot++;edge[tot].next=head[v];edge[tot].value=0;edge[tot].to=u;head[v]=tot;
  34. }
  35.  
  36. bool BFS()
  37. {
  38. memset(dis,-1,sizeof(dis));
  39. dis[S]=0;Q.push(S);
  40. while(!Q.empty())
  41. {
  42. int now=Q.front();Q.pop();
  43. for(int i=head[now];i;i=edge[i].next)
  44. {
  45. int v=edge[i].to;
  46. if(dis[v]<0&&edge[i].value>0)
  47. {
  48. dis[v]=dis[now]+1;
  49. Q.push(v);
  50. }
  51. }
  52. }
  53. return dis[T]>0;
  54. }
  55.  
  56. int Find(int k,int low)
  57. {
  58. if(k==T)
  59. return low;
  60. int qwer;
  61. for(int i=cur[k];i;i=edge[i].next)
  62. {
  63. cur[k]=i;
  64. int v=edge[i].to;
  65. if(dis[v]==dis[k]+1&&edge[i].value>0&&(qwer=Find(v,min(low,edge[i].value))))
  66. {
  67. edge[i].value-=qwer;
  68. edge[i^1].value+=qwer;
  69. return qwer;
  70. }
  71. }
  72. return 0;
  73. }
  74.  
  75. void Dinic()
  76. {
  77. while(BFS())
  78. {
  79. for(int i=0;i<=T;i++)
  80. cur[i]=head[i];
  81. while(1)
  82. {
  83. int qwer=Find(S,INF);
  84. if(qwer==0)
  85. break;
  86. Ans+=qwer;
  87. }
  88. }
  89. }
  90.  
  91. bool Pd()
  92. {
  93. for(int i=head[S];i;i=edge[i].next)
  94. {
  95. if(edge[i].value)
  96. return false;
  97. }
  98. return true;
  99. }
  100.  
  101. int main()
  102. {
  103. int Case=read();
  104. while(Case--)
  105. {
  106. tot=1;
  107. memset(head,sizeof(head));
  108. memset(Free,sizeof(Free));
  109. n=read();m=read();
  110. S=0;T=n+1;
  111. for(int i=1;i<=m;i++)
  112. {
  113. int x=read(),y=read();low[i]=read();int z=read();
  114. Free[x]-=low[i];Free[y]+=low[i];
  115. Addedge(x,y,z-low[i]);
  116. }
  117. for(int i=1;i<=n;i++)
  118. {
  119. if(Free[i]>0)
  120. Addedge(S,i,Free[i]);
  121. else Addedge(i,-Free[i]);
  122. }
  123. Ans=0;Dinic();
  124. if(Pd())
  125. {
  126. printf("YES\n");
  127. for(int i=1;i<=m;i++)
  128. {
  129. printf("%d\n",edge[i*2+1].value+low[i]);
  130. }
  131. }
  132. else printf("NO\n");
  133. }
  134. return 0;
  135. }

猜你在找的React相关文章