Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define mx 200
- int rgraph[mx][mx], parent[mx];
- int nodes, src, sink;
- void makeEdge(int n, int m)
- {
- int i, j, k, u, v;
- for(i = 1; i <= n; i++)
- {
- for(j = 1; j <= m; j++)
- {
- u = i + j - 1;
- v = n + 2*m - 1 + i - j;
- rgraph[u][v] = 99;
- }
- }
- }
- bool isPath(int s = src, int t = sink)
- {
- bool visited[mx];
- int u, uv;
- memset(visited, false, sizeof(visited));
- visited[s] = true;
- queue<int>q;
- q.push(s);
- parent[s] = -1;
- while(!q.empty())
- {
- u = q.front();
- q.pop();
- for(int v = 0; v <= nodes; v++)
- {
- uv = rgraph[u][v];
- if(!visited[v] && rgraph[u][v] > 0)
- {
- parent[v] = u;
- visited[v] = true;
- q.push(v);
- }
- }
- }
- if(visited[t])
- return true;
- return false;
- }
- void maxFlow()
- {
- int u, v;
- while(isPath())
- {
- int path_flow = 10000000;
- for(v = sink; v != src; v = parent[v])
- {
- u = parent[v];
- path_flow = min(path_flow, rgraph[u][v]);
- }
- for(v = sink; v != src; v = parent[v])
- {
- u = parent[v];
- rgraph[u][v] -= path_flow;
- rgraph[v][u] += path_flow;
- }
- }
- }
- int main()
- {
- int m, n, i, j, k, t, kase(0), nd, mn, sub, r__g, u, v;
- scanf("%d", &t);
- while(t-- && ++kase)
- {
- scanf("%d %d", &n, &m);
- nodes = 2 * (n + m);
- src = 0, sink = nodes, nd = 1;
- k = n + m - 1;
- mn = min(m, n);
- int left[k], right[k];
- for(i = 0; i < k; i++)
- scanf("%d", &left[i]);
- for(i = 0; i < k; i++)
- scanf("%d", &right[i]);
- memset(rgraph, 0, sizeof(rgraph));
- //start of making the nodes
- for(i = 0, sub = 1; i < k; i++, nd++)
- {
- if((i+1) < mn)
- {
- rgraph[src][nd] = left[i] - sub;
- sub++;
- }
- else if((i+1) >= mn && (n + m - (i+1)) >= mn)
- rgraph[src][nd] = left[i] - sub;
- else
- {
- --sub;
- rgraph[src][nd] = left[i] - sub;
- }
- r__g = rgraph[src][nd];
- }
- for(i = 0, sub = 1; i < k; i++, nd++)
- {
- if((i+1) < mn)
- {
- rgraph[nd][sink] = right[i] - sub;
- sub++;
- }
- else if((i+1) >= mn && (n + m - (i+1)) >= mn)
- rgraph[nd][sink] = right[i] - sub;
- else
- {
- --sub;
- rgraph[nd][sink] = right[i] - sub;
- }
- r__g = rgraph[nd][sink];
- }
- //end of making the nodes
- makeEdge(n, m);
- maxFlow();
- printf("Case %d:\n", kase);
- for(i = 1; i <= n; i++)
- {
- for(j = 1; j <= m - 1; j++)
- {
- u = i + j - 1;
- v = n + 2*m - 1 + i - j;
- printf("%d ", rgraph[v][u] + 1);
- }
- u = i + j - 1;
- v = n + 2*m - 1 + i - j;
- printf("%d", rgraph[v][u] + 1);
- printf("\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement