和HDOJ4888是一样的问题,最大流推断多解

1.把ISAP卡的根本出不来结果,仅仅能把全为0或者全为满流的给特判掉......

2.在残量网络中找大于2的圈要用一种类似tarjian的方法从汇点開始找,推断哪些点没有到汇点

A simple Gaussian elimination problem.

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1170    Accepted Submission(s): 377

Problem Description
Dragon is studying math. One day, he drew a table with several rows and columns, randomly wrote numbers on each elements of the table. Then he counted the sum of each row and column. Since he thought the map will be useless after he got the sums, he destroyed
the table after that.



However Dragon's mom came back and found what he had done. She would give dragon a feast if Dragon could reconstruct the table, otherwise keep Dragon hungry. Dragon is so young and so simple so that the original numbers in the table are one-digit number (e.g.
0-9).



Could you help Dragon to do that?
 
Input
The first line of input contains only one integer, T(<=30), the number of test cases. Following T blocks, each block describes one test case.



There are three lines for each block. The first line contains two integers N(<=500) and M(<=500), showing the number of rows and columns.



The second line contains N integer show the sum of each row.



The third line contains M integer show the sum of each column.
 
Output
Each output should occupy one line. Each line should start with "Case #i: ", with i implying the case number. For each case, if we cannot get the original table, just output: "So naive!", else if we can reconstruct the table by more than one ways, you should
output one line contains only: "So young!", otherwise (only one way to reconstruct the table) you should output: "So simple!".
 
Sample Input
  1. 3
  2. 1 1
  3. 5
  4. 5
  5. 2 2
  6. 0 10
  7. 0 10
  8. 2 2
  9. 2 2
  10. 2 2
 
Sample Output
  1. Case #1: So simple!
  2. Case #2: So naive!
  3. Case #3: So young!
 
