Guest User

Untitled

a guest
Dec 15th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef unsigned long long llu;
  5. typedef vector<int> vi;
  6. typedef pair<int, int> ii;
  7. typedef vector<ii> vii;
  8. typedef set<int> si;
  9. typedef map<int,int> mii;
  10. typedef map<string, int> msi;
  11. typedef map<int,pair<int,int> > miii;
  12. typedef complex<double> point;
  13. typedef pair<int, ii> iii;
  14. typedef vector<iii> viii;
  15. typedef pair<int,pair<int,char> > iic;
  16. typedef vector<iic> viic;
  17. typedef pair<int,char> ic;
  18. typedef pair<int,pair<string, bool> > isb;
  19. #define X real()
  20. #define Y imag()
  21. #define FileIn(file) freopen(file".txt", "r", stdin)
  22. #define FileOut(file) freopen(file".txt", "w", stdout)
  23. #define Fill(ar, val) memset(ar, val, sizeof(ar))
  24. #define PI 3.1415926535897932385
  25. #define all(ar) ar.begin(), ar.end()
  26. #define pb push_back
  27. #define bit(n) (1<<(n))
  28. #define Last(i) (i & -i)
  29. #define INF 500000000
  30. #define maxN 10000010
  31. #define VISITED 1
  32. #define UNVISITED -1
  33. #define P(p) const point &p
  34. #define L(p0, p1) P(p0), P(p1)
  35. #define MAX 2*52*52+10
  36. vector<int > edge[MAX];
  37. int cap[MAX][MAX];
  38. int mf, f,s,t; // global variables
  39. vi p; // p stores the BFS spanning tree from s
  40. void augment(int v, int minEdge) { // traverse BFS spanning tree from s->t
  41. if (v == s) { f = minEdge; return; } // record minEdge in a global var f
  42. else if (p[v] != -1) { augment(p[v], min(minEdge, cap[p[v]][v]));
  43. cap[p[v]][v] -= f; cap[v][p[v]] += f; }
  44. }
  45. void BuildGraph(int S, int T, int H, int W);
  46. int maxFlow()
  47. {
  48. mf = 0;
  49. while (1) { // now a true O(VE^2) Edmonds Karp’s algorithm
  50. f = 0;
  51. bitset<MAX> vis; vis[s] = true; // we change vi dist to bitset!
  52. queue<int> q; q.push(s);
  53. p.assign(MAX, -1);
  54. while (!q.empty()) {
  55. int u = q.front(); q.pop();
  56. if (u == t) break;
  57. for (int j = 0; j < (int)edge[u].size(); j++) {
  58. int v = edge[u][j]; // we use vector<vi> edge
  59. if (cap[u][v] > 0 && !vis[v])
  60. vis[v] = true, q.push(v), p[v] = u;
  61. }
  62. }
  63. augment(t, INF);
  64. if (f == 0) break;
  65. mf += f;
  66. }
  67. return mf;
  68. }
  69. int main()
  70. {
  71. int Case, H, W, B, x, y;
  72. scanf("%d", &Case);
  73. while (Case--) {
  74. scanf("%d %d %d", &H, &W, &B);
  75.  
  76. s = 0; // super source
  77. t = 1; // super sink
  78.  
  79. for (int i = 0; i <= 2*(H+1)*W; ++i) edge[i].clear();
  80. memset(cap, 0, sizeof(cap));
  81.  
  82. BuildGraph(s, t, H, W);
  83.  
  84. for (int i = 0; i < B; ++i) {
  85. scanf("%d %d", &x, &y);
  86. cap[s][(x*H+y)*2] = 1;
  87. edge[s].push_back((x*H+y)*2);
  88. }
  89.  
  90. if (maxFlow() == B) puts("possible");
  91. else puts("not possible");
  92. }
  93. }
  94. void BuildGraph(int S, int T, int H, int W)
  95. {//Basically in the following what we are doing is that we are hashing
  96. //each coordinate(a,b) to a unique integer by using the formula
  97. //i*H+j
  98. /**Eg: Consider we have 3x3 grid, so:
  99. (1,1)=4 (1,2)=5 (1,3)=6
  100. (2,1)=7 (2,2)=8 (2,3)=9
  101. (3,1)=10 (3,2)=11 (3,3)=12
  102. Now if we were to do u to u' if we go like i*H+j+1 we land into collisions,
  103. so lets try; (i*H+j)*2
  104. (1,1)=8 (1,2)=10 (1,3)=12
  105. (2,1)=14 (2,2)=16 (2,3)=18
  106. (3,1)=20 (3,2)=22 (3,3)=24
  107. And now we can easily map from u to u' */
  108. for (int j = 1; j <= W; ++j)
  109. {
  110. for (int i = 1; i <= H; ++i)
  111. {
  112. cap[(i*H+j)*2][(i*H+j)*2+1] = 1; // u to u'
  113. edge[(i*H+j)*2].push_back((i*H+j)*2+1);
  114.  
  115. cap[(i*H+j)*2+1][((i-1)*H+j)*2] = 1; // u' to up
  116. edge[(i*H+j)*2+1].push_back(((i-1)*H+j)*2);
  117.  
  118. cap[(i*H+j)*2+1][((i+1)*H+j)*2] = 1; // u' to down
  119. edge[(i*H+j)*2+1].push_back(((i+1)*H+j)*2);
  120.  
  121. cap[(i*H+j)*2+1][(i*H+j-1)*2] = 1; // u' to left
  122. edge[(i*H+j)*2+1].push_back((i*H+j-1)*2);
  123.  
  124. cap[(i*H+j)*2+1][(i*H+j+1)*2] = 1; // u' to right
  125. edge[(i*H+j)*2+1].push_back((i*H+j+1)*2);
  126.  
  127. if (i == 1 || j == 1 || i == H || j == W) {// if on the edge
  128. cap[(i*H+j)*2+1][T] = 1; // connect u' to the T
  129. edge[(i*H+j)*2+1].push_back(T);
  130. }
  131. }
  132. }
  133. }
Add Comment
Please, Sign In to add comment