HDU4862 Jump

Jump


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


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


题意:给一张n行m列的图,可以在图选择k个起点,要求把这张图遍历玩,每个点只能向右向下跳,跳一次消耗的能量是|x1-x2|+|y1-y2|-1,如果两个点的值相同能量就要加上这个值,求遍历完的最大能量。

分析:比赛的时候这题根本没看......后面看题解就知道可以用费用流,给出一个起点和一个终点,然后图分成两部分,第一部分是在1-n*m里面,第二部分是n*m+1-2*n*m里面,第一部分接起点,流量1,费用0,第二部分接终点流量1,费用0,然后把每个点向下向右能到达的点节起来(每个点的第一部分接到他能到达点的第二部分),流量1,费用就是消耗的能量(是消耗的能量,刚开始这里弄错了),最后再弄个点接到起点流量是k,费用0,然后这个点向第二部分的点都接上。图就建完了,跑一边最小费用最大流就是。。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <algorithm>
#include <stdlib.h>
using namespace std;
#define N 220
#define M 12345
#define INF (1<<29)
struct Edge{
    int from, to, flow, cap, nex, cost;
}edge[M*2];
int n, m, k;
int head[N],edgenum;
int nv;
void add(int u,int v,int cap,int cost){
    Edge E={u, v, 0, cap, head[u], cost};
    edge[edgenum]=E;
    head[u]=edgenum++;
    Edge E2={v, u, 0, 0, head[v], -cost};
    edge[edgenum]=E2;
    head[v]=edgenum++;
}
int dist[N], pre[N], pe[N];
bool vis[N];
queue<int>q;
bool spfa(int s,int t){
	while(!q.empty()) q.pop();
	memset(vis,0,sizeof(vis));
	memset(pre,-1,sizeof(pre));
	for(int i=1;i<=nv;i++) dist[i]=INF;
	dist[s]=0;
	vis[s]=1;
	q.push(s);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=head[u];i!=-1;i=edge[i].nex){
			int v=edge[i].to;
			if(edge[i].cap&&dist[v]>dist[u]+edge[i].cost){
				dist[v]=dist[u]+edge[i].cost;
				pre[v]=u; pe[v]=i;
				if(vis[v]==0){
					vis[v]=1;
					q.push(v);
				}
			}
		}
	}
	if(pre[t]==-1) return false;
	return true;
}
int MCMF(int s,int t,int need=0){
	int max_flow=0;
	int min_cost=0;
	while(spfa(s,t)){
		int aug=INF;
		for(int v=t;v!=s;v=pre[v]){
			aug=min(aug,edge[pe[v]].cap);
		}
		max_flow+=aug;
		min_cost+=dist[t]*aug;
		for(int v=t;v!=s;v=pre[v]){
			edge[pe[v]].cap-=aug;
			edge[pe[v]^1].cap+=aug;
		}
	}
	if(max_flow!=n*m) return -1;
	return min_cost;
}
void init()
{
    memset(head,-1,sizeof head); edgenum = 0;
}
int Hash1(int x, int y)
{
    return (x-1)*m+y;
}
int Hash2(int x, int y)
{
    return n*m +(x-1)*m+y;
}
char s[20];
int mp[20][20];
int main()
{
    int T, i, j, g, Cas = 1; scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&k);
        for(i=1;i<=n;i++)
        {
            scanf("%s",s+1);
            for(j = 1; j<=m; j++)
                mp[i][j]=s[j]-'0';
        }
        printf("Case %d : ",Cas++);
        init();
        int i,j,g;
        int from=0,to=Hash2(n,m)+10,jiji=to-2;
        nv=to+2;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                add(from,Hash1(i,j),1,0);
                add(Hash2(i,j),to,1,0);
                add(jiji,Hash2(i,j),1,0);
                for(g=j+1;g<=m;g++)
                {
                    int cos=g-j-1;
                    if(mp[i][j]==mp[i][g])
                        cos-=mp[i][j];
                    add(Hash1(i,j),Hash2(i,g),1,cos);
                }
                for(g=i+1;g <= n;g++)
                {
                    int cos=g-i-1;
                    if(mp[i][j]==mp[g][j])
                        cos-=mp[i][j];
                    add(Hash1(i,j),Hash2(g, j),1,cos);
                }
            }
        }
        add(from,jiji,k,0);
        int ans=MCMF(from,to);
        if(ans==-1)
            printf("-1\n");
        else
            printf("%d\n",-ans);
        }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值