1. #include <iostream>
  2. #include <cstdio>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <deque>
  7. #include <vector>
  8. #include <queue>
  9. #include <string>
  10. #include <cstring>
  11. #include <map>
  12. #include <stack>
  13. #include <set>
  14. #define INF 0x3f3f3f3f
  15. #define OPEN_FILE
  16. #define MAXN 626
  17. using namespace std;
  18.  
  19. int n;
  20. int win[MAXN], remain[MAXN][MAXN];
  21. struct Edge{
  22. int from, to, cap, flow;
  23. //Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f){};
  24. };
  25. bool comp(const Edge& a, const Edge& b){
  26. return (a.from < b.from || (a.from == b.from && a.to < b.to));
  27. }
  28. struct Dinic{
  29. int n, m, i, s, t;
  30. Edge e;
  31. vector<Edge> edges;
  32. vector<int> G[MAXN];
  33. int d[MAXN], cur[MAXN];
  34. bool vis[MAXN];
  35. void init(int n){
  36. this->n = n;
  37. for (i = ; i <= n; i++){
  38. G[i].clear();
  39. }
  40. edges.clear();
  41. }
  42. void AddEdge(int from, int to, int cap){
  43. edges.push_back(Edge{ from, to, cap, });
  44. edges.push_back(Edge{ to, from, , });
  45. m = edges.size();
  46. G[from].push_back(m - );
  47. G[to].push_back(m - );
  48. }
  49. bool BFS(){
  50. memset(vis, , sizeof(vis));
  51. queue<int> Q;
  52. Q.push(s);
  53. d[s] = ;
  54. vis[s] = ;
  55. while (!Q.empty()){
  56. int x = Q.front();
  57. Q.pop();
  58. for (i = ; i < G[x].size(); i++){
  59. Edge& e = edges[G[x][i]];
  60. if (!vis[e.to] && e.cap > e.flow){
  61. vis[e.to] = true;
  62. d[e.to] = d[x] + ;
  63. Q.push(e.to);
  64. }
  65. }
  66. }
  67. return vis[t];
  68. }
  69. int DFS(int x, int a){
  70. if (x == t || a == ) return a;
  71. int flow = , f;
  72. for (int& i = cur[x]; i < G[x].size(); i++){
  73. Edge& e = edges[G[x][i]];
  74. if (d[x] + == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > ){
  75. e.flow += f;
  76. edges[G[x][i] ^ ].flow -= f;
  77. flow += f;
  78. a -= f;
  79. if (a == ) break;
  80. }
  81. }
  82. return flow;
  83. }
  84. int MaxFlow(int s, int t, int need){
  85. int flow = ;
  86. this->s = s;
  87. this->t = t;
  88. while (BFS()){
  89. memset(cur, , sizeof(cur));
  90. flow += DFS(s, INF);
  91. if (flow > need) return flow;
  92. }
  93. return flow;
  94. }
  95. bool checkFull(int s){
  96. for (int i = ; i < G[s].size(); i++){
  97. if (edges[G[s][i]].flow != edges[G[s][i]].cap){
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
  103. };
  104.  
  105. int main()
  106. {
  107. #ifdef OPEN_FILE
  108. freopen("in.txt", "r", stdin);
  109. freopen("out.txt", "w", stdout);
  110. #endif // OPEN_FILE
  111. int T, x;
  112. scanf("%d", &T);
  113. for (int cas = ; cas <= T; cas++){
  114. scanf("%d", &n);
  115. memset(win, , sizeof(win));
  116. for (int i = ; i <= n; i++){
  117. scanf("%d%d", &win[i], &x);
  118. }
  119. memset(remain, , sizeof(remain));
  120. int p = ;
  121. for (int i = ; i <= n; i++){
  122. for (int j = ; j <= n; j++){
  123. scanf("%d", &x);
  124. if (i == j) continue;
  125. remain[i][] += x;
  126. if (remain[i][j] == && remain[j][i] == ){
  127. remain[i][j] = x;
  128. ++p;
  129. }
  130. }
  131. }
  132. int s = , t = n + p + , q;
  133. bool flag, first;
  134. Dinic ex;
  135. first = false;
  136. for (int k = ; k <= n; k++){
  137. ex.init(n * n);
  138. flag = false;
  139. q = ;
  140. int total = win[k] + remain[k][];
  141. for (int i = ; i <= n; i++){
  142. for (int j = i + ; j <= n; j++){
  143. if (!remain[i][j]) continue;
  144. ex.AddEdge(s, q, remain[i][j]);
  145. ex.AddEdge(q, p + i, INF);
  146. ex.AddEdge(q, p + j, INF);
  147. q++;
  148. }
  149. if (total - win[i] < ) {
  150. flag = true;
  151. break;
  152. }
  153. ex.AddEdge(p + i, t, total - win[i]);
  154. }
  155. if (flag){
  156. continue;
  157. }
  158. ex.MaxFlow(s, t, INF);
  159. if (ex.checkFull()){
  160. if (first){
  161. printf(" ");
  162. }
  163. printf("%d", k);
  164. first = true;
  165. }
  166. }
  167. printf("\n");
  168. }
  169. }

LA 2531 The K-league 最大流的更多相关文章

  1. POJ - 2516 Minimum Cost 每次要跑K次费用流

    传送门:poj.org/problem?id=2516 题意: 有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的.问为了满足不同买家的订单的最小的花费. 思路: 设立一个源 ...

  2. poj-2516.minimum cost(k次费用流)

    Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19883   Accepted: 7055 Des ...

  3. (算法入门经典大赛 优先级队列)LA 3135(之前K说明)

    A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor da ...

  4. hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙

    /** 题目:hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4106 ...

  5. 【wikioi】1034 家园(最大流+特殊的技巧)

    http://wikioi.com/problem/1034/ 太神了这题. 其实一开始我以为是费用流,但是总感觉不对. 原因是我没看到一句话,特定的时刻到达特定的点!! 也就是说,并不是每艘船每次都 ...

  6. BZOJ 2324 营救皮卡丘(最小费用最大流)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2324 题意:n+1个城市(0到n).初始时K个 人都在0城市.城市之间有距离.要求(1) ...

  7. hdu3081 Marriage Match II(二分+并查集+最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意: n个女生与n个男生配对,每个女生只能配对某些男生,有些女生相互是朋友,每个女生也可以跟她 ...

  8. POJ 2516 Minimum Cost (费用流)

    题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...

  9. 【费用流】【CODEVS】1227 方格取数2

    [算法]最小费用最大流(费用流) [题解] 费用流:http://www.cnblogs.com/onioncyc/p/6496532.html 本题构图: 在有限的k次行走中尽可能多的拿到数字,明显 ...

随机推荐

  1. CUDA中的归约

    CUDA编程实战书中的乘方和解决办法: 对一个数组执行某种计算,然后产生一个更小的结果数组. 由一个线程在共享内存上进行迭代并计算出总和值.而如果用并行,所花时间就与数组长度的对数成正比. 代码的思想 ...

  2. DNS Prefetching

    For Developers‎ > ‎Design Documents‎ > ‎ DNS Prefetching 目录 1 Problem 2 Solution 3 Architectur ...

  3. ArcGIS api for javascript——地理编码任务-地理编码地址

    描述 本例允许用户输入一个地址,然后显示匹配的地址的位置.这通常地被称为地理编码.在ArcGIS JavaScript API中,使用Locator类执行地理编码. 定位器构造函数需要ArcGIS S ...

  4. c3p0出现 An attempt by a client to checkout a Connection has timed out

    java.sql.SQLException: An attempt by a client to checkout a Connection has timed out. at com.mchange ...

  5. Codeforces 327A-Flipping Game(暴力枚举)

    A. Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  6. Python使用Redis实现一个简单作业调度系统

    Python使用Redis实现一个简单作业调度系统 概述 Redis作为内存数据库的一个典型代表,已经在非常多应用场景中被使用,这里仅就Redis的pub/sub功能来说说如何通过此功能来实现一个简单 ...

  7. 【LeetCode】3Sum Closest 解题报告

    [题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...

  8. UINavigationBar的系统渲染方式

    昨天想手工实现一下类知乎日报的Navigation Bar的动态颜色改变,但不管怎么设置Navigation Bar的 backgroundColor barTintColor alpha參数都达不到 ...

  9. vue.2.0-自定义全局组件

    App.vue <template> <div id="app"> <h3>welcome vue-loading</h3> < ...

  10. 10.MongoDB:将Json数据直接写入MongoDB的方法

    Json转Bson MongoDB中是以Bson数据格式进行存储的,Json字符串没有办法直接写入MongoDB 可以将Json字符串转换成DBObject或者Document,然后写入MongoDB ...