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. hdu1198Farm Irrigation (DFS)

    Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is ...

  2. WCF技术剖析之二十: 服务在WCF体系中是如何被描述的?

    原文:WCF技术剖析之二十: 服务在WCF体系中是如何被描述的? 任何一个程序都需要运行于一个确定的进程中,进程是一个容器,其中包含程序实例运行所需的资源.同理,一个WCF服务的监听与执行同样需要通过 ...

  3. 在chrome中使用axure生成原型的问题

    来自:非原型不设计

  4. JAVA 操作 DBF 文件数据库

    1.依赖夹包 javadbf-[].4.1.jar jconn3.jar 2.添加属性文件 jdbc.properties jdbc.driverClassName=com.sybase.jdbc3. ...

  5. Customize Spring @RequestParam Deserialization for Maps and/or Nested Objects

    @RestController class MyController { @RequestMapping(...) public void test(Container container) { .. ...

  6. 基于visual Studio2013解决C语言竞赛题之1069链表查找删除

        题目 解决代码及点评 /* 功能:建立一个链表,每个结点包括:学号.姓名.性别.年龄.输入一个年龄,如果链表中的结点所包含的年龄等于此年龄, 将此结点删除,输出最后的链表. 时 ...

  7. Swift - 使用UIImagePickerController从相册选择照片并展示

    1,UIImagePickerController介绍 (1)选择相册中的图片或者拍照,都是通过UIImagePickerController控制器实例化一个对象,然后通过self.presentVi ...

  8. python 在 eclipse 上的编码配置问题

    Eclipse的设置 window->preferences->general->editors->text editors->spelling->encoding ...

  9. Delphi安装NT服务程序时(不出现提示信息)

    如果我们不加上"/silent",那么Delphi在安装和卸载NT服务程序时候,都会出现一个提示信息,不希望出现这个提示信息,那么使用如下命令: 1,安装:“你的nt程序 /ins ...

  10. kgdb调试注意事项

    0    首先提下注意事项的背景:    kgdb和printk共用一个串口 1    设置波特率:    //最高支持460800波特率    arm-eabi-gdb  ./vmlinux    ...