hdu 4862 jump 多校第一场第二题

Jump

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 545    Accepted Submission(s): 237


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
 
意思是有k次机会走,一共要把所有点走完,每次只能走右边或者是下边,可以跳几格走,每次走所需要耗费的体力为两点坐标距离减1,如果起点和终点符号相同,则获得那么多能量。输出最后最大的能量。
最小费用最大流,建图如下

最小K路径覆盖的模型,用费用流或者KM算法解决,构造二部图,X部有N*M个节点,源点向X部每个节点连一条边,流量1,费用0Y部有N*M个节点,每个节点向汇点连一条边,流量1,费用0,如果X部的节点x可以在一步之内到达Y部的节点y,那么就连边x->y,费用为从x格子到y格子的花费能量减去得到的能量,流量1,再在X部增加一个新的节点,表示可以从任意节点出发K次,源点向其连边,费用0,流量K,这个点向Y部每个点连边,费用0,流量1,最这个图跑最小费用最大流,如果满流就是存在解,反之不存在,最小费用的相反数就是可以获得的最大能量

网络流还是做得太少,联想不到。

/***********************************************\
 |Author: Messyidea
 |Created Time: 2014-7-25 12:45:18
 |File Name: b.cpp
 |Description: 
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3fffffff ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;
#define maxn 1005
#define maxm 30005
using namespace std;
struct Edge{
    int u,v,cap,cost,x;
}E[maxm];
int e,l[maxn];
inline void init(){
    e = 0;
    memset(l,-1,sizeof(l));
}
inline void insert(int u,int v,int cap,int cost){
    E[e].u = u;E[e].v = v;E[e].cap = cap;E[e].cost = cost;E[e].x = l[u];l[u] = e++;
    E[e].u = v;E[e].v = u;E[e].cap = 0;E[e].cost = -cost;E[e].x = l[v];l[v] = e++;
}
int q[20*maxn],s,t,inq[maxn],dis[maxn],eid[maxn];
void min_cost_max_flow(int src,int sink,int &cap,int &cost){
    cost = cap = 0;
    while(true){
        for(int i=0;i<maxn;++i) dis[i] = inf;
        s = t = 0;
        q[t++] = src;
        inq[src] = 1;
        dis[src] = 0;
        while(s < t){
            int u = q[s ++];inq[u] = 0;
            for(int p = l[u];p>=0;p = E[p].x){
                if(E[p].cap <= 0) continue;
                int v = E[p].v;
                if(dis[v] > dis[u] + E[p].cost){
                    dis[v] = dis[u] + E[p].cost;
                    eid[v] = p;
                    if(inq[v] == 0){
                        inq[v] = 1;q[t++] = v;
                    }
                }
            }
        }
        if(dis[sink] >= inf) return ;
        int c = inf;
        for(int i = sink;i!=src;i=E[eid[i]].u) c = min(c,E[eid[i]].cap);
        cost  += dis[sink]*c;cap += c;
        for(int i=sink;i!=src;i = E[eid[i]].u){
            int p = eid[i];E[p].cap -= c;E[p^1].cap += c;
        }
    }
}
char ss[20][20];
int main() {
	//freopen("input.txt","r",stdin); 
    int T;
    scanf("%d",&T);
    int n,m,k,cost,cap,sum;
    int cas = 1;
    while(T--){
        scanf("%d%d%d",&n,&m,&k);
        init();
        for(int i=0;i<n;++i) scanf("%s",ss[i]);
        int p = n*m;
        int _s = p*2+1;
        int _t = _s+1;
        int kk = _t+1;
        insert(_s,kk,k,0);
        rep(i,n){
            rep(j,m){
                insert(_s,m*i+j,1,0);
                insert(m*i+j+p,_t,1,0);
                insert(kk,m*i+j+p,1,0);
            }
        }
        rep(i,n){
            rep(j,m){
                for(int x=1;j+x<m;++x){
                    insert(i*m+j,p+i*m+j+x,1,x-1-(ss[i][j] == ss[i][j+x]?ss[i][j]-'0':0));
                }
                for(int x=1;i+x<n;++x){
                    insert(i*m+j,p+(i+x)*m+j,1,x-1-(ss[i][j] == ss[i+x][j]?ss[i][j]-'0':0));
                }
            }
        }
        min_cost_max_flow(_s,_t,cap,cost);
        if(cap == n*m){
            printf("Case %d : %d\n",cas++,-cost);
        }else {
            printf("Case %d : %d\n",cas++,-1);
        }
    }
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值