UVa 1025 A Spy in the Metro

本文深入解析AI技术在音视频处理领域的应用实例与面临的挑战,包括视频分割、语义识别、自动驾驶等关键议题,旨在提供全面的洞察与思考。
部署运行你感兴趣的模型镜像

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: t1represents 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

#include <cstdio>
#include <cstring>
int inf = 1<<30;

// 站台个数
int N;

// 约定见面时间
int T;

// t[i]代表从站台i到站台i+1的时间
int t[100];

// 从站台1开出的火车个数
int M1;

// 从站台1开出火车的出发时间
int d[100];

// 从最后一个站台开出的火车个数
int M2;

// 从最后一个站台开出火车的出发时间
int e[100];

// 记录数据
// record[i][j]代表从j时刻在站台i到最终会面时间的最少等待时间
int record[60][210];

// left_time[i]代表从站台1到达站台i的时间
int left_time[60];

// right_time[i]代表从最后一个站台到达站台i的时间
int right_time[60];

int get_min(int x, int this_t);

int main()
{
	int count = 1;
	while(scanf("%d", &N) == 1 && N != 0)
	{
		// 读入数据
		scanf("%d", &T);
		left_time[1] = 0;
		right_time[N] = 0;
		for(int i = 1; i <= N-1; i++)
		{
			scanf("%d", &t[i]);		
			left_time[i+1] = left_time[i] + t[i];	
		}
		for(int i = N-1; i >= 1; i--)
			right_time[i] = right_time[i+1] + t[i];
	
		scanf("%d", &M1);
		for(int i = 1; i <= M1; i++)
			scanf("%d", &d[i]);

		scanf("%d", &M2);
		for(int i = 1; i <= M2; i++)
			scanf("%d", &e[i]);
		// 初始化
		memset(record, -1, sizeof(record));					


/*		printf("left: ");
		for(int i = 1; i <= N; i++)
			printf("(%d:%d) ", i, left_time[i]);
		printf("\nright: ");
		for(int i = 1; i <= N; i++)
                        printf("(%d:%d) ", i, right_time[i]);
		printf("\n");
*/		// 计算结果
		int result = get_min(1, 0);			
	
		printf("Case Number %d: ", count);
		count++;
		if(result == inf)	
			printf("impossible\n");
		else
			printf("%d\n", result);	
			
	}		
	return 0;
}

// 计算结果
// 代表计算在站台x,现在时刻为this_t到达最终站台所用最少等待时间
// 如果不可能,返回inf
int get_min(int x, int this_t)
{
//	printf("here: %d, time: %d\n", x, this_t);
	if(record[x][this_t] != -1)
		return record[x][this_t];

	record[x][this_t] = inf;


	if(x == N)
	{
		record[x][this_t] = T-this_t;
	}
	// 查看从左到右开的所有火车
	for(int i = 1; i <= M1; i++)
	{
		if(x == N)
			break;
		if(d[i]+left_time[x] >= this_t && d[i]+left_time[x+1] <= T)
		{
//			printf("\tnow: %d, time: %d\n", x+1, d[i]+left_time[x+1]);
			int t_result = get_min(x+1, d[i]+left_time[x+1]);
			if(t_result != inf)
			{
				int t_time = t_result + (d[i]+left_time[x]-this_t);
				if(t_time < record[x][this_t])
					record[x][this_t] = t_time;
			}
		}	
	}

	// 查看从右到左开的所有火车
	for(int i = 1; i <= M2; i++)
	{
		if(x == 1)
			break;
		if(e[i]+right_time[x] >= this_t && e[i]+right_time[x-1] <= T)
                {       

//			printf("\tnow: %d, time: %d\n", x-1, e[i]+right_time[x-1]); 
                        int t_result = get_min(x-1, e[i]+right_time[x-1]);
                        if(t_result != inf)
                        {
                                int t_time = t_result + (e[i]+right_time[x]-this_t);
                                if(t_time < record[x][this_t])
                                        record[x][this_t] = t_time;
                        }
                }
	}	

	return record[x][this_t];	
}


做的第一道动态规划题目,纪念一下。
首先读完题最好想一想能不能贪心,如果不能的话举出反例,然后再想动态规划。
状态是record(i, j)代表j时刻在站台i到最终时刻所用最少等待时间。
用记忆化搜索完成,不可能的状态值设为(1 << 30).

您可能感兴趣的与本文相关的镜像

Qwen-Image

Qwen-Image

图片生成
Qwen

Qwen-Image是阿里云通义千问团队于2025年8月发布的亿参数图像生成基础模型,其最大亮点是强大的复杂文本渲染和精确图像编辑能力,能够生成包含多行、段落级中英文文本的高保真图像

## 软件功能详细介绍 1. **文本片段管理**:可以添加、编辑、删除常用文本片段,方便快速调用 2. **分组管理**:支持创建多个分组,不同类型的文本片段可以分类存储 3. **热键绑定**:为每个文本片段绑定自定义热键,实现一键粘贴 4. **窗口置顶**:支持窗口置顶功能,方便在其他应用程序上直接使用 5. **自动隐藏**:可以设置自动隐藏,减少桌面占用空间 6. **数据持久化**:所有配置和文本片段会自动保存,下次启动时自动加载 ## 软件使用技巧说明 1. **快速添加文本**:在文本输入框中输入内容后,点击"添加内容"按钮即可快速添加 2. **批量管理**:可以同时编辑多个文本片段,提高管理效率 3. **热键冲突处理**:如果设置的热键与系统或其他软件冲突,会自动提示 4. **分组切换**:使用分组按钮可以快速切换不同类别的文本片段 5. **文本格式化**:支持在文本片段中使用换行符和制表符等格式 ## 软件操作方法指南 1. **启动软件**:双击"大飞哥软件自习室——快捷粘贴工具.exe"文件即可启动 2. **添加文本片段**: - 在主界面的文本输入框中输入要保存的内容 - 点击"添加内容"按钮 - 在弹出的对话框中设置热键和分组 - 点击"确定"保存 3. **使用热键粘贴**: - 确保软件处于运行状态 - 在需要粘贴的位置按下设置的热键 - 文本片段会自动粘贴到当前位置 4. **编辑文本片段**: - 选中要编辑的文本片段 - 点击"编辑"按钮 - 修改内容或热键设置 - 点击"确定"保存修改 5. **删除文本片段**: - 选中要删除的文本片段 - 点击"删除"按钮 - 在确认对话框中点击"确定"即可删除
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值