POJ 3422 Kaka's Matrix Travels 求最大流最“大”费用流
来源:互联网 发布:mac连接小米鼠标方法 编辑:程序博客网 时间:2024/05/14 11:33
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 21 2 30 2 11 4 2
Sample Output
15
Source
#include<iostream>#include<algorithm>#include<cstring>#include<queue>#include<cstdio>using namespace std;const int MAXN=610*610*2+2;const int inf=1<<29;int pre[MAXN]; // pre[v] = k:在增广路上,到达点v的边的编号为kint dis[MAXN]; // dis[u] = d:从起点s到点u的路径长为dint vis[MAXN]; // inq[u]:点u是否在队列中int path[MAXN];int head[MAXN];int NE,tot,ans,max_flow,map[666][666];struct node{ int u,v,cap,cost,next;} Edge[MAXN<<2];void addEdge(int u,int v,int cap,int cost){ Edge[NE].u=u; Edge[NE].v=v; Edge[NE].cap=cap; Edge[NE].cost=cost; Edge[NE].next=head[u]; head[u]=NE++; Edge[NE].v=u; Edge[NE].u=v; Edge[NE].cap=0; Edge[NE].cost=-cost; Edge[NE].next=head[v]; head[v]=NE++;}int SPFA(int s,int t) // 源点为0,汇点为sink。{ int i; for(i=s;i<=t;i++) dis[i]=inf; memset(vis,0,sizeof(vis)); memset(pre,-1,sizeof(pre)); dis[s] = 0; queue<int>q; q.push(s); vis[s] =1; while(!q.empty()) // 这里最好用队列,有广搜的意思,堆栈像深搜。 { int u =q.front(); q.pop(); for(i=head[u]; i!=-1;i=Edge[i].next) { int v=Edge[i].v; if(Edge[i].cap >0&& dis[v]>dis[u]+Edge[i].cost) { dis[v] = dis[u] + Edge[i].cost; pre[v] = u; path[v]=i; if(!vis[v]) { vis[v] =1; q.push(v); } } } vis[u] =0; } if(pre[t]==-1) return 0; return 1;}void end(int s,int t){ int u, sum = inf; for(u=t; u!=s; u=pre[u]) { sum = min(sum,Edge[path[u]].cap); } max_flow+=sum; //记录最大流 for(u = t; u != s; u=pre[u]) { Edge[path[u]].cap -= sum; Edge[path[u]^1].cap += sum; ans += sum*Edge[path[u]].cost; // cost记录的为单位流量费用,必须得乘以流量。 }}int main(){ int i,j,n,s,t,k; while(scanf("%d%d",&n,&k)!=EOF) { int nn=n*n; memset(head,-1,sizeof(head)); NE=ans=max_flow=s=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&map[i][j]); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { int num=(i-1)*n+j; addEdge(num,num+nn,1,-map[i][j]); addEdge(num,num+nn,inf,0); if(j!=n) addEdge(num+nn,num+1,inf,0); if(i!=n) addEdge(num+nn,num+n,inf,0); } addEdge(s,1,k,0); t=nn*2+1; addEdge(nn*2,t,inf,0); while(SPFA(s,t)) { end(s,t); } printf("%d\n",-ans); } 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 最小费用最大流
- 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 Kaka's Matrix Travels 3422 (最小费用最大流)
- POJ Kaka's Matrix Travels (最大费用最大流)
- Kaka's Matrix Travels (最大费用流)
- 【最大费用流】Kaka's Matrix Travels
- poj Kaka's Matrix Travels 费用流
- POJ 3422 Kaka's Matrix Travels 解题报告(最大费用最大流)
- POJ 3422 - Kaka's Matrix Travels 构图最大费用最大流
- 一种实现无级变速代理的好方法
- 杭电acm 1013 Digital Roots
- android各种权限说明
- 学校行政会议管理系统
- 一个图示公钥私钥数字签名数字证书
- POJ 3422 Kaka's Matrix Travels 求最大流最“大”费用流
- static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别?(转)
- java中,IEEE754标准(四字节)的字节数据和浮点数之间的转化(Float and byte[])
- 22-JAVA面向对象-重写toString()与equals()方法
- 推荐一个很好dede模板
- 杭电acm 1020 Encoding
- Linux C 定时器 篇(四)单线程多定时器
- spring事务管理2
- ZOJ 3734: LIKE vs CANDLE