题意:

      要求构造一个矩阵,给你行和,列和,还有一些点的上下范围,输出一个满足题意的矩阵。

思路:

      这个题目很经典,这是自己看上下流后接触的第一道题,感觉很基础的一道题目,现在我们来分析下,如果这个题目是只给行和,列和,让我们构造矩阵应该很简单了,直接一遍最大流,然后判断是否满流,满流就把残余网络拿出来,整理下就是答案了,关键这个题

目就是不但要求满流,某些点还有上限制,或者下限制,那么就直接是上下流呗,对了还有一个地方提醒一下,在建图之前判断一下有没有输入数据冲突的情况,下面说关键部分,也就是建图,建图之前定义几个变量,s(源点),t(汇点),ss(超级源点),tt(超级汇点).


s连接所有的行i              add(s ,i ,行和 , 行和);

所有的列j连接终点t          add(j ,t ,列和 ,列和);

建立一条t -> s              add(t ,s ,0 ,INF);//为了把有源汇的最大流变成无源的

对于任意两点i,j             add(i ,j ,下限 ,上限);

简单说下上下界网络流可行流判断

首先,可行流的判断就是根据在流里面,任意点的流入和流出永远都必须是相等的。

对于一个加边操作,a -> b ,下界 上界 可以这样处理

a -> b 流量为上界减去下界   这个可以叫自由边(就是不是必须流的边)

a -> tt ,ss -> b 流量都是下界   这两个叫做必须边,要想有解,必须边最后必须满流 

如果是有源的,那么我们就 add(t ,s ,0 ,INF);变成无源

