uva 11374 Airport Express(spfa 邻接表+队列)

本文探讨了一种策略,帮助用户在有限预算下通过合理使用特定类型火车票,实现从城市Iokh前往机场的最短时间路径。通过分析不同火车线路的速度、路线和成本,我们提供了一个算法,用于确定最佳乘车方案。该方案考虑了经济型和商业型火车的差异,并通过SPFA算法计算了最优路径,以确保乘客能够最大化节省时间。

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

In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more quickly than other transports. There are two types of trains in Airport-Express, the Economy-Xpress and theCommercial-Xpress. They travel at different speeds, take different routes and have different costs.

Jason is going to the airport to meet his friend. He wants to take the Commercial-Xpress which is supposed to be faster, but he doesn't have enough money. Luckily he has a ticket for the Commercial-Xpress which can take him one station forward. If he used the ticket wisely, he might end up saving a lot of time. However, choosing the best time to use the ticket is not easy for him.

Jason now seeks your help. The routes of the two types of trains are given. Please write a program to find the best route to the destination. The program should also tell when the ticket should be used.

 

Input

The input consists of several test cases. Consecutive cases are separated by a blank line.

The first line of each case contains 3 integers, namely NS and E (2 ≤ N ≤ 500, 1 ≤ SE ≤ N), which represent the number of stations, the starting point and where the airport is located respectively.

There is an integer M (1 ≤ M ≤ 1000) representing the number of connections between the stations of the Economy-Xpress. The next Mlines give the information of the routes of the Economy-Xpress. Each consists of three integers XY and Z (XY ≤ N, 1 ≤ Z ≤ 100). This means X and Y are connected and it takes Z minutes to travel between these two stations.

The next line is another integer K (1 ≤ K ≤ 1000) representing the number of connections between the stations of the Commercial-Xpress. The next K lines contain the information of the Commercial-Xpress in the same format as that of the Economy-Xpress.

All connections are bi-directional. You may assume that there is exactly one optimal route to the airport. There might be cases where you MUST use your ticket in order to reach the airport.

 

Output

For each case, you should first list the number of stations which Jason would visit in order. On the next line, output "Ticket Not Used" if you decided NOT to use the ticket; otherwise, state the station where Jason should get on the train of Commercial-Xpress. Finally, print the total time for the journey on the last line. Consecutive sets of output must be separated by a blank line.

 

Sample Input

4 1 4
4
1 2 2
1 3 3
2 4 4
3 4 5
1
2 4 3

 

Sample Output

1 2 4
2
5
题意:第一行给出 n个地方,起点和终点;
第二行 平民航线的个数 接着下面是平民航线的起点、终点和时间;
剩下的是土豪航线的数量 接着是土豪航线的起点、终点和时间;
思路:做个假设如果用spfa对每一条土豪航线进行取最短路,那么消耗的时间会非常大,不可取。
起点和终点已知我们可以用spfa求出起点到土豪航线起点所消耗的时间,也可以求出土豪航线终点到终点的时间,最后再加上土豪航线的时间,就是最后求出的总时间。这样只要使用两次spfa就可 以求出总时间。
注意:这个道题有好多坑,看注释吧,uva不保存交过的代码,这点有点坑。
ac代码:
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<iostream>
  4 #include<queue>
  5 #include<algorithm>
  6 using namespace std;
  7 const int inf=1<<29;                                  
  8 const int maxn=1100;
  9 const int maxm=maxn*maxn;
 10 int e,head[maxn],pnt[maxm],nxt[maxm],cost[maxm],dist[maxn],pre[maxn],dist1[maxn],pre1[maxn];
 11 int n,m,a,b,d[maxm];
 12 int cnt[maxn];
 13 queue<int> q;                                           //这里没有用优先队列,因为数据不大,ac只用了25ms。
 14 bool vis[maxn];
 15 void AddEdge(int u,int v,int c)
 16 {
 17     pnt[e]=v;
 18     nxt[e]=head[u];
 19     cost[e]=c;
 20     head[u]=e++;
 21 }
 22 void Spfa(int st)                                        //计算起点每个到土豪航线的起点的时间
 23 {
 24     memset(dist,0x3f3f3f,sizeof(dist));                 // 初始化dist数组,dist为极大值。
 25     dist[st]=0;
 26     q.push(st);
 27     while(!q.empty())
 28     {
 29         int u=q.front();
 30         q.pop();
 31         vis[u]=0;
 32         for(int i=head[u]; i!=-1; i=nxt[i])
 33             if(dist[pnt[i]]>dist[u]+cost[i])
 34             {
 35                 pre[pnt[i]]=u;
 36                 dist[pnt[i]]=dist[u]+cost[i];
 37                 if(!vis[pnt[i]])
 38                 {
 39                     q.push(pnt[i]);
 40                     vis[pnt[i]]=1;
 41                 }
 42             }
 43     }
 44 }
 45 void Spfa1(int st)                                        //计算每个土豪航线的终点到终点的时间
 46 {
 47     memset(dist1,0x3f3f3f,sizeof(dist1));
 48     dist1[st]=0;
 49     q.push(st);
 50     while(!q.empty())
 51     {
 52         int u=q.front();
 53         q.pop();
 54         vis[u]=0;
 55         for(int i=head[u]; i!=-1; i=nxt[i])
 56             if(dist1[pnt[i]]>dist1[u]+cost[i])
 57             {
 58                 pre1[pnt[i]]=u;
 59                 dist1[pnt[i]]=dist1[u]+cost[i];
 60                 if(!vis[pnt[i]])
 61                 {
 62                     q.push(pnt[i]);
 63                     vis[pnt[i]]=1;
 64                 }
 65             }
 66     }
 67 }
 68 void dfs(int u)                                             //搜索起点到土豪航线的起点路径
 69 {
 70     if(u==a)
 71     {
 72         printf("%d",u);
 73         return;
 74     }
 75     dfs(pre[u]);
 76     printf(" %d",u);
 77 }
 78 void dfs1(int u)                                             //搜索土豪航线的终点到终点的路径
 79 {
 80     printf(" %d",u);
 81     if(u==b)
 82         return;
 83     dfs1(pre1[u]);
 84 }
 85 
 86 int main()
 87 {
 88     int x,c1=0;
 89     while(scanf("%d%d%d",&n,&a,&b)!=EOF)
 90     {
 91         if(c1)printf("\n");                                     //这里是个坑,不写这句话就wrong,还有最后一句话
 92         e=0;
 93         int flag=0;
 94         scanf("%d",&m);
 95         memset(head,-1,sizeof(head));
 96         memset(pre,-1,sizeof(pre));                            //储存起点到土豪航线起点路径
 97         for(int i=0; i<m; i++)
 98         {
 99             int u,v,c;
100             scanf("%d%d%d",&u,&v,&c);
101             AddEdge(u,v,c);
102             AddEdge(v,u,c);
103         }
104         Spfa(a);
105         int num=dist[b];
106         memset(pre1,-1,sizeof(pre1));                           //储存土豪航线的终点到终点的路径
107         Spfa1(b);
108         scanf("%d",&x);
109         int k=0,l=0;                                           //k是记录土豪航线的起点,l是记录土豪航线的终点
110         for(int i=0; i<x; i++)
111         {
112             int w,y,z;
113             scanf("%d%d%d",&w,&y,&z);
114             if(num>dist[w]+dist1[y]+z)
115             {
116                 num=dist[w]+dist1[y]+z;
117                 k=w;
118                 l=y;
119             }
120             if(num>dist[y]+dist1[w]+z)                         //因为是无向图,两个方向都要计算
121             {
122                 num=dist[y]+dist1[w]+z;
123                 k=y;
124                 l=w;
125             }
126         }
127         if(k==0)
128         {
129             dfs(b);
130             printf("\nTicket Not Used\n%d\n",num);
131         }
132         else
133         {
134             dfs(k);
135             dfs1(l);
136             printf("\n%d\n%d\n",k,num);
137         }
138         c1=1;                                                  //这句很重要
139     }
140 }

 

 



