HDOJ 4862 Jump
K路径覆盖问题,最小费用最大流。。。。
最小K路径覆盖的模型,用费用流或者KM算法解决,构造二部图,X部有N*M个节点,源点向X部每一个节点连一条边,流量1,费用0,Y部有N*M个节点,每一个节点向汇点连一条边,流量1,费用0,假设X部的节点x能够在一步之内到达Y部的节点y,那么就连边x->y,费用为从x格子到y格子的花费能量减去得到的能量,流量1,再在X部添加一个新的节点,表示能够从随意节点出发K次,源点向其连边,费用0,流量K,这个点向Y部每一个点连边,费用0,流量1,最这个图跑最小费用最大流,假设满流就是存在解,反之不存在,最小费用的相反数就是能够获得的最大能量
Jump
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 546 Accepted Submission(s): 238
right or below the current grid to jump, but it has not traveled before. Every time you can jump as many times as you want, as long as you do not violate rules. If you are from (x1, y1) to (x2, y2), then you consume |x1-x2|+|y1-y2|-1 energies. Energy can be
negative.
However, in a jump, if you start position and end position has same numbers S, then you can increase the energy value by S.
Give me the maximum energy you can get. Notice that you have to go each grid exactly once and you don’t have to play exactly K times.
Then T cases followed and each case begin with three numbers N, M and K. Means there are N rows and M columns, you have K times to play.
Then N lines follow, each line is a string which is made up by M numbers.
The grids only contain numbers from 0 to 9.
(T<=100, N<=10,M<=10,K<=100)
5 1 5 1 91929 1 5 2 91929 1 5 3 91929 3 3 3 333 333 333 3 3 2 333 333 333
Case 1 : 0 Case 2 : 15 Case 3 : 16 Case 4 : 18 Case 5 : -1
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <queue>
- using namespace std;
- const int maxn=110000;
- const int INF=0x3f3f3f3f;
- int n,m,k;
- int a[20][20];
- struct Edge
- {
- int to,next,cap,flow,cost;
- }edge[maxn];
- int Adj[maxn],Size,N;
- void init()
- {
- memset(Adj,-1,sizeof(Adj)); Size=0;
- }
- void addedge(int u,int v,int cap,int cost)
- {
- edge[Size].to=v;
- edge[Size].next=Adj[u];
- edge[Size].cost=cost;
- edge[Size].cap=cap;
- edge[Size].flow=0;
- Adj[u]=Size++;
- }
- void Add_Edge(int u,int v,int cap,int cost)
- {
- addedge(u,v,cap,cost);
- addedge(v,u,0,-cost);
- }
- int dist[1000],vis[1000],pre[1000];
- bool spfa(int s,int t)
- {
- queue<int> q;
- for(int i=0;i<N;i++)
- {
- dist[i]=INF;vis[i]=false; pre[i]=-1;
- }
- dist[s]=0; vis[s]=true; q.push(s);
- while(!q.empty())
- {
- int u=q.front();
- q.pop();
- vis[u]=false;
- for(int i=Adj[u];~i;i=edge[i].next)
- {
- int v=edge[i].to;
- if(edge[i].cap>edge[i].flow&&
- dist[v]>dist[u]+edge[i].cost)
- {
- dist[v]=dist[u]+edge[i].cost;
- pre[v]=i;
- if(!vis[v])
- {
- vis[v]=true;
- q.push(v);
- }
- }
- }
- }
- if(pre[t]==-1) return false;
- return true;
- }
- int MinCostMaxFlow(int s,int t,int& cost)
- {
- int flow=0;
- cost=0;
- while(spfa(s,t))
- {
- int Min=INF;
- for(int i=pre[t];~i;i=pre[edge[i^1].to])
- {
- if(Min>edge[i].cap-edge[i].flow)
- Min=edge[i].cap-edge[i].flow;
- }
- for(int i=pre[t];~i;i=pre[edge[i^1].to])
- {
- edge[i].flow+=Min;
- edge[i^1].flow-=Min;
- cost+=edge[i].cost*Min;
- }
- flow+=Min;
- }
- return flow;
- }
- char in[10010];
- int main()
- {
- int T_T,cas=1;
- scanf("%d",&T_T);
- while(T_T--)
- {
- init();
- scanf("%d%d%d",&n,&m,&k);
- memset(a,0,sizeof(a));
- for(int i=0;i<n;i++)
- {
- scanf("%s",in);
- for(int j=0;j<m;j++)
- {
- a[i][j]=in[j]-'0';
- }
- }
- ///source:2*n*m sink:2*n*m+1 mid:2*n*m+2;
- int source=2*n*m,sink=2*n*m+1,mid=2*n*m+2;
- N=mid+1;
- Add_Edge(source,mid,k,0);
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<m;j++)
- {
- int from=i*m+j;
- Add_Edge(source,from,1,0);
- Add_Edge(from+n*m,sink,1,0);
- Add_Edge(mid,from+n*m,1,0);
- for(int ii=i+1;ii<n;ii++)
- {
- int to=ii*m+j+n*m;
- int cost=0;
- if(a[i][j]==a[ii][j])
- cost=a[i][j];
- cost-=ii-i-1;
- Add_Edge(from,to,1,-cost);
- }
- for(int jj=j+1;jj<m;jj++)
- {
- int to=i*m+jj+n*m;
- int cost=0;
- if(a[i][j]==a[i][jj])
- cost=a[i][j];
- cost-=jj-j-1;
- Add_Edge(from,to,1,-cost);
- }
- }
- }
- int C,F;
- F=MinCostMaxFlow(source,sink,C);
- C=-C;
- if(F!=n*m) C=-1;
- printf("Case %d : %d\n",cas++,C);
- }
- return 0;
- }
HDOJ 4862 Jump的更多相关文章
- HDU 4862 Jump(更多的联合培训学校1)(最小费用最大流)
职务地址:pid=4862">HDU4862 最小费用流做的还是太少. 建图想不出来. . . 直接引用官方题解的话吧... 最小K路径覆盖的模型.用费用流或者KM算法解决,构造二部图 ...
- HDU 4862 Jump(最小K路径覆盖)
输入一个n×m网格图,每个结点的值为0-9,可以从任意点出发不超过k次,走完每个点且仅访问每个结点一次,问最终的能量最大值.不可全部走完的情况输出-1. 初始能量为0. 而结点(x,y)可以跳跃到结点 ...
- HDU 4862 Jump 费用流
又是一个看了题解以后还坑了一天的题…… 结果最后发现是抄代码的时候少写了一个负号. 题意: 有一个n*m的网格,其中每个格子上都有0~9的数字.现在你可以玩K次游戏. 一次游戏是这样定义的: 你可以选 ...
- HDU 4862 Jump 任意起点最大权K链不相交覆盖
你可以从任意起点开始起跳最多K次 每次跳你可以选择往右或者往下跳 从(x1,y1)跳到(x2,y2) 消耗的能量是曼哈顿距离-1 但是如果每次跳的起点和终点格子里的数字是相同的为X的话你会得到X能量 ...
- HDU 4862 JUMP 最小费用最大流
2014 多校的B题,由于我不怎么搞图论,当时碰到这个题目,我怎么想都没往网络流方面弄,不过网络流真的是个好东西,对于状态多变,无法用动规或者数据结构来很好表示的时候,非常有用 这个题目要求每个点一定 ...
- 【HDU 2014 Multi-University Training Contest 1 1002】/【HDU 4862】Jump
多校训练就这么华丽丽的到了 ,于是乎各种华丽丽的被虐也開始了. 这是多校的1002; 最小费用最大流. 题目大意: 有n*m个方格,每一个方格都一个的十进制一位的数.你能够操作K次. 对于每一次操作, ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
随机推荐
- ofstream 使用的一点主意事项
有如下代码段: ofstream ofs; while(...) { ofs.close(); ofs.open(...) ofs << "内容"; ... } ofs ...
- C++ strcpy strcpy_s strncpy strlcpy
strncpy的用法:它与strcpy的不同之处就在于复制n个字符,而不是把所有字符拷贝(包括结尾'\0'). 函数原型:char * strncpy(char *dst,const char * s ...
- const对象默认是static的,而不是extern的
const 和 static 变量,可以放在头文件中 const对象默认是static的,而不是extern的,所以即使放在头文件中声明和定义.多个cpp引用同一个头文件,互相也没有感知,所以不会导致 ...
- Ubuntu 挂载ISO文件的方法
1.在终端中输入:sudo mkdir /media/iso 在/media下生成一个iso文件夹用来挂载iso文件2.然后输入:sudo mount -o loop /home/X/X/XXXX.i ...
- windows live writer插件说明文档(附录网盘地址)
百度云地址:http://pan.baidu.com/s/1hqnjzjY 1.Screen Capture tool 用于直接在WLWriter中进行截图的一个插件,要配合SnagIt 这个软件使用 ...
- 1352 - Colored Cubes (枚举方法)
There are several colored cubes. All of them are of the same size but they may be colored differentl ...
- 几种经典的数据排序及其Java实现
选择排序 思想 n个记录的文件的直接选择排序可经过n-1趟直接选择排序得到有序结果: ①初始状态:无序区为R[1..n],有序区为空. ②第1趟排序 在无序区R[1..n]中选出关键字最小的记录R[k ...
- CSS中position详解与常见应用实现
在web前台开发时候,我们必不可少的会用到postion属性进行布局定位.今天总结了一下position知识点,与常用功能与大家分享,欢迎大家交流指正. 首先我们对postion属性进行详解. 在CS ...
- Tesseract Ocr引擎
Tesseract Ocr引擎 1.Tesseract介绍 tesseract 是一个google支持的开源ocr项目,其项目地址:https://github.com/tesseract-ocr/t ...
- Javascript 进阶 封装
js中处处是对象,面向对象的第一步当然就是封装了,由于Js中没有类的概念,所以封装起来也比较麻烦,下面介绍两种js的封装. 1.使用约定优先的原则,将所有的私有变量以_开头 <script ty ...