最后跑一遍 ss,tt的最大流,如果满流则有可行解,输出答案的话知道把所有自由边拿出来,加上下限就可以了。(因为此时下限已满流).


  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. #define N_node 240
  5. #define N_edge 50000
  6. #define INF 1000000000
  7. using namespace std;
  8. typedef struct
  9. {
  10. int from ,to ,next ,cost;
  11. }STAR;
  12. typedef struct
  13. {
  14. int x ,t;
  15. }DEP;
  16. STAR E[N_edge];
  17. DEP xin ,tou;
  18. int list[N_node] ,listt[N_node] ,tot;
  19. int deep[N_node] ,sum_must;
  20. int map[220][22][3];
  21. int Ans[220][22];
  22. int maxx(int x ,int y)
  23. {
  24. return x > y ? x : y;
  25. }
  26. int minn(int x ,int y)
  27. {
  28. return x < y ? x : y;
  29. }
  30. void add(int a ,int b ,int c)
  31. {
  32. E[++tot].from = a;
  33. E[tot].to = b;
  34. E[tot].cost = c;
  35. E[tot].next = list[a];
  36. list[a] = tot;
  37. E[++tot].from = b;
  38. E[tot].to = a;
  39. E[tot].cost = 0;
  40. E[tot].next = list[b];
  41. list[b] = tot;
  42. }
  43. void ADD(int a ,int b ,int c ,int d ,int ss ,int tt)
  44. {
  45. add(a ,b ,d - c);
  46. add(a ,tt ,c);
  47. add(ss ,b ,c);
  48. sum_must += c;
  49. }
  50. bool BFS_Deep(int s ,int t ,int n)
  51. {
  52. xin.x = s ,xin.t = 0;
  53. queue<DEP>q;
  54. q.push(xin);
  55. memset(deep ,255 ,sizeof(deep));
  56. deep[s] = 0;
  57. while(!q.empty())
  58. {
  59. tou = q.front();
  60. q.pop();
  61. for(int k = list[tou.x] ;k ;k = E[k].next)
  62. {
  63. xin.x = E[k].to;
  64. xin.t = tou.t + 1;
  65. if(deep[xin.x] != -1 || !E[k].cost)
  66. continue;
  67. deep[xin.x] = xin.t;
  68. q.push(xin);
  69. }
  70. }
  71. for(int i = 0 ;i <= n ;i ++)
  72. listt[i] = list[i];
  73. return deep[t] != -1;
  74. }
  75. int DFS_Flow(int s ,int t ,int flow)
  76. {
  77. if(s == t) return flow;
  78. int nowflow = 0;
  79. for(int k = listt[s] ;k ;k = E[k].next)
  80. {
  81. listt[s] = k;
  82. int to = E[k].to;
  83. int c = E[k].cost;
  84. if(deep[to] != deep[s] + 1 || !c)
  85. continue;
  86. int tmp = DFS_Flow(to ,t ,minn(c ,flow - nowflow));
  87. nowflow += tmp;
  88. E[k].cost -= tmp;
  89. E[k^1].cost += tmp;
  90. if(nowflow == flow) break;
  91. }
  92. if(!nowflow) deep[s] = 0;
  93. return nowflow;
  94. }
  95. int DINIC(int s ,int t ,int n)
  96. {
  97. int ans = 0;
  98. while(BFS_Deep(s ,t ,n))
  99. {
  100. ans += DFS_Flow(s ,t ,INF);
  101. }
  102. return ans;
  103. }
  104. bool jude(int n ,int m)
  105. {
  106. for(int i = 1 ;i <= n ;i ++)
  107. for(int j = 1 ;j <= m ;j ++)
  108. if(map[i][j][1] > map[i][j][2]) return 0;
  109. return 1;
  110. }
  111. int main ()
  112. {
  113. int a ,b ,c ,i ,j ,n ,m ,w ,T;
  114. char str[4];
  115. scanf("%d" ,&T);
  116. while(T--)
  117. {
  118. scanf("%d %d" ,&n ,&m);
  119. memset(list ,0 ,sizeof(list)) ,tot = 1;
  120. sum_must = 0;
  121. int s = 0 ,t = n + m + 1 ,ss = n + m + 2 ,tt = n + m + 3;
  122. for(i = 1 ;i <= n ;i ++)
  123. {
  124. scanf("%d" ,&a);
  125. ADD(s ,i ,a ,a ,ss ,tt);
  126. }
  127. for(i = 1 ;i <= m ;i ++)
  128. {
  129. scanf("%d" ,&a);
  130. ADD(i + n ,t ,a ,a ,ss ,tt);
  131. }
  132. for(i = 1 ;i <= n ;i ++)
  133. for(j = 1 ;j <= m ;j ++)
  134. map[i][j][1] = 0 ,map[i][j][2] = INF;
  135. scanf("%d" ,&w);
  136. while(w--)
  137. {
  138. scanf("%d %d %s %d" ,&a ,&b ,str ,&c);
  139. if(a && b)
  140. {
  141. if(str[0] == '<') map[a][b][2] = minn(map[a][b][2] ,c - 1);
  142. if(str[0] == '=') map[a][b][1] = maxx(map[a][b][1] ,c) ,map[a][b][2] = minn(map[a][b][2] ,c);
  143. if(str[0] == '>') map[a][b][1] = maxx(map[a][b][1] ,c + 1);
  144. }
  145. if(a && !b)
  146. {
  147. for(j = 1 ;j <= m ;j ++)
  148. {
  149. if(str[0] == '<') map[a][j][2] = minn(map[a][j][2] ,c - 1);
  150. if(str[0] == '=') map[a][j][1] = maxx(map[a][j][1] ,c) ,map[a][j][2] = minn(map[a][j][2] ,c);
  151. if(str[0] == '>') map[a][j][1] = maxx(map[a][j][1] ,c + 1);
  152. }
  153. }
  154. if(!a && b)
  155. {
  156. for(j = 1 ;j <= n ;j ++)
  157. {
  158. if(str[0] == '<') map[j][b][2] = minn(map[j][b][2] ,c - 1);
  159. if(str[0] == '=') map[j][b][1] = maxx(map[j][b][1] ,c) ,map[j][b][2] = minn(map[j][b][2] ,c);
  160. if(str[0] == '>') map[j][b][1] = maxx(map[j][b][1] ,c + 1);
  161. }
  162. }
  163. if(!a && !b)
  164. {
  165. for(i = 1 ;i <= n ;i ++)
  166. for(j = 1 ;j <= m ;j ++)
  167. {
  168. if(str[0] == '<') map[i][j][2] = minn(map[i][j][2] ,c - 1);
  169. if(str[0] == '=') map[i][j][1] = maxx(map[i][j][1] ,c) ,map[i][j][2] = minn(map[i][j][2] ,c);
  170. if(str[0] == '>') map[i][j][1] = maxx(map[i][j][1] ,c + 1);
  171. }
  172. }
  173. }
  174. if(!jude(n ,m))
  175. {
  176. puts("IMPOSSIBLE");
  177. continue;
  178. }
  179. for(i = 1 ;i <= n ;i ++)
  180. for(j = 1 ;j <= m ;j ++)
  181. ADD(i ,j + n ,map[i][j][1] ,map[i][j][2] ,ss ,tt);
  182. ADD(t ,s ,0 ,INF ,ss ,tt);
  183. int Flow = DINIC(ss ,tt ,tt);
  184. if(Flow != sum_must)
  185. {
  186. puts("IMPOSSIBLE");
  187. continue;
  188. }
  189. for(i = 2 ;i <= tot ;i ++)
  190. if(E[i].from >= 1 && E[i].from <= n && E[i].to >= n + 1 && E[i].to <= n + m)
  191. Ans[E[i].from][E[i].to - n] = E[i^1].cost + map[E[i].from][E[i].to - n][1];
  192. for(i = 1 ;i <= n ;i ++)
  193. for(j = 1 ;j <= m ;j ++)
  194. if(j == m)printf("%d\n" ,Ans[i][j]);
  195. else printf("%d " ,Ans[i][j]);
  196. if(T) puts("");
  197. }
  198. return 0;
  199. }

