HIT 2715 Matrix3(最大费用最大流)

news/2024/10/29 1:24:47/

Matrix3

My Tags   (Edit)
  Source : bin3
  Time limit : 5 sec   Memory limit : 64 M

Submitted : 302, Accepted : 74

Zhouguyue is a "驴友" and nowadays he likes traveling on an N * N matrix with a non-negative number in each grid, and each grid has a height. Zhouguyue starts his matrix travel with POINT = 0. For each travel, zhouguyue can land on any grid he wants with the help of bin3's helicopter, and then he can only move to ajacent grids whose height is less than his current height. Notice that when he is at the side of the matrix, he can also move out of the matrix. After he moves out of the matrix, he completes one travel. He adds the number in each grid he visited to POINT, and replaces it with zero. Now zhouguyue is wondering what is the maximum POINT he can obtain after he travels at most K times. Note the POINT is accumulative during the travels.

Input

The first line is a integer T indicating the number of test cases.T cases fllows. The first line of each case contains two integers N and K (1 ≤ N ≤ 50, 0 ≤ K ≤ 50) described above. The following N lines represents the matrix. You can assume the numbers in the matrix are non-negative integers and no more than 10000. The following N lines represents the height of each grid. The heghts are also non-negative integers.

Output

The maximum POINT zhouguyue can obtain after he travels at most K times.

Sample Input

1
3 2
1 2 3
3 2 1
2 4 23 5 3
2 1 0
1 2 3

Sample Output

17

题目:http://acm.hit.edu.cn/hoj/problem/view?id=2715

题意:就是一个矩阵,每次你可以任意选一个格子,然后可以跳到相邻的比他低的格子上去,每个格子里有分数,问连续k次能得到的分数和最大值。。。

分析:很明显的最大费用最大流。。。首先肯定要分点,把每个格子分成两个点,这两个点之间要连两条边,一条容量为1,费用为格子分数的相反数(转换为最小费用流),另一条边容量为无穷,费用为0(以下的边都是这样的边),表示格子可以多次通过,然后就是对于相邻的格子并满足条件的,也要连上一条边,注意点的分层,然后就是虚构出三个点,一个汇点,在矩阵边缘的格子都有连上她,一个源点,连到每个格子,一个总源点,连到源点,这条边要求容量为k就行了。。。

然后套模板。。。1Y

貌似hit的题很少人刷的样子,我居然是最快的- -


代码:

#include<cstdio>
#include<iostream>
using namespace std;
const int mm=66666;
const int mn=5555;
const int oo=1e9;
int src,dest,node,edge;
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
int ver[mm],cost[mm],flow[mm],next[mm];
int head[mn],dis[mn],p[mn],q[mn];
int h[55][55];
bool vis[mn]={0};
void prepare(int _node,int _src,int _dest)
{node=_node,src=_src,dest=_dest;for(int i=0;i<node;++i)head[i]=-1;edge=0;
}
void addedge(int u,int v,int f,int c)
{ver[edge]=v,flow[edge]=f,cost[edge]=c,next[edge]=head[u],head[u]=edge++;ver[edge]=u,flow[edge]=0,cost[edge]=-c,next[edge]=head[v],head[v]=edge++;
}
bool Spfa()
{int i,u,v,l,r=0,tmp;for(i=0;i<node;++i)dis[i]=oo;dis[q[r++]=src]=0;p[src]=p[dest]=-1;for(l=0;l!=r;(++l==mn)?l=0:l)for(i=head[u=q[l]],vis[u]=0;i>=0;i=next[i])if(flow[i]&&dis[v=ver[i]]>(tmp=dis[u]+cost[i])){dis[v]=tmp;p[v]=i^1;if(vis[v])continue;vis[q[r++]=v]=1;if(r==mn)r=0;}return p[dest]>-1;
}
int Spfaflow()
{int i,delta,ret=0;while(Spfa()){for(i=p[dest],delta=oo;i>=0;i=p[ver[i]])if(flow[i^1]<delta)delta=flow[i^1];for(i=p[dest];i>=0;i=p[ver[i]])flow[i]+=delta,flow[i^1]-=delta;ret-=delta*dis[dest];}return ret;
}
int main()
{int i,j,x,y,k,n,m,t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);prepare(n*n*2+3,n*n*2+1,n*n*2+2);for(i=0;i<n;++i)for(j=1;j<=n;++j){scanf("%d",&k);addedge(0,i*n+j,oo,0);addedge(i*n+j,n*n+i*n+j,1,-k);addedge(i*n+j,n*n+i*n+j,oo,0);}for(i=1;i<=n;++i)for(j=1;j<=n;++j)scanf("%d",&h[i][j]);for(i=1;i<=n;++i)for(j=1;j<=n;++j)for(k=0;k<4;++k){x=i+dx[k];y=j+dy[k];if(x<1||x>n||y<1||y>n||h[x][y]>=h[i][j])continue;addedge(n*n+i*n-n+j,x*n-n+y,oo,0);}for(i=1;i<=n;++i)for(j=1;j<=n;++j)if(i==1||j==1||i==n||j==n)addedge(n*n+n*i-n+j,dest,oo,0);addedge(src,0,m,0);printf("%d\n",Spfaflow());}return 0;
}




