POJ 3422 Kaka's Matrix Travels(费用流)
Kaka's Matrix Travels
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 POJ Monthly--2007.10.06, Huang, Jinsong
|
测试一下最小费用最大流的模板。
很经典的建图方法。
1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <iostream> 5 #include <string> 6 #include <queue> 7 using namespace std; 8 9 const int MAXN = 10000; 10 const int MAXM = 100000; 11 const int INF = 0x3f3f3f3f; 12 struct Edge 13 { 14 int to,next,cap,flow,cost; 15 }edge[MAXM]; 16 int head[MAXN],tol; 17 int pre[MAXN],dis[MAXN]; 18 bool vis[MAXN]; 19 int N;//节点总个数,节点编号从0~N-1 20 void init(int n) 21 { 22 N = n; 23 tol = 0; 24 memset(head,-1,sizeof(head)); 25 } 26 void addedge(int u,int v,int cap,int cost) 27 { 28 edge[tol].to = v; 29 edge[tol].cap = cap; 30 edge[tol].cost = cost; 31 edge[tol].flow = 0; 32 edge[tol].next = head[u]; 33 head[u] = tol++; 34 edge[tol].to = u; 35 edge[tol].cap = 0; 36 edge[tol].cost = -cost; 37 edge[tol].flow = 0; 38 edge[tol].next = head[v]; 39 head[v] = tol++; 40 } 41 bool spfa(int s,int t) 42 { 43 queue<int>q; 44 for(int i = 0;i < N;i++) 45 { 46 dis[i] = INF; 47 vis[i] = false; 48 pre[i] = -1; 49 } 50 dis[s] = 0; 51 vis[s] = true; 52 q.push(s); 53 while(!q.empty()) 54 { 55 int u = q.front(); 56 q.pop(); 57 vis[u] = false; 58 for(int i = head[u]; i != -1;i = edge[i].next) 59 { 60 int v = edge[i].to; 61 if(edge[i].cap > edge[i].flow && 62 dis[v] > dis[u] + edge[i].cost ) 63 { 64 dis[v] = dis[u] + edge[i].cost; 65 pre[v] = i; 66 if(!vis[v]) 67 { 68 vis[v] = true; 69 q.push(v); 70 } 71 } 72 } 73 } 74 if(pre[t] == -1)return false; 75 else return true; 76 } 77 //返回的是最大流,cost存的是最小费用 78 int minCostMaxflow(int s,int t,int &cost) 79 { 80 int flow = 0; 81 cost = 0; 82 while(spfa(s,t)) 83 { 84 int Min = INF; 85 for(int i = pre[t];i != -1;i = pre[edge[i^1].to]) 86 { 87 if(Min > edge[i].cap - edge[i].flow) 88 Min = edge[i].cap - edge[i].flow; 89 } 90 for(int i = pre[t];i != -1;i = pre[edge[i^1].to]) 91 { 92 edge[i].flow += Min; 93 edge[i^1].flow -= Min; 94 cost += edge[i].cost * Min; 95 } 96 flow += Min; 97 } 98 return flow; 99 } 100 101 int a[55][55]; 102 int main() 103 { 104 int n,k; 105 while(scanf("%d%d",&n,&k) == 2) 106 { 107 for(int i = 0;i < n;i++) 108 for(int j = 0;j < n;j++) 109 scanf("%d",&a[i][j]); 110 init(2*n*n+2); 111 for(int i = 0;i < n;i++) 112 for(int j = 0;j < n;j++) 113 { 114 addedge(n*i+j+1,n*n+n*i+j+1,1,-a[i][j]); 115 addedge(n*i+j+1,n*n+n*i+j+1,INF,0); 116 } 117 118 for(int i = 0;i < n;i++) 119 for(int j = 0;j < n;j++) 120 { 121 if(i < n-1) 122 addedge(n*n+n*i+j+1,n*(i+1)+j+1,INF,0); 123 if(j < n-1) 124 addedge(n*n+n*i+j+1,n*i+j+1+1,INF,0); 125 } 126 addedge(0,1,k,0); 127 addedge(2*n*n,2*n*n+1,INF,0); 128 int cost; 129 minCostMaxflow(0,2*n*n+1,cost); 130 printf("%d\n",-cost); 131 } 132 return 0; 133 }