解题报告 之 UVA1025 A Spy in the Metro

解题报告 之 UVA1025 A Spy in the Metro


Description

Download as PDF

Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first station of Algorithms City Metro, examining the time table. The Algorithms City Metro consists of a single line with trains running both ways, so its time table is not complicated.

Maria has an appointment with a local spy at the last station of Algorithms City Metro. Maria knows that a powerful organization is after her. She also knows that while waiting at a station, she is at great risk of being caught. To hide in a running train is much safer, so she decides to stay in running trains as much as possible, even if this means traveling backward and forward. Maria needs to know a schedule with minimal waiting time at the stations that gets her to the last station in time for her appointment. You must write a program that finds the total waiting time in a best schedule for Maria.

The Algorithms City Metro system has N stations, consecutively numbered from 1 to N. Trains move in both directions: from the first station to the last station and from the last station back to the first station. The time required for a train to travel between two consecutive stations is fixed since all trains move at the same speed. Trains make a very short stop at each station, which you can ignore for simplicity. Since she is a very fast agent, Maria can always change trains at a station even if the trains involved stop in that station at the same time.

\epsfbox{p2728.eps}

Input 

The input file contains several test cases. Each test case consists of seven lines with information as follows.
Line 1.
The integer  N (  2$ \le$N$ \le$50), which is the number of stations.
Line 2.
The integer  T (  0$ \le$T$ \le$200), which is the time of the appointment.
Line 3.
N - 1 integers:  t1t2,..., tN - 1 (  1$ \le$ti$ \le$70), representing the travel times for the trains between two consecutive stations:  t1 represents the travel time between the first two stations,  t2 the time between the second and the third station, and so on.
Line 4.
The integer  M1 (  1$ \le$M1$ \le$50), representing the number of trains departing from the first station.
Line 5.
M1 integers:  d1d2,..., dM1 (  0$ \le$di$ \le$250 and  di < di + 1), representing the times at which trains depart from the first station.
Line 6.
The integer  M2 (  1$ \le$M2$ \le$50), representing the number of trains departing from the  N-th station.
Line 7.
M2 integers:  e1e2,..., eM2 (  0$ \le$ei$ \le$250 and  ei < ei + 1) representing the times at which trains depart from the  N-th station.

The last case is followed by a line containing a single zero.

Output 

For each test case, print a line containing the case number (starting with 1) and an integer representing the total waiting time in the stations for a best schedule, or the word `  impossible' in case Maria is unable to make the appointment. Use the format of the sample output.

Sample Input 

4
55
5 10 15
4
0 5 10 20
4
0 5 10 15
4
18
1 2 3
5
0 3 6 10 12
6
0 3 5 7 12 15
2
30
20
1
20
7
1 3 5 7 11 13 17
0

Sample Output 

Case Number 1: 5
Case Number 2: 0
Case Number 3: impossible


题目大意:地铁有n个车站1~n,有一个间谍0时刻 在1号车站,并且要在T时刻到n车站与某人会面。从1开往n的地铁有M1辆并且在分别在di时刻发车,从n开往1的地铁有M2辆并且分别在ei时刻发车。地铁开往下一站需要ti的时间。假设地铁在每个站停留时间忽略,但间谍可以在地铁到达时上下车。为了不被抓住,间谍需要尽量减少在车站候车的时间,那么这个间谍最少需要等待多少时间呢?

分析:作为dp模块的第一题,还是先看了一下DAG模型的,发现以前看这个dp的时候因为还没学数据结构和离散数学而导致了很多图都不懂,就理解的很浅,然后这一次再重新温习发现收货比较多。那么就针对这道题具体分析一下。

首先我们可以以时间作为自然流逝量,这样更新的时候才有一个先后顺序,那么我们以d[t][j]来表示在t时刻,侦探在第j个车站时离会面还剩下的时间。然后我们来构建状态转移方程。我们可以看到d[t][j]可能去到三种情况。一是就在那个车站等了一分钟(为什么是一分钟呢,因为在此题中是一分钟是最小的单位时间)。二是坐了1->n的地铁,三是坐了n->1的地铁。那么d[t][j]局面就可能来自之后某时间的三个局面(因为是逆序的,这里有点难想明白。肯定是先有了后面的最优情况,然后再推前面的最优情况)。

另外还有一件事情在于要先把地铁的运转情况整合成一个has_train[t][j][2]的表,表示t时刻在j地铁站是否有地铁可做(0/1表示朝向)。就可以了。

上代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#define INF 10000000
using namespace std;

int dp[2100][550];
int t[550];
int has_train[2100][550][5];

int main()
{
	int kase = 1;
	int n,T,m1,m2;
	while (cin >> n&&n)
	{	
		memset(t, 0, sizeof t);
		memset(has_train, 0, sizeof has_train);
		

		cin >> T;
		for (int i = 1; i < n; i++)
		{
			cin >> t[i];
		}

		cin >> m1;
		for (int i = 0; i < m1; i++)
		{
			int st;
			cin >> st;
			int time = st;
			for (int i = 0; i < n; i++)
			{
				time += t[i];
				if(time<=T)
				has_train[time][i][0] = 1; //新发出了一辆车,更新1->n方向哪些时间、车站有车到达
			}
		}

		cin >> m2;
		for (int i = 0; i < m2; i++)
		{
			int st;
			cin >> st;
			int time = st;
			for (int i = n - 1; i >=0; i--)
			{
				if (time <= T)
				has_train[time][i][1] = 1;  //新发出了一辆车,更新n->1方向哪些时间、车站有车到达
				time += t[i];
			}
		}

		for (int i = 0; i < n; i++)
			dp[T][i] = INF;						//已经到了T时刻,但未完成任务,等待时间初始化为无穷
		dp[T][n - 1] = 0;						//已经到了T时刻,刚好在第n个车站。算完成了任务,之后就不用等待了

		for (int i = T - 1; i >= 0;i--)
		for (int j = 0; j < n; j++)
		{
			int& ans = dp[i][j];
			ans = dp[i + 1][j] + 1;			//此时等待一分钟,那么就比dp[i+1][j]多等了一分钟(就是此时这一分钟)。
			if (j < n - 1 && has_train[i][j][0] && i + t[j+1] <= T) //此时向右走了,那么之后等待的时间就等同于到了右边下一站时刻之后等待的时间
				ans = min(ans, dp[i + t[j+1]][j + 1]);						 //比较绕,因为是逆序更新的

			if (j>0 && has_train[i][j][1] && i + t[j] <= T)				 //同理,此刻向左走了
				ans = min(ans, dp[i + t[j]][j-1]);
		}

		cout << "Case Number " << kase++ << ": ";
		if (dp[0][0] >= INF) cout << "impossible" << endl;
		else cout << dp[0][0] << endl;
	}
	return 0;
}

终于开始dp了,虽然上周的确是我请吃饭,,,但是还是很感动。。~~就是这样。Just linger out~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值