1227 方格取数 2
Posted joeylee97
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1227 方格取数 2相关的知识,希望对你有一定的参考价值。
题目描述 Description
给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现在从(1,1)出发,可以往右或者往下走,最后到达(n,n),每达到一格,把该格子的数取出来,该格子的数就变成0,这样一共走K次,现在要求K次所达到的方格的数的和最大
输入描述 Input Description
第一行两个数n,k(1<=n<=50, 0<=k<=10)
接下来n行,每行n个数,分别表示矩阵的每个格子的数
输出描述 Output Description
一个数,为最大和
样例输入 Sample Input
3 1
1 2 3
0 2 1
1 4 2
样例输出 Sample Output
11
数据范围及提示 Data Size & Hint
1<=n<=50, 0<=k<=10
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<sstream> #include<algorithm> #include<queue> #include<deque> #include<iomanip> #include<vector> #include<cmath> #include<map> #include<stack> #include<set> #include<functional> #include<memory> #include<list> #include<string> using namespace std; typedef long long LL; typedef unsigned long long ULL; /* 每个点只能计算一次(不是只能过一次) 于是拆点,把一个点拆成入点和出点,入点和出点之间加两条边,一条是容量为1,费用为权值,一条是容量为INF,费用为0 把每个点向下方和右方加边 */ const int MAXN = 10000; const int MAXM = 100000; const int INF = 0x3f3f3f3f; struct Edge { int to, next, cap, flow, cost; }edge[MAXM]; int head[MAXN], tol; int pre[MAXN], dis[MAXN]; bool vis[MAXN]; int N;//节点总个数,节点编号从0~N-1 void init(int n) { N = n; tol = 0; memset(head, -1, sizeof(head)); } void addedge(int u, int v, int cap, int cost) { edge[tol].to = v; edge[tol].cap = cap; edge[tol].cost = cost; edge[tol].flow = 0; edge[tol].next = head[u]; head[u] = tol++; edge[tol].to = u; edge[tol].cap = 0; edge[tol].cost = -cost; edge[tol].flow = 0; edge[tol].next = head[v]; head[v] = tol++; } bool spfa(int s, int t) { queue<int>q; for (int i = 0; i < N; i++) { dis[i] = INF; vis[i] = false; pre[i] = -1; } dis[s] = 0; vis[s] = true; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost) { dis[v] = dis[u] + edge[i].cost; pre[v] = i; if (!vis[v]) { vis[v] = true; q.push(v); } } } } if (pre[t] == -1)return false; else return true; } //返回的是最大流,cost存的是最小费用 int minCostMaxflow(int s, int t, int &cost) { int flow = 0; cost = 0; while (spfa(s, t)) { int Min = INF; for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]) { if (Min > edge[i].cap - edge[i].flow) Min = edge[i].cap - edge[i].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; } return flow; } int g[55][55]; int n, k; inline int index(int i, int j) { return (i - 1)*n + j; } int main() { ios::sync_with_stdio(0); cin >> n >> k; init(2*n*n + 2); for (int i = 1; i <= n; i++) { for(int j = 1;j<=n;j++) { cin >> g[i][j]; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { int i1 = index(i, j); addedge(i1, i1 + n*n, 1, -g[i][j]); addedge(i1, i1 + n*n, INF, 0); if (i != n) { addedge(i1 + n*n, index(i + 1, j), INF, 0); } if (j != n) { addedge(i1 + n*n, index(i, j + 1), INF, 0); } } } addedge(0, 1, k, 0); addedge(n*n * 2, 2 * n * n + 1, INF, 0); int ans, tmp; tmp = minCostMaxflow(0, 2 * n*n + 1, ans); cout << -ans << endl; }
以上是关于1227 方格取数 2的主要内容,如果未能解决你的问题,请参考以下文章