POJ 2396 构造矩阵(上下流)的更多相关文章

  1. POJ 2396 Budget 有上下界的网络流

    POJ 2396  Budget 题意简述:给定矩阵(每个元素都是非负整数)各行各列的和,并且限制其中的某些元素,给出一个可行解,特殊评测.矩阵规模小于200*20. 网络流的模型是显而易见的,不过对 ...

  2. POJ 2396 Budget ——有上下界的网络流

    给定矩阵的每行每列的和,和一些大于小于等于的限制.然后需要求出一组可行解. 上下界网络流. 大概的思想就是计算出每一个点他需要强行流入或者流出的量,然后建出超级源点和汇点,然后删除下界,就可以判断是否 ...

  3. poj 2396 Budget 边容量有上下界的最大流

    题意: 给一个矩阵的每行和及每列和,在给一些行列或点的限制条件.求一个满足的矩阵. 分析: 转化为有上下界的网络流,注意等于也是一种上下界关系,然后用dinic算法. 代码: //poj 2396 / ...

  4. poj 3735 Training little cats(构造矩阵)

    http://poj.org/problem?id=3735 大致题意: 有n仅仅猫,開始时每仅仅猫有花生0颗,现有一组操作,由以下三个中的k个操作组成: 1. g i 给i仅仅猫一颗花生米 2. e ...

  5. POJ 3233 Matrix Power Series(构造矩阵求等比)

    Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. ...

  6. POJ 3070 Fibonacci(矩阵高速功率)

    职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...

  7. hdu 5015 233 Matrix(构造矩阵)

    http://acm.hdu.edu.cn/showproblem.php?pid=5015 由于是个二维的递推式,当时没有想到能够这样构造矩阵.从列上看,当前这一列都是由前一列递推得到.依据这一点来 ...

  8. [数学-构造矩阵]NEFU 1113

    依据题意.我已经推导出tn的公式.ti=ti.a+ti.b,ti.a=5*t(i-1).a+4*t(i-1).b,ti.b=t(i-1).a+t(i-1).b 然而以下居然不能继续推到sn的公式!!! ...

  9. HDU1757-A Simple Math Problem,矩阵快速幂,构造矩阵水过

    A Simple Math Problem 一个矩阵快速幂水题,关键在于如何构造矩阵.做过一些很裸的矩阵快速幂,比如斐波那契的变形,这个题就类似那种构造.比赛的时候手残把矩阵相乘的一个j写成了i,调试 ...

随机推荐

  1. 剑指 Offer 15. 二进制中1的个数

    剑指 Offer 15. 二进制中1的个数 Offer 15 题目描述: 方法一:使用1逐位相与的方式来判断每位是否为1 /** * 方法一:使用1逐位与的方法 */ public class Off ...

  2. Microsoft Teams 最新功能发布:协作篇

    正在进行的2021年的Microsoft Ignite大会,发布了一系列跟Microsoft Teams相关的新功能,英文介绍请参考 https://techcommunity.microsoft.c ...

  3. Java I/O流 01

    文件IO·异常 和 File类 异常的概述和分类 * A:异常的概述 * 异常就是Java程序在运行过程中出现的错误 * B:异常的分类 * 用过API查看Throwable * Error * 服务 ...

  4. mysql 基本指令 1

    desc 表名  --查看表属性 show create table 表名 \g;  --查看代码 alter table 表名 auto_increment=20;  --改自增的值 MySQL:自 ...

  5. 由于makefile编译所有子目录中 sed 's,/($*/)/.o[ :],/1.o $@ : ,g' <$@ > $@ 的解释

    这个语句分为好几层,我们一层一层来看 1. sed 's,/($*/)/.o[ :],/1.o $@ : ,g' <$@ > $@ 首先看加粗这一层,$@表示目标参数中的.d文件, '&l ...

  6. JS的this指向深入

    this指向深入 this的绑定规则 默认绑定 this默认指向了window 全局环境下this指向了window 函数独立调用,函数内部的this也指向了window <script> ...

  7. 07-Spring ConfigurationClassPostProcessor

    ConfigurationClassPostProcessor 功能 此类是一个后置处理器类,主要功能是参与 BeanFactory 中 BeanDefinition 的操作和 BeanDefinit ...

  8. Spring 声明式事务与编程式事务详解

    本文转载自IBM开发者论坛:https://developer.ibm.com/zh/articles/os-cn-spring-trans 根据自己的学习理解有所调整,用于学习备查. 事务管理对于企 ...

  9. Shuffle Card HDU - 6707

    题目链接:https://vjudge.net/problem/HDU-6707 题意:给你一个数组a[ ](a[1]=1,a[2]=2.....a[n]=n),然后m次操作,每次把那个数拿到最前面去 ...

  10. 【linux】命令-网络相关

    目录 前言 1. ifconfig 1.1 语法 1.2 参数说明 1.3 例程 2. iw 2.1 扫描可用无线网络 2.2 WiFi连接步骤(教程A) 2.2.1 查看可以用无线设备信息 2.2. ...