【有上下界最大费用可行流】HDU-4862 Jump

34 篇文章 0 订阅
30 篇文章 0 订阅

Jump

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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
 
————————————————————机械的分割线————————————————————
前言:耽误了一些时间,到现在才发博客。。。
思路:这道题可以抽象成有容量上下界的费用流,因为图上的数字需要在K次(上界)以内被经过1次且仅一次。
据说这道题是“最小K路径覆盖”,但是我明显没学到这个呢。只有问了Silence大牛,,感谢指教。
就当做是有容量上下界建立伴随网络。
先建立虚拟的源汇点(因为选定了一个点之后只能向下或者向右走),然后拆点(每个数字上下界1,1),每个点的出点和它能到达的点的入点相连,上下界0,1,虚拟源点连接入点,出点连接虚拟汇点,
为了控制K次,将虚拟源点拆开,边权为K,然后虚拟汇点连接虚拟源点,边权INF,
设置超级源汇点,建立伴随网络,跑一次SPFA+EK。
P.S. 一开始EK中遍历增广路的姿势不对,调了好久。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 222, M = 1e5;
struct Node {
	int u, v, w, c;
	int next;
}edge[M];
int tot, S, T, Ss, Tt, K, head[N], q[N], dis[N], pre[N], path[N];
char mat[N][M];
int n, m;
bool inq[N];

void init()
{
	tot = 0;
	memset(head, -1, sizeof(head));
}

void add(int u, int v, int w, int c)
{
	edge[tot].u = u; edge[tot].v = v;
	edge[tot].w = w; edge[tot].c = c;
	edge[tot].next = head[u]; head[u] = tot++;
	edge[tot].u = v; edge[tot].v = u;
	edge[tot].w = 0; edge[tot].c = -c;
	edge[tot].next = head[v]; head[v] = tot++;
}

bool spfa(int S, int T)
{
	memset(inq, 0, sizeof(inq));
	memset(dis, 0x3f, sizeof(dis));
	dis[S] = 0; inq[S] = true;
	int fron = 0, rear = 0;
	q[rear++] = S;
	while(fron < rear) {
		int u = q[fron%N]; fron++;
		inq[u] = false;
		for(int i = head[u]; ~i; i = edge[i].next) {
			int v = edge[i].v;
			if(edge[i].w > 0 && dis[v] > dis[u] + edge[i].c) {
				dis[v] = dis[u] + edge[i].c;
				pre[v] = u; path[v] = i;
				if(!inq[v]) {
					inq[v] = true;
					q[rear%N] = v; rear++;
				}
			}
		}
	}
	return dis[T] < INF;
}

int EK()
{
	int ret = 0, flow = 0;
	while(spfa(S, T)) {
		int mini = INF;
		for(int i = T; i != S; i = pre[i]) {
			mini = min(mini, edge[path[i]].w);
		}
		for(int i = T; i != S; i = pre[i]) {
			edge[path[i]].w -= mini;
			edge[path[i]^1].w += mini;
		}
		ret += mini * dis[T];
		flow += mini;
	}
	if(flow == n*m)
		return ret;
	return 1;
}

void build()
{
	int Sss = 2*n*m+1;
	Ss = Sss+1; Tt = Ss + 1;
	S = Tt + 1; T = S + 1;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			//拆点
			int u = (i-1) * m + j, v0 = u + n*m;
			for(int k = 1; k+j <= m; k++) {
				int v1 = (i-1) * m + j + k;
				int cost = k-1;
				if(mat[i][j] == mat[i][j+k]) cost -= mat[i][j] - '0';
				add(v0, v1, 1, cost);
			}
			for(int k = 1; k+i <= n; k++) {
				int v2 = (i+k-1) * m + j;
				int cost = k-1;
				if(mat[i][j] == mat[i+k][j]) cost -= mat[i][j] - '0';
				add(v0, v2, 1, cost);
			}
		}
	}
	add(Sss, Ss, K, 0);
	add(Tt, Sss, INF, 0);
	for(int i = 1; i <= n*m; i++) {
		add(Ss, i, 1, 0);
		add(i+n*m, Tt, 1, 0);
		add(S, i+n*m, 1, 0);
		add(i, T, 1, 0);
	}
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	int Cas;
	int kase = 1;
	scanf("%d", &Cas);
	while(Cas--) {
		scanf("%d%d%d", &n, &m, &K);
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= m; j++) {
				scanf(" %c", &mat[i][j]);
			}
		}
		init();
		build();
		int ans = EK();
		printf("Case %d : %d\n", kase++, -ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值