Author
BJTU
 

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5.  
  6. #pragma comment(linker, "/STACK:1024000000,1024000000")
  7.  
  8. using namespace std;
  9.  
  10. const int maxn=20000;
  11. const int maxm=500000;
  12. const int INF=0x3f3f3f3f;
  13.  
  14. struct Edge
  15. {
  16. int to,next,cap,flow;
  17. }edge[maxm];
  18.  
  19. int Size,Adj[maxn];
  20. int gap[maxn],dep[maxn],pre[maxn],cur[maxn];
  21.  
  22. void init()
  23. {
  24. Size=0; memset(Adj,-1,sizeof(Adj));
  25. }
  26.  
  27. void addedge(int u,int v,int w,int rw=0)
  28. {
  29. edge[Size].to=v; edge[Size].cap=w; edge[Size].next=Adj[u];
  30. edge[Size].flow=0; Adj[u]=Size++;
  31. edge[Size].to=u; edge[Size].cap=rw; edge[Size].next=Adj[v];
  32. edge[Size].flow=0; Adj[v]=Size++;
  33. }
  34.  
  35. int sap(int start,int end,int N)
  36. {
  37. memset(gap,0,sizeof(gap));
  38. memset(dep,0,sizeof(dep));
  39. memcpy(cur,Adj,sizeof(Adj));
  40.  
  41. int u=start;
  42. pre[u]=-1; gap[0]=N;
  43. int ans=0;
  44.  
  45. while(dep[start]<N)
  46. {
  47. if(u==end)
  48. {
  49. int Min=INF;
  50. for(int i=pre[u];~i;i=pre[edge[i^1].to])
  51. if(Min>edge[i].cap-edge[i].flow)
  52. Min=edge[i].cap-edge[i].flow;
  53. for(int i=pre[u];~i;i=pre[edge[i^1].to])
  54. {
  55. edge[i].flow+=Min;
  56. edge[i^1].flow-=Min;
  57. }
  58. u=start;
  59. ans+=Min;
  60. continue;
  61. }
  62. bool flag=false;
  63. int v;
  64. for(int i=cur[u];~i;i=edge[i].next)
  65. {
  66. v=edge[i].to;
  67. if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
  68. {
  69. flag=true;
  70. cur[u]=pre[v]=i;
  71. break;
  72. }
  73. }
  74. if(flag)
  75. {
  76. u=v;
  77. continue;
  78. }
  79. int Min=N;
  80. for(int i=Adj[u];~i;i=edge[i].next)
  81. if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
  82. {
  83. Min=dep[edge[i].to];
  84. cur[u]=i;
  85. }
  86. gap[dep[u]]--;
  87. if(!gap[dep[u]]) return ans;
  88. dep[u]=Min+1;
  89. gap[dep[u]]++;
  90. if(u!=start) u=edge[pre[u]^1].to;
  91. }
  92. return ans;
  93. }
  94. int n,m;
  95. int a[maxn],b[maxn];
  96.  
  97. bool vis[maxn],no[maxn];
  98. int Stack[maxm],stk;
  99.  
  100. bool dfs(int u,int pre,bool flag)
  101. {
  102. vis[u]=true;
  103. Stack[stk++]=u;
  104. for(int i=Adj[u];~i;i=edge[i].next)
  105. {
  106. int v=edge[i].to;
  107. if(v==pre) continue;
  108. if(edge[i].flow>=edge[i].cap) continue;
  109. if(!vis[v])
  110. {
  111. if(dfs(v,u,edge[i^1].cap>edge[i^1].flow)) return true;
  112. }
  113. else if(!no[v]) return true;
  114. }
  115. if(flag==false)
  116. {
  117. while(true)
  118. {
  119. int v=Stack[--stk];
  120. no[v]=true;
  121. if(v==u) break;
  122. }
  123. }
  124. return false;
  125. }
  126.  
  127. int main()
  128. {
  129. int T_T,cas=1;
  130. scanf("%d",&T_T);
  131. while(T_T--)
  132. {
  133. scanf("%d%d",&n,&m);
  134. int sum1=0,sum2=0;
  135. for(int i=1;i<=n;i++)
  136. {
  137. scanf("%d",a+i); sum1+=a[i];
  138. }
  139. for(int i=1;i<=m;i++)
  140. {
  141. scanf("%d",b+i); sum2+=b[i];
  142. }
  143. if(sum1!=sum2)
  144. {
  145. printf("Case #%d: So naive!\n",cas++);
  146. continue;
  147. }
  148. if(sum1==sum2&&((sum1==0)||(sum1==n*m*9)))
  149. {
  150. printf("Case #%d: So simple!\n",cas++);
  151. continue;
  152. }
  153.  
  154. /*************build graph*****************/
  155. init();
  156. for(int i=1;i<=n;i++) addedge(0,i,a[i]);
  157. for(int i=1;i<=n;i++)
  158. for(int j=1;j<=m;j++)
  159. addedge(i,n+j,9);
  160. for(int i=1;i<=m;i++) addedge(i+n,n+m+1,b[i]);
  161. /*************build graph*****************/
  162. int MaxFlow=sap(0,n+m+1,n+m+2);
  163.  
  164. if(MaxFlow!=sum1)
  165. {
  166. printf("Case #%d: So naive!\n",cas++);
  167. continue;
  168. }
  169. stk=0;
  170. memset(vis,0,sizeof(vis));
  171. memset(no,0,sizeof(no));
  172. if(dfs(n+m+1,n+m+1,0))
  173. {
  174. printf("Case #%d: So young!\n",cas++);
  175. }
  176. else
  177. {
  178. printf("Case #%d: So simple!\n",cas++);
  179. }
  180. }
  181. return 0;
  182. }

