1111 Online Map-PAT甲级

本文探讨了在给定地图上寻找从起点到终点的最短路径和最快路径的算法实现。通过使用邻接矩阵表示街道信息,结合Dijkstra算法和深度优先搜索(DFS),有效地解决了路径推荐问题。代码示例展示了如何处理输入数据,执行算法,并输出路径详情。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2≤N≤500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

V1 V2 one-way length time

where V1 and V2 are the indices (from 0 to N−1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance D in the format:

Distance = D: source -> v1 -> ... -> destination

Then in the next line print the fastest path with total time T:

Time = T: source -> w1 -> ... -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:

10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5

Sample Output 1:

Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5

Sample Input 2:

7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5

Sample Output 2:

Distance = 3; Time = 4: 3 -> 2 -> 5

考察dijkstra与dfs,参考dalao的代码如下:

满分代码如下:

#include<bits/stdc++.h>
using namespace std;
int N,M,source,des,shortest[2];
//source表示起点,des表示终点
//shortest存储最短路径的长度(下标为0)和最快路径的时间(下标为1)
int graph[505][505][2];
//图的邻接矩阵,下标为0的邻接矩阵表示长度,下标为1的邻接矩阵表示时间
int dis[505],past[505],num[505];
//存储到各点的最短距离,各点的父节点
//还有一个辅助数组num,求最短路径时存储时间,求最快路径时存储结点的个数
bool visit[505];//是否已经访问过
vector<int>path[2];
//下标为0时存储最短路径,下标为1时存储最快路径
void dijkstra(int index){
	//index=0时表示求最短路径,index=1表示求的是最快路径
	while(!visit[des]){
		//还没有访问到终点
		int v=-1,MIN=INT_MAX;
		for(int i=0;i<N;i++){
			//求出目前距离访问最短的未访问的结点
			if(!visit[i]&&MIN>dis[i]){
				MIN=dis[i];
				v=i;
			} 
		} 
		if(v==-1) return;
		visit[v]=true;
		for(int i=0;i<N;i++){
			//遍历v能访问到的结点
			if(!visit[i]&&graph[v][i][index]!=0&&dis[i]>dis[v]+graph[v][i][index]){
				dis[i]=dis[v]+graph[v][i][index];
				past[i]=v;//更新父节点
				if(index==0){
					//求最短路径
					num[i]=num[v]+graph[v][i][1];//更新时间 
				}else if(index==1){
					num[i]=num[v]+1;//更新结点上的个数 
				}
			}else if(graph[v][i][index]!=0&&dis[i]==dis[v]+graph[v][i][index]){
				//距离相等
				if(index==0&&num[i]>num[v]+graph[v][i][1]){
					//时间更短
					past[i]=v;
					num[i]=num[v]+graph[v][i][1]; 
				}else if(graph[v][i][index]!=0&&index==1&&num[i]>num[v]+1){
					past[i]=v;
					num[i]=num[v]+1;
				}
			}
		}
		
	} 
	shortest[index]=dis[des];
} 
void dfs(int v,int index){
	//index=0求最短路径,=1求最快路径
	if(v==source){
		path[index].push_back(v);
		return;
	} 
	dfs(past[v],index);
	path[index].push_back(v);
}
bool cmp(){
	//比较两个路径是否完全一致
	if(path[0].size()!=path[1].size())
		return false;
	for(int i=0;i<path[0].size();i++){
		if(path[0][i]!=path[1][i])
			return false;
	} 
	return true;
}
void output(int index){
	//输出路径
	for(int i=0;i<path[index].size();i++){
		printf("%s%d",i>0?" -> ":"",path[index][i]);
	} 
	printf("\n");
}
int main(){
	scanf("%d%d",&N,&M);
	while(M--){
		int a,b,c,d,e;
		scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
		graph[a][b][0]=d;
		graph[a][b][1]=e;
		if(c==0){
			graph[b][a][0]=d;
			graph[b][a][1]=e;
		}
	}
	scanf("%d%d",&source,&des);
	for(int i=0;i<2;i++){
		//两次用dijkstra+DFS算法
		fill(visit,visit+N,false);
		fill(dis,dis+N,INT_MAX);
		fill(num,num+N,INT_MAX);
		dis[source]=0;
		num[source]=0;
		dijkstra(i);
		dfs(des,i); 
	}
	if(cmp()){
		printf("Distance = %d; Time = %d: ",shortest[0],shortest[1]);
		output(0);
	}else{
		printf("Distance = %d: ",shortest[0]);
		output(0);
		printf("Time = %d: ",shortest[1]);
		output(1);
	}
	return 0;
}

 

内容概要:本文档详细介绍了Analog Devices公司生产的AD8436真均方根-直流(RMS-to-DC)转换器的技术细节及其应用场景。AD8436由三个独立模块构成:轨到轨FET输入放大器、高动态范围均方根计算内核和精密轨到轨输出放大器。该器件不仅体积小巧、功耗低,而且具有广泛的输入电压范围和快速响应特性。文档涵盖了AD8436的工作原理、配置选项、外部组件选择(如电容)、增益调节、单电源供电、电流互感器配置、接地故障检测、三相电源监测等方面的内容。此外,还特别强调了PCB设计注意事项和误差源分析,旨在帮助工程师更好地理解和应用这款高性能的RMS-DC转换器。 适合人群:从事模拟电路设计的专业工程师和技术人员,尤其是那些需要精确测量交流电信号均方根值的应用开发者。 使用场景及目标:①用于工业自动化、医疗设备、电力监控等领域,实现对交流电压或电流的精准测量;②适用于手持式数字万用表及其他便携式仪器仪表,提供高效的单电源解决方案;③在电流互感器配置中,用于检测微小的电流变化,保障电气安全;④应用于三相电力系统监控,优化建立时间和转换精度。 其他说明:为了确保最佳性能,文档推荐使用高质量的电容器件,并给出了详细的PCB布局指导。同时提醒用户关注电介质吸收和泄漏电流等因素对测量准确性的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值