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

主题链接:http://acm.hdu.edu.cn/showproblem.php?

pid=4975

——>>方法如:http://blog.csdn.net/scnu_jiechao/article/details/40658221

先做hdu - 4888,再来做此题的时候,感觉这题好 SB 呀。将代码提交后自己就 SB 了。咋 TLE 了??

此题卡时间比較严。。能够在判环的地方优化一下4888的代码。由于原选建好的图中的行到列的边,在判环的时候,每条边会被判 (N - 1) * (M - 1) + 1 次,假设先对残量网络又一次建图,那么就仅仅会在建图的时候被判一次。差距甚远。

注:输入开挂会RE。猜想输入数据中有负数。。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <algorithm>
  5.  
  6. using std::min;
  7. using std::queue;
  8.  
  9. const int MAXN = 500 * 2 + 10;
  10. const int MAXM = 500 * 500 + 2 * MAXN;
  11. const int INF = 0x3f3f3f3f;
  12.  
  13. struct EDGE
  14. {
  15. int from;
  16. int to;
  17. int cap;
  18. int flow;
  19. int nxt;
  20. } edge[MAXM << 1];
  21.  
  22. int N, M, kase;
  23. int sum;
  24. int S, T;
  25. int hed[MAXN], ecnt;
  26. int cur[MAXN], h[MAXN];
  27. bool impossible, bUnique;
  28.  
  29. void Init()
  30. {
  31. impossible = false;
  32. bUnique = true;
  33. ecnt = 0;
  34. memset(hed, -1, sizeof(hed));
  35. }
  36.  
  37. void AddEdge(int u, int v, int cap)
  38. {
  39. edge[ecnt].from = u;
  40. edge[ecnt].to = v;
  41. edge[ecnt].cap = cap;
  42. edge[ecnt].flow = 0;
  43. edge[ecnt].nxt = hed[u];
  44. hed[u] = ecnt++;
  45. edge[ecnt].from = v;
  46. edge[ecnt].to = u;
  47. edge[ecnt].cap = 0;
  48. edge[ecnt].flow = 0;
  49. edge[ecnt].nxt = hed[v];
  50. hed[v] = ecnt++;
  51. }
  52.  
  53. bool Bfs()
  54. {
  55. memset(h, -1, sizeof(h));
  56. queue<int> qu;
  57. qu.push(S);
  58. h[S] = 0;
  59. while (!qu.empty())
  60. {
  61. int u = qu.front();
  62. qu.pop();
  63. for (int e = hed[u]; e != -1; e = edge[e].nxt)
  64. {
  65. int v = edge[e].to;
  66. if (h[v] == -1 && edge[e].cap > edge[e].flow)
  67. {
  68. h[v] = h[u] + 1;
  69. qu.push(v);
  70. }
  71. }
  72. }
  73.  
  74. return h[T] != -1;
  75. }
  76.  
  77. int Dfs(int u, int cap)
  78. {
  79. if (u == T || cap == 0) return cap;
  80.  
  81. int flow = 0, subFlow;
  82. for (int e = cur[u]; e != -1; e = edge[e].nxt)
  83. {
  84. cur[u] = e;
  85. int v = edge[e].to;
  86. if (h[v] == h[u] + 1 && (subFlow = Dfs(v, min(cap, edge[e].cap - edge[e].flow))) > 0)
  87. {
  88. flow += subFlow;
  89. edge[e].flow += subFlow;
  90. edge[e ^ 1].flow -= subFlow;
  91. cap -= subFlow;
  92. if (cap == 0) break;
  93. }
  94. }
  95.  
  96. return flow;
  97. }
  98.  
  99. int Dinic()
  100. {
  101. int maxFlow = 0;
  102.  
  103. while (Bfs())
  104. {
  105. memcpy(cur, hed, sizeof(hed));
  106. maxFlow += Dfs(S, INF);
  107. }
  108.  
  109. return maxFlow;
  110. }
  111.  
  112. int ReadInt()
  113. {
  114. int ret = 0;
  115. char ch;
  116.  
  117. while ((ch = getchar()) && ch >= '0' && ch <= '9')
  118. {
  119. ret = ret * 10 + ch - '0';
  120. }
  121.  
  122. return ret;
  123. }
  124.  
  125. void Read()
  126. {
  127. int r, c;
  128. int rsum = 0, csum = 0;
  129.  
  130. scanf("%d%d", &N, &M);
  131. S = 0;
  132. T = N + M + 1;
  133. getchar();
  134. for (int i = 1; i <= N; ++i)
  135. {
  136. // r = ReadInt();
  137. scanf("%d", &r);
  138. rsum += r;
  139. AddEdge(S, i, r);
  140. }
  141. for (int i = 1; i <= M; ++i)
  142. {
  143. // c = ReadInt();
  144. scanf("%d", &c);
  145. csum += c;
  146. AddEdge(i + N, T, c);
  147. }
  148.  
  149. if (rsum != csum)
  150. {
  151. impossible = true;
  152. return;
  153. }
  154.  
  155. sum = rsum;
  156. for (int i = 1; i <= N; ++i)
  157. {
  158. for (int j = M; j >= 1; --j)
  159. {
  160. AddEdge(i, j + N, 9);
  161. }
  162. }
  163. }
  164.  
  165. void CheckPossible()
  166. {
  167. if (impossible) return;
  168. if (Dinic() != sum)
  169. {
  170. impossible = true;
  171. }
  172. }
  173.  
  174. void AddEdge(int u, int v)
  175. {
  176. edge[ecnt].to = v;
  177. edge[ecnt].nxt = hed[u];
  178. hed[u] = ecnt++;
  179. }
  180.  
  181. void ReBuild()
  182. {
  183. memset(hed, -1, sizeof(hed));
  184. int total = ecnt;
  185. ecnt = 0;
  186. for (int e = 0; e < total; ++e)
  187. {
  188. if (edge[e].cap > edge[e].flow)
  189. {
  190. AddEdge(edge[e].from, edge[e].to);
  191. }
  192. }
  193. }
  194.  
  195. bool vis[MAXN];
  196. bool CheckCircle(int x, int f)
  197. {
  198. vis[x] = true;
  199. for (int e = hed[x]; e != -1; e = edge[e].nxt)
  200. {
  201. int v = edge[e].to;
  202. if (v != f)
  203. {
  204. if (vis[v]) return true;
  205. if (CheckCircle(v, x)) return true;
  206. }
  207. }
  208. vis[x] = false;
  209. return false;
  210. }
  211.  
  212. void CheckUnique()
  213. {
  214. if (impossible) return;
  215. memset(vis, 0, sizeof(vis));
  216. for (int i = 1; i <= N; ++i)
  217. {
  218. if (CheckCircle(i, -1))
  219. {
  220. bUnique = false;
  221. return;
  222. }
  223. }
  224. }
  225.  
  226. void Output()
  227. {
  228. printf("Case #%d: ", ++kase);
  229. if (impossible)
  230. {
  231. puts("So naive!");
  232. }
  233. else if (bUnique)
  234. {
  235. puts("So simple!");
  236. }
  237. else
  238. {
  239. puts("So young!");
  240. }
  241. }
  242.  
  243. int main()
  244. {
  245. int T;
  246.  
  247. scanf("%d", &T);
  248. while (T--)
  249. {
  250. Init();
  251. Read();
  252. CheckPossible();
  253. ReBuild();
  254. CheckUnique();
  255. Output();
  256. }
  257.  
  258. return 0;
  259. }

