Choose the best route

简介: Choose the best route

题目:

One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s

home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home

so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki

needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.


Input

There are several test cases.

Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.

Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .

Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.

Output

The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”.Sample Input

5 8 5
1 2 2
1 5 3
1 3 4
2 4 7
2 5 6
2 3 5
3 5 1
4 5 1
2
2 3
4 3 4
1 2 3
1 3 4
2 3 2
1
1

Sample Output

1
-1

解题思路:这个是最短路经的题目,首先介绍样例:第一个5是五个公交站,8代表八条路径,第二个5是所到达的终点,接下来是家附近的公交站,然后求从该点出发到达终点的最短时间。

这个按正常思路容易超时,所以可以反过来求终点到各点的距离。

程序代码:

#include<stdio.h>
#include<string.h>
int e[2000][2000],dis[50000],book[50000];
int n,m,t;
int inf=99999999;
void dij()
{
  int i,j,k,u,v,min;
  for(i=1;i<=n;i++)
    dis[i]=e[t][i];
  for(i=1;i<=n;i++)
    book[i]=0;
  book[1]=1;
  for(i=1;i<n;i++)
  {
    min=inf;
    for(j=1;j<=n;j++)
    {
      if(book[j]==0&&dis[j]<min)
      {
        min=dis[j];
        u=j;
      }
    }
    book[u]=1;
    for(v=1;v<=n;v++)
    {
      if(e[u][v]<inf)
      {
        if(dis[v]>dis[u]+e[u][v])
          dis[v]=dis[u]+e[u][v];
      }
    }
  }
}
int main()
{
  int i,j,t1,t2,t3,a,b,c,d,tem;
  while(scanf("%d%d%d",&n,&m,&t)!=EOF)
  {
    memset(dis,0,sizeof(dis));
    memset(book,0,sizeof(book));
    for(i=1;i<=n;i++)
      for(j=1;j<=n;j++)
        if(i==j)  e[i][j]=0;
        else      e[i][j]=inf;
    for(i=1;i<=m;i++)
    {
      scanf("%d%d%d",&t1,&t2,&t3);
      if(e[t1][t2]>t3)
        e[t1][t2]=t3;
    }
    for(i=1;i<n;i++)
      for(j=i+1;j<=n;j++)
      {
        tem=e[i][j];
        e[i][j]=e[j][i];
        e[j][i]=tem;
      }
    d=inf;
    dij();
//    for(i=1;i<=n;i++)
//      printf("*%d ",dis[i]);
//    printf("%d\n");
    scanf("%d",&a);
    for(i=1;i<=a;i++)
    {
      scanf("%d",&b);
    //  printf("*%d\n",dis[b]);
      if(d>dis[b])
        d=dis[b];   
    }
    if(d==inf)
      printf("-1\n");
    else
      printf("%d\n",d);
  }
  return 0;
}
相关文章
|
6月前
|
存储 人工智能 自然语言处理
以前写重复提示词花 10 分钟,现在用 Hermes 技能一条命令搞定,效率提升 300%
本文全面介绍 Hermes Agent 技能(Skills)系统,从核心概念、格式规范、安装管理到自定义开发,带你掌握可复用工作流与知识单元的使用方法,让 AI 智能体自动执行复杂任务。
988 0
|
7月前
|
人工智能 运维 并行计算
2026校招技术岗薪资大盘点:AI方向白菜价40w起,这个方向却跌破20w
本文从工程视角解析2026届技术岗薪资两极分化现象:AI方向硕士年薪40–55w,传统测试/前端等岗跌至20w以下。核心原因在于“自动化剩余”差异——岗位价值取决于人类不可替代的复杂判断与系统深度。提出四层定价漏斗模型,并给出两条可落地的升级路径:攀登高复杂度领域、转型智能体训练师。趋势已明,结构性差距将持续拉大。
|
12月前
|
Java Maven Spring
SpringBoot打包
本项目通过Maven打包为单一可执行JAR,内置配置文件与依赖,使用Spring Boot插件指定主类并repackage。执行`mvn clean package`构建后,通过`java -jar`运行(前台/后台),`kill -9 pid`停止服务。也可将JAR、依赖与配置分离部署,提升灵活性与维护性。(238字)
|
11月前
|
机器学习/深度学习 人工智能 算法
【AI大模型面试宝典七】- 训练优化篇
【AI大模型面试宝典】聚焦强化学习核心考点:从SARSA轨迹、在线/离线数据来源,到同策略与异策略差异,深入解析PPO、DPO、GRPO等主流算法原理与优化技巧,助你系统掌握RLHF、奖励模型设计及训练稳定性方案,轻松应对大模型面试高频难题,快速提升实战能力,offer拿到手软!
1188 0
|
SQL Oracle 关系型数据库
19c多租户架构下的UNDO管理- Local Undo
Oracle 12c引入多租户架构,PDB共享CDB的UNDO表空间。19c新增Local Undo特性,支持各PDB独立管理UNDO,提升性能、隔离性与可管理性,且为热克隆、PDB迁移等高级功能的前提。建议19c环境启用Local Undo。
1114 10
|
存储 消息中间件 canal
zk基础—2.架构原理和使用场景
ZooKeeper(ZK)是一个分布式协调服务,广泛应用于分布式系统中。它提供了分布式锁、元数据管理、Master选举及分布式协调等功能,适用于如Kafka、HDFS、Canal等开源分布式系统。ZK集群采用主从架构,具有顺序一致性、高性能、高可用和高并发等特点。其核心机制包括ZAB协议(保证数据一致性)、Watcher监听回调机制(实现通知功能)、以及基于临时顺序节点的分布式锁实现。ZK适合小规模集群部署,主要用于读多写少的场景。
|
人工智能 自然语言处理 网络安全
|
监控 安全 Linux
docker 命令 --cap-add NET_ADMIN 什么作用
`--cap-add NET_ADMIN` 是 Docker 容器运行时的一个参数,用于添加网络管理员权限(NET_ADMIN capability)给容器。 在 Linux 系统中,每个进程都有特定的权限来执行特定的操作。网络管理员权限是一种较高级的权限,它允许容器内的进程执行一些需要网络配置和管理的操作,例如配置网络接口、修改路由表、设置防火墙规则等。 通过添加 `--cap-add NET_ADMIN` 参数,Docker 容器将获得网络管理员权限,使其能够执行更高级的网络操作。这对于某些特定的应用场景,如网络监控、网络测试等非常有用。 需要注意的是,使用网络管理员权限可能存在安全
3881 1
|
机器学习/深度学习 人工智能 监控
基于AI视觉的泳池安全监控系统技术解析
本方案针对泳池安全管理的技术挑战,提出基于边缘计算与云平台的混合架构系统。通过改进YOLOv8模型实现92.7%溺水检测精度,结合LSTM分析异常姿态,多特征融合提升水面静止及呼吸检测能力。同时,安全员行为识别、图像增强模块有效应对环境干扰和监管盲区问题。系统采用DeepSORT优化多目标追踪,硬件配置支持实时视频流分析,实际应用中平均溺水识别时间仅2.3秒,显著优于人工检测。未来将探索多模态感知融合与自适应学习机制,进一步提升系统性能。
1117 0