PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
All of you must have heard the name of Baker Vai. Yes, he rides a bike and likes to help people. That's why he is popular amongst general people.
Baker Vai lives in a city which can be modeled as a 2D m x n matrix. Where the north-west corner is cell 1, 1 and the south-east corner is cell m, n. In each cell there are certain amount of people who needs help which is already known to Baker Vai.
Each day Baker Vai starts his journey from the north-west corner and he can only go to east or south. This way he reaches the south-east corner of the city. After that he returns back to the north-west, but this time he can only move to west or north. He doesn't want a cell to be visited twice other than the two corners. And if he visits a cell, he helps all the people in the cell.
Now you are given the map of the city and the number of people who need help in all cells for a particular day. You have to help Baker Vai finding the maximum number of people he can help in that day.
Input starts with an integer T (≤ 25), denoting the number of test cases.
Each case contains a blank line and two integers, m, n (2 ≤ m, n ≤ 100). Each of the next m lines will contain n integers, denoting the number of people who are in need. In a cell there will be no more than 20 people and a cell can be empty, too.
For each test case, print the case number and the maximum number of people Baker Vai can help considering the above conditions.
Sample Input |
Output for Sample Input |
2
3 3 1 1 1 1 0 1 1 1 1
3 4 1 1 0 1 1 1 1 1 0 1 10 1 |
Case 1: 8 Case 2: 18 |
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define debug printf("1\n") #define W(a) while(a--) #define CLR(a, b) memset(a, b, sizeof(a)) #define Ri(a) scanf("%d", &a) #define Pi(a) printf("%d\n", (a)) #define FOR(i, l, r) for(int i = l; i <= r; i++) #define FOR1(i, l, r) for(int i = l; i < r; i++) #define MAXN 20010 #define MAXM 100000 #define INF 0x3f3f3f3f using namespace std; struct Edge{ int from, to, cap, flow, cost, next; }; Edge edge[MAXM]; int head[MAXN], edgenum; bool vis[MAXN]; int pre[MAXN], dist[MAXN]; int S, T; void init(){ edgenum = 0; CLR(head, -1); } void addEdge(int u, int v, int w, int c) { Edge E1 = {u, v, w, 0, c, head[u]}; edge[edgenum] = E1; head[u] = edgenum++; Edge E2 = {v, u, 0, 0, -c, head[v]}; edge[edgenum] = E2; head[v] = edgenum++; } bool BFS(int s, int t) { queue<int> Q; CLR(vis, false); CLR(pre, -1); CLR(dist, -INF); vis[s] = true; dist[s] = 0; Q.push(s); while(!Q.empty()) { //debug; int u = Q.front(); Q.pop(); vis[u] = false; for(int i = head[u]; i != -1; i = edge[i].next) { Edge E = edge[i]; if(dist[E.to] < dist[u] + E.cost && E.cap > E.flow) { dist[E.to] = dist[u] + E.cost; pre[E.to] = i; if(!vis[E.to]) { vis[E.to] = true; Q.push(E.to); } } } } return pre[t] != -1; } void MCMF(int s, int t, int &cost, int &flow) { cost = flow = 0; while(BFS(s, t)) { int Min = INF; for(int i = pre[t]; i != -1; i = pre[edge[i^1].to]) { Edge E = edge[i]; Min = min(Min, E.cap-E.flow); } for(int i = pre[t]; i != -1; i = pre[edge[i^1].to]) { edge[i].flow += Min; edge[i^1].flow -= Min; cost += edge[i].cost * Min; } flow += Min; } } int Map[110][110]; int n, m; int getpoint(int x, int y){ return (x-1) * m + y; } int solve() { Ri(n), Ri(m); init(); int temp = n * m; FOR(i, 1, n) { FOR(j, 1, m) { Ri(Map[i][j]); int can = 1; if(i == 1 && j == 1 || i == n && j == m) can = 2; addEdge(getpoint(i, j), getpoint(i, j) + temp, can, Map[i][j]); } } S = 1, T = getpoint(n, m) + temp; FOR(i, 1, n) { FOR(j, 1, m) { if(i < n) addEdge(getpoint(i, j)+temp, getpoint(i+1, j), 1, 0); if(j < m) addEdge(getpoint(i, j)+temp, getpoint(i, j+1), 1, 0); } } int cost, flow; MCMF(S, T, cost, flow); return cost-Map[1][1]-Map[n][m]; } int main() { int t, kcase = 1; Ri(t); W(t){ printf("Case %d: %d\n", kcase++, solve()); } return 0; }