POJ 3422 Kaka's Matrix Travels 最小费用流
来源:互联网 发布:2016十大网络用语 编辑:程序博客网 时间:2024/05/22 00:50
Description
On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels withSUM = 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 toSUM in each grid the rook visited, and replaces it with zero. It is not difficult to know the maximumSUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximumSUM he can obtain after his Kth travel. Note the SUM is accumulative during theK travels.
Input
The first line contains two integers N andK (1 ≤ N ≤ 50, 0 ≤ K ≤ 10) described above. The followingN 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 hisKth travel.
Sample Input
3 21 2 30 2 11 4 2
Sample Output
15
Source
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<iostream>#include<queue>using namespace std;#define MAX 100#define MAXN 100001#define MAXE 1000001#define IMAX 21474836struct GROUPS{int f,t,flow,cost,next;};GROUPS a[MAXE];int N,K,map[MAX][MAX],tot=0,last[MAXN],dist[MAXN],ans=0,fa[MAXN];bool vis[MAXN];void add(int from,int to,int cost,int flow){ a[tot].f=from; a[tot].t=to; a[tot].next=last[from]; a[tot].flow=flow; a[tot].cost=cost; last[from]=tot++; a[tot].f=to; a[tot].t=from; a[tot].next=last[to]; a[tot].flow=0; a[tot].cost=-cost; last[to]=tot++; }void pre_group(){ for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) { int getnum=(i-1)*N+j; add(getnum,N*N+getnum,-map[i][j],1); add(getnum,N*N+getnum,0,K-1); } for(int i=1;i<N;i++)//down to conect for(int j=1;j<=N;j++) { int nextnum=i*N+j; int nownum=(i-1)*N+j; add(N*N+nownum,nextnum,0,K); } for(int i=1;i<=N;i++)//right to conect for(int j=1;j<N;j++) { int nextnum=(i-1)*N+j+1; int nownum=(i-1)*N+j; add(N*N+nownum,nextnum,0,K); } add(0,1,0,K); add(2*N*N,2*N*N+1,0,K); }bool spfa(){ queue<int> Q; for(int i=0;i<=2*N*N+1;i++) dist[i]=IMAX; memset(fa,-1,sizeof(fa)); memset(vis,false,sizeof(vis)); dist[0]=0; vis[0]=true; Q.push(0); while(!Q.empty()) { int now=Q.front(); Q.pop(); for(int i=last[now];i!=-1;i=a[i].next) { int to=a[i].t; if(dist[now]+a[i].cost<dist[to] && a[i].flow) { dist[to]=dist[now]+a[i].cost; fa[to]=i; if(!vis[to]) { vis[to]=true; Q.push(to); } } } vis[now]=false; } if(dist[2*N*N+1]==IMAX) return false; return true;}void work(){ while(spfa()) { int minflow=IMAX; for(int i=fa[2*N*N+1];i!=-1;i=fa[a[i].f]) minflow=minflow>a[i].flow?a[i].flow:minflow; for(int i=fa[2*N*N+1];i!=-1;i=fa[a[i].f]) { a[i].flow-=minflow; a[i^1].flow+=minflow; } ans+=dist[2*N*N+1]*minflow; }}int main(){ //freopen("input.in","r",stdin); //freopen("output.out","w",stdout); memset(last,-1,sizeof(last)); scanf("%d%d",&N,&K); for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) scanf("%d",&map[i][j]); pre_group(); work(); printf("%d",-ans); //system("pause"); return 0;}
- poj 3422 Kaka's Matrix Travels 最小费用最大流
- POJ 3422 Kaka's Matrix Travels 最小费用流
- poj-3422-Kaka's Matrix Travels-最小费用最大流
- POJ 3422 Kaka's Matrix Travels 最小费用最大流
- POJ 3422 Kaka's Matrix Travels (最小费用流)
- poj 3422 Kaka's Matrix Travels(最小费用最大流)
- POJ Kaka's Matrix Travels 3422 (最小费用最大流)
- POJ 3422 Kaka's Matrix Travels (最小费用流)
- poj 3422 Kaka's Matrix Travels(最小费用流)
- poj Kaka's Matrix Travels 费用流
- Kaka's Matrix Travels POJ 3422 最大费用流
- POJ 3422 Kaka's Matrix Travels (最大费用最大流)
- POJ 3422 Kaka's Matrix Travels (最大费用最大流)
- POJ 3422 Kaka's Matrix Travels 费用流
- poj 3422 Kaka's Matrix Travels 费用流
- 【POJ】 3422 Kaka’s Matrix Travels 费用流
- POJ 3422 Kaka's Matrix Travels | 费用流
- poj 3422 Kaka's Matrix Travels (费用流)
- android jni 参数
- 基于ArcGIS的Web3D实现思路(二)
- sql2008 还原时 出现sqlserver数据库正在使用,所以无法获得对数据库的独占访问权
- CALayer3-层的属性
- 关联规则之apriori算法
- POJ 3422 Kaka's Matrix Travels 最小费用流
- jQuery 遍历
- CALayer4-自定义层
- 易解自定义view
- 用户10秒就关闭网站的25种原因
- Eclipse显示空格,Tab,回车和行号
- Linq SelectionMany用法
- 计算机科学相关刊物
- C++读写二进制文件块