hdu - 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 最大流+找环

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

  4. HDOJ 4975 A simple Gaussian elimination problem.

    和HDOJ4888是一样的问题,最大流推断多解 1.把ISAP卡的根本出不来结果,仅仅能把全为0或者全为满流的给特判掉...... 2.在残量网络中找大于2的圈要用一种类似tarjian的方法从汇点開 ...

  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. hdu 4972 A simple dynamic programming problem(高效)

    pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...

随机推荐

  1. Jenkins: 使用Jenkins搭建持续集成(CI)环境

    http://www.cnitblog.com/luckydmz/archive/2012/01/03/77007.html 首先从官网http://jenkins-ci.org/下载 Java We ...

  2. XPSP2 PSDK(还有lostspeed)

    XPSP2 PSDK Full Download with Local Install Use the full download to copy the entire Windows XP SP2 ...

  3. Next-Key Locks

    Next-Key Locks 一个next-key lock 是 一个record lock 在index record 和 一个区间锁 在一个区间在index record之前 InnoDB 执行 ...

  4. C# Http以文件的形式上传文件

    以下的是上传的方法: // <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summary> /// <para ...

  5. 硬盘重装Ubuntu12.04的感受

    好久没更blog了,最近这两天系统也出了问题,win7蓝屏,ubuntu进不去-.后来win7整好了,ubuntu依旧顽固.用惯了linux,就不想在转到win7下面了,估计是习惯了各种敲命令的感觉吧 ...

  6. WM_PARENTNOTIFY的作用(不完全)

    VCL里源码如下: procedure TWinControl.WMParentNotify(var Message: TWMParentNotify); begin with Message do ...

  7. poj 2411 Mondriaan's Dream dp

    一个比较简单的状压dp,记录下每个点的状态即可. #include <iostream> #include <cstdio> #include <cstring> ...

  8. XML实例文档

    from: http://www.w3school.com.cn/xpath/xpath_examples.asp XML实例文档 我们将在下面的例子中使用这个 XML 文档: "books ...

  9. 世界gis相关的资源网站分类整理

    ********************首先介绍个新颖的GIS论坛——GIS520论坛******************** GIS520论坛(共享地信学习资源的专业论坛) www.gis520.c ...

  10. ffplay2 android 版正式公布

    项目地址:https://github.com/DeYangLiu/AndroidPlayer/ 下载链接: 看点: 支持软键盘输入和历史记录.使用了EditText和内部存储. 这里考虑了历史记录的 ...