转载于:https://www.cnblogs.com/Xacm/p/3937463.html

内容概要:本文针对国内加密货币市场预测研究较少的现状,采用BP神经网络构建了CCi30指数预测模型。研究选取2018年3月1日至2019年3月26日共391天的数据作为样本,通过“试凑法”确定最优隐结点数目,建立三层BP神经网络模型对CCi30指数收盘价进行预测。论文详细介绍了数据预处理、模型构建、训练及评估过程,包括数据归一化、特征工程、模型架构设计(如输入层、隐藏层、输出层)、模型编译与训练、模型评估(如RMSE、MAE计算)以及结果可视化。研究表明,该模型在短期内能较准确地预测指数变化趋势。此外,文章还讨论了隐层节点数的优化方法及其对预测性能的影响,并提出了若干改进建议,如引入更多技术指标、优化模型架构、尝试其他时序模型等。 适合人群:对加密货币市场预测感兴趣的研究人员、投资者及具备一定编程基础的数据分析师。 使用场景及目标:①为加密货币市场投资者提供一种新的预测工具和方法;②帮助研究人员理解BP神经网络在时间序列预测中的应用;③为后续研究提供改进方向,如数据增强、模型优化、特征工程等。 其他说明:尽管该模型在短期内表现出良好的预测性能,但仍存在一定局限性,如样本量较小、未考虑外部因素影响等。因此,在实际应用中需谨慎对待模型预测结果,并结合其他分析工具共同决策。
内容概要:该论文针对新型电力系统中非线性负荷和分布式电源接入导致的电能质量扰动识别难题,提出了一种结合优化广义S变换(OGST)和混合输入神经网络的方法。OGST通过自适应选取高斯窗函数参数保留扰动信号的幅值和频率特征;混合输入神经网络分别处理原始时间序列和时频矩阵,融合两种特征后识别扰动类型。实验表明,该方法在26种扰动类型的仿真数据上识别准确率达99.77%,在实际电网信号上达到92.5%,优于传统单一输入神经网络。论文还提供了详细的代码实现,包括优化广义S变换、电能质量扰动信号生成、混合输入神经网络模型构建及训练流程。 适合人群:具备一定编程基础,特别是对电能质量监测、信号处理和深度学习感兴趣的工程师和研究人员。 使用场景及目标:①适用于电力系统中的电能质量监测,特别是智能变电站和新能源电站的并网检测;②通过OGST和混合输入神经网络,提高对复杂电能质量扰动的识别准确率和鲁棒性;③支持实时处理和边缘计算部署,满足工业场景的实时性和资源限制要求。 其他说明:该方法不仅在理论上创新,而且在实际应用中表现出色。通过参数优化和混合输入架构,能够有效应对不同类型的电能质量扰动。此外,论文还提供了完整的代码实现和实验验证,便于读者复现实验结果并应用于实际项目中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值