POJ 3422 Kaka's Matrix Travels (最小费用流)

网友投稿 696 2022-10-03

POJ 3422 Kaka's Matrix Travels (最小费用流)

POJ 3422 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 21 2 30 2 11 4 2

Sample Output

15

题意

给出一个 N∗N

思路

和 HDU 3376 一样的题型,只不过 HDU 3376 里面规定只有两条路径,而这里规定有 k 条,然后在建图策略中只需要修改原来矩阵中点到扩充点费用为0的边的容量为k-1即可。

具体请看:​​HDU 3376 Matrix Again ​​

AC 代码

#include#include#include#include#include#includeusing namespace std;#include#includeconst int MAXX = 5500;const int INF = 0x3f3f3f3f;struct edge{ int to; //边终点 int next; //下一个兄弟位置 int cap; //容量 int flow; //流量 int cost; //费用} edge[MAXX<<2];int head[MAXX],tol;int pre[MAXX],dis[MAXX];int N; //节点总个数void init(int n){ N=n; tol=0; memset(head,-1,sizeof(head));}void addedge(int u,int v,int cap,int cost) //同时增加原边与反向边{ edge[tol].to=v; edge[tol].cap=cap; edge[tol].cost=cost; edge[tol].flow=0; edge[tol].next=head[u]; head[u]=tol++; edge[tol].to=u; edge[tol].cap=0; edge[tol].cost=-cost; edge[tol].flow=0; edge[tol].next=head[v]; head[v]=tol++;}/* * SPFA 算法判断是否存在s到t的通路 */bool spfa(int s,int t){ queueq; bool vis[MAXX]; for(int i=0; iedge[i].flow&&dis[v]>dis[u]+edge[i].cost) //如果可以松弛该点 { dis[v]=dis[u]+edge[i].cost; pre[v]=i; if(!vis[v]) //如果该点不在队列中,入队 { vis[v]=true; q.push(v); } } } } return (pre[t]!=-1); //返回是否s到t是否有路径}/* * int s 起点 * int t 终点 */int minCostMaxFlow(int s,int t,int &cost){ int flow=0; while(spfa(s,t)) { int minn=INF; //当前路径可增广值 for(int i=pre[t]; i!=-1; i=pre[edge[i^1].to]) //因为建图时每增加一条边会同时加入它的反向边,因此i^1为找出与i刚好相反的部分 { if(minn>edge[i].cap-edge[i].flow) minn=edge[i].cap-edge[i].flow; } for(int i=pre[t]; i!=-1; i=pre[edge[i^1].to]) //修改图,计算花费 { edge[i].flow+=minn; edge[i^1].flow-=minn; cost+=edge[i].cost*minn; } flow+=minn; } return flow;}int main(){ int n,k; while(~scanf("%d%d",&n,&k)) { int N=n*n,temp,cost=0; init(N+N+2); for(int i=0; i

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:如何在小程序中使用腾讯视频插件(腾讯视频小程序播放插件)
下一篇:C++ 虚继承派生类构造函数的写法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~