Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7117 | Accepted: 2841 |
Description
On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka moves one rook from the left-upper grid to the right-bottom one, taking care that the rook moves only to the right or down. Kaka adds the number to SUM in each grid the rook visited, and replaces it with zero. It is not difficult to know the maximum SUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximum SUM he can obtain after his Kth travel. Note the SUM is accumulative during the K travels.
Input
The first line contains two integers N and K (1 ≤ N ≤ 50, 0 ≤ K ≤ 10) described above. The following N lines represents the matrix. You can assume the numbers in the matrix are no more than 1000.
Output
The maximum SUM Kaka can obtain after his Kth travel.
Sample Input
3 2 1 2 3 0 2 1 1 4 2
Sample Output
15
题意:给出一个n*n的格子,每个格子都有一个数值。要进行k次以下操作:
从最左上角开始到最右下角,每次只能向下或者向右,每到达一个格子,就能取得格子中的数值,并且要将格子中的数值变成0。
求k次以后所能获得的最大值。
思路:这题是hdu 2686 的加强版,这题是求k条路径,尽管每个格子的数值只能取一次,但每个格子可以经过多次。构图如下:
对于每个格子,拆成两个点,这两个点之间连两条边,一条费用为格子中的数值,流量是1,另一条费用为0,流量为INF;
每个格子向下面和右边相邻的格子连边,费用为0,流量为INF;
源点向最左上角点连边,费用为0,流量为k;
最右下角点向汇点连边,费用为0,流量为k。
AC代码:
#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> #include <queue> #include <ctime> #include <algorithm> #define ll __int64 using namespace std; const int INF = 1000000000; const int maxn = 10000; struct Edge{ int u, v, cost, cap, flow, next; }et[10000000]; int low[maxn], pre[maxn], dis[maxn], eh[maxn], G[maxn][maxn]; bool vis[maxn]; int s, t, num, n, k, anscost; bool ok(int x, int y){ if(x >= 1 && x <= n && y >= 1 && y <= n) return true; return false; } void init(){ memset(eh, -1, sizeof(eh)); num = 0; } void add(int u, int v, int cost, int cap, int flow){ Edge e = {u, v, cost, cap, flow, eh[u]}; et[num] = e; eh[u] = num++; } void addedge(int u, int v, int cost, int cap){ add(u, v, cost, cap, 0); add(v, u, -cost, 0, 0); } bool spfa(){ queue<int> Q; memset(pre, -1, sizeof(pre)); memset(low, 0, sizeof(low)); memset(vis, false, sizeof(vis)); fill(&dis[0], &dis[maxn], -INF); dis[s] = 0, low[s] = INF, vis[s] = true; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = false; for(int i = eh[u]; i != -1; i = et[i].next) { int v = et[i].v, cost = et[i].cost, cap = et[i].cap, flow = et[i].flow; if(cap - flow && dis[v] < dis[u] + cost) { dis[v] = dis[u] + cost; pre[v] = i; low[v] = min(low[u], cap - flow); if(!vis[v]) { vis[v] = true; Q.push(v); } } } } return dis[t] != -INF; } void costflow(){ anscost = 0; while(spfa()) { int x = pre[t]; anscost += low[t] * dis[t]; while(x != -1) { et[x].flow += low[t]; et[x^1].flow -= low[t]; x = pre[et[x].u]; } } } int main() { while(~scanf("%d%d", &n, &k)) { init(); s = 0, t = 2 * n * n + 1; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { scanf("%d", &G[i][j]); int u = (i - 1) * n + j; addedge(u, u + n * n, G[i][j], 1); addedge(u, u + n * n, 0, INF); } addedge(s, 1, 0, k); addedge(2 * n * n, t, 0, k); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { int u = (i - 1) * n + j; if(ok(i + 1, j)) addedge(u + n * n, u + n, 0, INF); if(ok(i, j + 1)) addedge(u + n * n, u + 1, 0, INF); } costflow(); printf("%d\n", anscost); } return 0; }