HDOJ 4975 A simple Gaussian elimination problem.的更多相关文章

  1. HDU 4975 A simple Gaussian elimination problem.

    A simple Gaussian elimination problem. Time Limit: 1000ms Memory Limit: 65536KB This problem will be ...

  2. hdu 4975 A simple Gaussian elimination problem.(网络流,推断矩阵是否存在)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One ...

  3. hdu - 4975 - A simple Gaussian elimination problem.(最大流量)

    意甲冠军:要在N好M行和列以及列的数字矩阵和,每个元件的尺寸不超过9,询问是否有这样的矩阵,是独一无二的N(1 ≤ N ≤ 500) , M(1 ≤ M ≤ 500). 主题链接:http://acm ...

  4. hdu 4975 A simple Gaussian elimination problem 最大流+找环

    原题链接 http://acm.hdu.edu.cn/showproblem.php?pid=4975 这是一道很裸的最大流,将每个点(i,j)看作是从Ri向Cj的一条容量为9的边,从源点除法连接每个 ...

  5. hdu4975 A simple Gaussian elimination problem.(正确解法 最大流+删边判环)(Updated 2014-10-16)

    这题标程是错的,网上很多题解也是错的. http://acm.hdu.edu.cn/showproblem.php?pid=4975 2014 Multi-University Training Co ...

  6. A simple Gaussian elimination problem.(hdu4975)网络流+最大流

    A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...

  7. A simple Gaussian elimination problem.

    hdu4975:http://acm.hdu.edu.cn/showproblem.php?pid=4975 题意:给你一个n*m的矩阵,矩阵中的元素都是0--9,现在给你这个矩阵的每一行和每一列的和 ...

  8. hdu4975 A simple Gaussian elimination problem.(最大流+判环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 题意:和hdu4888基本一样( http://www.cnblogs.com/a-clown/ ...

  9. BNU 4356 ——A Simple But Difficult Problem——————【快速幂、模运算】

    A Simple But Difficult Problem Time Limit: 5000ms Memory Limit: 65536KB 64-bit integer IO format: %l ...

随机推荐

  1. NHibernate3剖析:Query篇之NHibernate.Linq增强查询

    系列引入 NHibernate3.0剖析系列分别从Configuration篇.Mapping篇.Query篇.Session策略篇.应用篇等方面全面揭示NHibernate3.0新特性和应用及其各种 ...

  2. php在数字前面补0得到固定长度数字的两种方法

    比較基础,事实上两个内置函数都能实现. 1  sprintf 语法: string sprintf(string format, mixed [args]...); 返回值: 字符串 函数种类: 资料 ...

  3. Qt Quick 简单介绍

    Qt Quick 是 Qt 提供的一种高级用户界面技术.使用它可轻松地为移动和嵌入式设备创建流畅的用户界面. 在 Android 设备上, Qt Quick 应用默认使用 OpenGL ES ,渲染效 ...

  4. Android学习路线(十三)Activity生命周期——暂停和恢复(Pausing and Resuming )一个Activity

    在正常使用应用的过程中.前台的activity在一些时候会被其它的组件遮挡,导致这个activity暂停.举个样例.当一个半透明的activity被打开(比如一个dialog样式的activity), ...

  5. uva_644暴力加字典树解法

    暴力 #include<iostream> #include<string.h> #include<cstdio> using namespace std; int ...

  6. JqGrid 查询时未设置初始页码导致的问题

    本文所述问题发生在查询的数据有至少2页数据时的情况下.本例中的产品质量查询就是这样. 第一步:查询该时间段内的数据,结果为13页的数据内容,显示当前页第1页.如下图所示: 第二步:点击翻页按钮,打开第 ...

  7. exsi中的虚拟机添加磁盘后虚拟机中磁盘不出现

    exsi中的虚拟机添加磁盘后虚拟机中磁盘不出现解决: 计算机---> 管理: 这里可以选择磁盘,格式,分区, 改盘符等操作

  8. 预测一下web前端未来的6个趋势

    2018年前端技术的发展也将进入到一个相对稳定的阶段, 就前端主流技术框架的发展而言,过去的几年里发展极快,在填补原有技术框架空白和不足的同时也渐渐趋于成熟. 未来前端在已经趋向成熟的技术方向上面将会 ...

  9. Hadoop_HDFS-基础知识摘要

    Hadoop典型应用有:搜索.日志处理.推荐系统.数据分析.视频图像分析.数据保存等.0.数据要首先分块 Block:将一个文件进行分块,通常是64M. NameNode:--管理节点保存整个文件系统 ...

  10. js Date() 时间函数处理 关于 toLocaleDateString()

    toLocaleDateString()方法的真正含义为「根据本地时间把Date对象的日期部分转换为字符串」,这意味着:在不同的浏览器或者服务器中,我们可能得到不同的字符串. 例如,将 Chrome ...