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

Problem Description
There are n*m grids, each grid contains a number, ranging from 0-9. Your initial energy is zero. You can play up to K times the game, every time you can choose any one of the grid as a starting point (but not traveled before) then you can choose a grid on the
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.
 
Input
The first line is an integer T, stands for the number of the text cases.

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)
 
Output
Each case, The first you should output “Case x : ”,(x starting at 1),then output The maximum number of energy value you can get. If you can’t reach every grid in no more than K times, just output -1.
 
Sample Input
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
 
Sample Output
Case 1 : 0
Case 2 : 15
Case 3 : 16
Case 4 : 18
Case 5 : -1
 
Author
FZU
 
Source
 

#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的更多相关文章

  1. HDU 4862 Jump(更多的联合培训学校1)(最小费用最大流)

    职务地址:pid=4862">HDU4862 最小费用流做的还是太少. 建图想不出来. . . 直接引用官方题解的话吧... 最小K路径覆盖的模型.用费用流或者KM算法解决,构造二部图 ...

  2. HDU 4862 Jump(最小K路径覆盖)

    输入一个n×m网格图,每个结点的值为0-9,可以从任意点出发不超过k次,走完每个点且仅访问每个结点一次,问最终的能量最大值.不可全部走完的情况输出-1. 初始能量为0. 而结点(x,y)可以跳跃到结点 ...

  3. HDU 4862 Jump 费用流

    又是一个看了题解以后还坑了一天的题…… 结果最后发现是抄代码的时候少写了一个负号. 题意: 有一个n*m的网格,其中每个格子上都有0~9的数字.现在你可以玩K次游戏. 一次游戏是这样定义的: 你可以选 ...

  4. HDU 4862 Jump 任意起点最大权K链不相交覆盖

    你可以从任意起点开始起跳最多K次 每次跳你可以选择往右或者往下跳 从(x1,y1)跳到(x2,y2) 消耗的能量是曼哈顿距离-1 但是如果每次跳的起点和终点格子里的数字是相同的为X的话你会得到X能量 ...

  5. HDU 4862 JUMP 最小费用最大流

    2014 多校的B题,由于我不怎么搞图论,当时碰到这个题目,我怎么想都没往网络流方面弄,不过网络流真的是个好东西,对于状态多变,无法用动规或者数据结构来很好表示的时候,非常有用 这个题目要求每个点一定 ...

  6. 【HDU 2014 Multi-University Training Contest 1 1002】/【HDU 4862】Jump

    多校训练就这么华丽丽的到了 ,于是乎各种华丽丽的被虐也開始了. 这是多校的1002; 最小费用最大流. 题目大意: 有n*m个方格,每一个方格都一个的十进制一位的数.你能够操作K次. 对于每一次操作, ...

  7. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. rackup工具

    gem包rack提供了rackup工具来启动webapplication 下面是一个入门范例,使用 bundler 管理库的一个sinatra应用   在begin文件夹下有三个文件 begin.ru ...

  2. Swift - 禁用UIWebView和WKWebView的下拉拖动效果

    使用UIWebView或WKWebView加载网页时,如果页面处于最顶端时,用户用手指往下拖动,会露出灰色空背景.同样页面在最底部的时候,继续向上拖动,下方也会露出空背景. 要禁止这个拖动效果,可进行 ...

  3. Wireshark安装、简单使用、过滤器简介

    1.简介 Wireshark是一款非常著名的网络嗅探器,它的前身是Ethereal.Wireshark是一款免费的软件,只需要从官网下根据不同的系统(window,linux等)下载其对应的安装文件即 ...

  4. iOS - 单例传值 (一)

    点击打开链接    iOS - 单例传值 (二) 单例只会对某个类实例化一次/单例类,对单例这个类实例化一次有且仅有一个对象 你单例初始化,只能初始化一次,然后你指向的对象,其实都是指向一个内存地址, ...

  5. dedecms的安装以及为他配置虚拟主机

    一.概念: 1.CMS是:Content Manage System   内容管理系统 内容包括:商品,文章,软件,视频 2.当前市面上常用的CMS有哪些? DedeCMS: 织梦     中小型公司 ...

  6. SQL中on条件与where条件的区别(转载)

    数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left jion时,on和where条件的区别如下: 1. on条件是在生成临时表时使用的条 ...

  7. Endnote从头开始五:修改output style(转载)

    Endnote从头开始五:修改output style Endnote中虽然有大量的期刊格式,但是并不能囊括所有我们需要的style,所以学会自己制作或编辑已有的期刊格式是很重要的,本节内容是Endn ...

  8. AOP编程,spring实现及JDK,CGLIB实现

    什么是AOP? AOP(Aspect-OrientedProgramming,面向方面编程)和OOP(Object-Oriented Programing,面向对象编程)思想不同,两者并非对立关系,前 ...

  9. Qt之文件操作 QFile

    原地址:http://blog.csdn.net/liuhongwei123888/article/details/6084761 今天学习QT的文件操作 1.QIODevice直接继承自QObjec ...

  10. 关于Delphi中TRttiContext.FindType失效的问题

    自从Delphi2010后,Delphi中的Rtti功能得到了增强.我们终于可以不用先RegisterClass,再GetClass获取类的信息了.而只是简单的通过TRttiContext.GetTy ...