例题9-1 城市里的间谍(A Spy in the Metro, ACM/ICPC World Finals 2003, UVa1025)

思路:
1.  定义状态dp[pos][time], 表示从此状态到终点需要的等待时间。
2. 预处理出每个状态是否有向左或向右的火车。
3. 记忆化搜索的优点在于思路清晰,无需考虑递推顺序,注意仅dp[n][T] = 0。
4. 采用填表法进行递推,dp[i][j] = min(dp[i][j+1]+1, dp[i+1][j+ti], dp[i-1][j+t(i-1)])。
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <algorithm>
#define SF(a) scanf("%d", &a)
#define PF(a) printf("%d\n", a)  
#define SFF(a, b) scanf("%d%d", &a, &b)  
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SFFFF(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define LL long long
#define mod 10007
#define inf 100000007
#define eps 1e-12
using namespace std;
int buf[20] ;
int read() {
	int x = 0; char ch = getchar(); bool f = 0;
	while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}
void write(int x) {
	if (!x) { putchar(48); return; }
	int l = 0; if (x < 0) putchar('-'), x = -x;
	while (x) buf[++l] = x % 10, x = x / 10;
	while (l) putchar(buf[l--] + 48);
}
//-------------------------chc------------------------------//
const int maxt = 205;
const int maxn = 55;
int n, T;
int t[maxn] = { 0 };
bool have_r[maxn][maxt], have_l[maxn][maxt], vis[maxn][maxt];
int dp[maxn][maxt];

void pre() {
	CLEAR(have_r, 0), CLEAR(have_l, 0);
	CLEAR(dp, 0), CLEAR(vis, 0);
	int M1 = read(), s;
	FOR(i, 0, M1) {
		SF(s);
		FOR(j, 1, n) {
			if (s > T) break;
			have_l[j][s] = true;
			s += t[j];
		}
	}
	int M2 = read();
	FOR(i, 0, M2) {
		SF(s);
		for (int j = n; j > 1; --j) {
			if (s > T) break;
			have_r[j][s] = true;
			s += t[j - 1];
		}
	}
}

int solve(int i, int j) {	//	pos, time
	if (vis[i][j]) return dp[i][j];
	vis[i][j] = true;

	int &ans = dp[i][j];
	if (i == n && j == T) return ans = 0;
	else if (j >= T) return ans = inf;
	ans = inf;
	bool l = have_l[i][j], r = have_r[i][j];
	if (r) ans = min(ans, solve(i - 1, j + t[i - 1]));
	if (l) ans = min(ans, solve(i + 1, j + t[i]));
	return ans = min(solve(i, j + 1) + 1, ans);
}

void solve_() {
	dp[n][T] = 0;
	FOR(i, 1, n) dp[i][T] = inf;
	for (int j = T - 1; j >= 0; --j) {
		FOR(i, 1, n + 1) {
			int &ans = dp[i][j];
			ans = dp[i][j + 1] + 1;
			if (have_l[i][j] && j + t[i] <= T) ans = min(ans, dp[i + 1][j + t[i]]);
			if (have_r[i][j] && j + t[i - 1] <= T) ans = min(ans, dp[i - 1][j + t[i - 1]]);
		}
	}
}

void debug() {
	FOR(i, 1, n + 1) {
		FOR(j, 0, T + 1) {
			printf("%d ", dp[i][j]);
		}
		cout << endl;
	}
}

int main() {
	//IN(); OUT();
	int kase = 1;
	while (SF(n) && n) {
		SF(T);
		FOR(i, 1, n) SF(t[i]);
		pre();
		//solve(1, 0);
		solve_();
		//debug();
		int ans = dp[1][0];
		printf("Case Number %d: ", kase++);
		if (ans >= inf) puts("impossible");
		else PF(ans);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值