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. JQuery学习(3)

    创建精灵界面导航: 有以下图,合理的布局让图片正确显示: 先写导航栏html代码: <div id="navMenu"> <ul id="spriteN ...

  2. 视频播放-SurfaceView

    1.视图 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...

  3. PHP - 操作MySQL数据库

    第16章 PHP操作MySQL 学习要点: 1.PHP连接到MySQL 2.增删改查 3.其他常用函数 如果你已经具有了使用PHP.SQL和MySQL的丰富经验,现在就可以把所有这些技术组合在一起.P ...

  4. 如何在Eclipse配置Tomcat服务器

    链接地址:http://jingyan.baidu.com/article/3065b3b6efa9d7becff8a4c6.html 要想在Eclipse运行jsp文件,首先需要指定对应的服务器,即 ...

  5. PHP:根据IP地址获取所在城市

    文件目录: ipLocation -----qqwry ----------QQWry.Dat -----ipCity.class.php ipCity.class.php文件代码: <?php ...

  6. javascript笔记整理(数组)

    数组是一个可以存储一组或是一系列相关数据的容器. 一.为什么要使用数组. a.为了解决大量相关数据的存储和使用的问题. b.模拟真是的世界. 二.如何创建数组 A.通过对象的方式来创建——var a= ...

  7. 基于visual Studio2013解决C语言竞赛题之1050矩阵反斜线求和

       题目 解决代码及点评 /************************************************************************/ /* 50 ...

  8. 【图像处理】Gabor过滤器

    Gabor内核参考wiki 使用实数Real的公式计算核函数代码: Mat getGaborFilter(float lambda, float theta, float sigma2,float g ...

  9. high volume logging

    logging 是现在系统中必不可少的组件了.市面上已经有很多很多非常成熟的日志产品,log*系列就是一个典型代表.对于erlang系统来说,也有很多,比如error_logger, disk_log ...

  10. GitHub详解(转)

    GitHub 是一个共享虚拟主机服务,用于存放使用Git版本控制的软件代码和内容项目.它由GitHub公司(曾称Logical Awesome)的开发者Chris Wanstrath.PJ Hyett ...