标签:des style blog http color io os java ar
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.
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.
The maximum SUM Kaka can obtain after his Kth travel.
3 2 1 2 3 0 2 1 1 4 2
15
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 10000; 18 struct arc{ 19 int v,w,f,next; 20 arc(int x = 0,int y = 0,int z = 0,int nxt = 0){ 21 v = x; 22 w = y; 23 f = z; 24 next = nxt; 25 } 26 }; 27 arc e[maxn*20]; 28 int head[maxn],p[maxn],d[maxn],S,T,tot; 29 bool in[maxn]; 30 int n,m; 31 queue<int>q; 32 void add(int u,int v,int w,int f){ 33 e[tot] = arc(v,w,f,head[u]); 34 head[u] = tot++; 35 e[tot] = arc(u,-w,0,head[v]); 36 head[v] = tot++; 37 } 38 bool spfa(){ 39 while(!q.empty()) q.pop(); 40 for(int i = S; i <= T; i++){ 41 d[i] = INF; 42 in[i] = false; 43 p[i] = -1; 44 } 45 d[S] = 0; 46 in[S] = true; 47 q.push(S); 48 while(!q.empty()){ 49 int u = q.front(); 50 q.pop(); 51 in[u] = false; 52 for(int i = head[u]; ~i; i = e[i].next){ 53 if(e[i].f > 0 && d[e[i].v] > d[u] + e[i].w){ 54 d[e[i].v] = d[u] + e[i].w; 55 p[e[i].v] = i; 56 if(!in[e[i].v]){ 57 q.push(e[i].v); 58 in[e[i].v] = true; 59 } 60 } 61 } 62 } 63 return p[T] > -1; 64 } 65 int solve(){ 66 int tmp = 0,minV; 67 while(spfa()){ 68 minV = INF; 69 for(int i = p[T]; ~i; i = p[e[i^1].v]) 70 minV = min(minV,e[i].f); 71 for(int i = p[T]; ~i; i = p[e[i^1].v]){ 72 e[i].f -= minV; 73 e[i^1].f += minV; 74 tmp += minV*e[i].w; 75 } 76 } 77 return tmp; 78 } 79 int main() { 80 int tmp; 81 while(~scanf("%d %d",&n,&m)){ 82 S = 0; 83 T = n*n*2 + 1; 84 tot = 0; 85 memset(head,-1,sizeof(head)); 86 for(int i = 1; i <= n; i++) 87 for(int j = 1; j <= n; j++){ 88 scanf("%d",&tmp); 89 add(n*(i-1)+j,n*(i-1)+j+n*n,-tmp,1); 90 add(n*(i-1)+j,n*(i-1)+j+n*n,0,INF); 91 if(i < n) add(n*(i-1)+j+n*n,n*i+j,0,INF); 92 if(j < n) add(n*(i-1)+j+n*n,n*(i-1)+j+1,0,INF); 93 } 94 add(S,1,0,m); 95 add(2*n*n,T,0,m); 96 printf("%d\n",-solve()); 97 } 98 return 0; 99 }
POJ 3422 Kaka's Matrix Travels
标签:des style blog http color io os java ar
原文地址:http://www.cnblogs.com/crackpotisback/p/3971435.html