| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 6970 | Accepted: 2759 |
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
Source
#include <cstdio>
#include <cstring>
#include <vector>
#include<iostream>
#include <algorithm>
#include<queue>
using namespace std;
const int maxn = 5000 + 5; // 顶点的最大数目
const int INF = 1000000000;
struct Edge {
int from, to, cap, flow, cost;
};
struct MCMF {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn]; // 是否在队列中
int d[maxn]; // Bellman-Ford,单位流量的费用
int p[maxn]; // 上一条弧
int a[maxn]; // 可改进量
void init(int n) {
this->n = n;
for(int i = 0; i < n; i++) G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap, int cost) {
edges.push_back((Edge){from, to, cap, 0, cost});
edges.push_back((Edge){to, from, 0, 0, -cost});
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BellmanFord(int s, int t, int &flow,int &cost) {
for(int i = 0; i < n; i++) d[i] = INF;
memset(inq, 0, sizeof(inq));
d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;
queue<int> Q;
Q.push(s);
while(!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = 0;
for(int i = 0; i < G[u].size(); i++) {
Edge& e = edges[G[u][i]];
if(e.cap > e.flow && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if(!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; }
}
}
}
if(d[t] == INF) return false;//s-t不连通,失败退出
flow += a[t];
cost += d[t] * a[t];
int u = t;
while(u != s) {
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].from;
}
return true;
}
// 需要保证初始网络中没有负权圈
int Mincost(int s, int t) {
int flow = 0,cost = 0;
while(BellmanFord(s, t,flow, cost));
return cost;
}
};
MCMF g;
int M[maxn][maxn];
int main(){
int n,k;
while(scanf("%d%d",&n,&k) != EOF){
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
scanf("%d",&M[i][j]);
}
}
g.init(2*n*n+1);
int sorce = 2*n*n,sink = 2*n*n-1;
g.AddEdge(sorce,n*n,k,0);
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
g.AddEdge(i*n+j,i*n+j+n*n,1,-M[i][j]);
}
}
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
if(j != n-1){
g.AddEdge(i*n+j+n*n,i*n+j+1,INF,0);
g.AddEdge(i*n+j+n*n,i*n+j+1+n*n,INF,0);
}
if(i != n-1){
g.AddEdge(i*n+j+n*n,(i+1)*n+j,INF,0);
g.AddEdge(i*n+j+n*n,(i+1)*n+j+n*n,INF,0);
}
}
}
int ans = -g.Mincost(sorce,sink);
if(k != 0) ans += M[0][0];
printf("%d\n",ans);
}
return 0;
}
描述了一个从左上角到右下角的矩阵中,通过一系列旅行获取最大收益的问题。涉及到矩阵拆分、网络流算法的应用,以及如何在多条路径中优化收益。
207

被折叠的 条评论
为什么被折叠?