http://www.ppmy.cn/news/190343.html

相关文章

Ubuntu 18.04 cuda 9.0 双1080TI 只显示一张

追加&#xff1a;【已解决&#xff0c;有一张显卡硬件不稳定】 参考我的最终记录&#xff1a; https://blog.csdn.net/u012911347/article/details/82854018 这又是一篇关于cuda和nvidia的博客&#xff0c;暂时解决了显卡就只显示一张和cuda无法使用的问题。 如果你想了解更…

dell服务器接2k显示器,4K、2K已成主流DELL高分辨率显示器推荐

【IT168 资讯】高分辨率显示器的推出,让人们的视觉体验提升到了新的层次。而高分辨率显示器如今也正在从专业市场定位逐渐走进日常用户家中。目前,市面上可见到的桌面显示器的分辨率虽然已经达到5K,不过,如此之高的分辨率要普及还需时日,所以有着高分辨率需求的用户不如更…

hoj 2715 (费用流 拆点)

http://acm.hit.edu.cn/hoj/problem/view?id2715 将每个格子 i 拆成两个点 i’, i’’并加边(i’, i’’, 1, -Vi), (i’, i’’, ∞, 0), (s, i’, ∞, 0); 控制只有一次能取到宝物。 对相邻的四个格子 j, Hi > Hj 则加边(i’’, j’, ∞, 0); 若格子 i 在边界上则加边(i’…

HarmonyOS/OpenHarmony按键设备键值

按键设备键值。 作者:坚果整理,欢迎大家加入坚果组织一起学习HarmonyOS/OpenHarmony应用开发 导入模块 import {KeyCode} from @ohos.multimodalInput.keyCode;KeyCode 按键键码值。 名称值说明KEYCODE_FN0功能(Fn)键KEYCODE_UNKNOWN-1未知按键KEYCODE_HOME1功能(Home…

JD2715 忠诚2——线段树

Description 老管家是一个聪明能干的人。他为财主工作了整整10年&#xff0c;财主为了让自已账目更加清楚。要求管家每天记k次账&#xff0c;由于管家聪明能干&#xff0c;因而管家总是让财主十分满意。但是由于一些人的挑拨&#xff0c;财主还是对管家产生了怀疑。于是他决定用…

HOJ - 2715最小费用流

国庆八天乐&#xff0c;刷题也快乐。 HOJ崩了&#xff0c;但是VJ可以把题目挂出来。 题目链接&#xff1a;https://vjudge.net/contest/188441#problem/A 涉及到矩阵里面的网络流&#xff0c;化为图来做。 某个点有流量限制&#xff0c;一定要想到拆点。 求最大值的话&#xff…

HOJ 2715 Matrix3

限制增广次数的费用流。 【题目大意】 一个N*N的网格&#xff0c;每个单元都有一个价值Vi的宝物和一个高度Hi。现在ZhouGuyue要作至多K次旅行&#xff0c;每次旅行如下&#xff1a;他可以借助bin3的直升机飞到任意一个单元&#xff0c;之后他每次只能向相邻的且高度比当前所在格…

洛谷 P2715 约数和

给出a和b求a^b的约数和。 题目描述 输入输出格式 输入格式&#xff1a; 一行两个数a,b。 输出格式&#xff1a; 一个数表示结果对 9901 的模。 输入输出样例 输入样例#1&#xff1a; 2 3 输出样例#1&#xff1a; 15 说明 对于 30%的数据&#xff0c;a,b≤ 10 对于 100%的数据&a…