HDU-2680,Choose the best route(Dijkstra)

简介: HDU-2680,Choose the best route(Dijkstra)

Problem Description:


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


程序代码:


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f
int n,m,s;
int map[1001][1001],book[1001],dis[1001];
void Dijkstra(int v)
{
  int i,j,k,minn,u;
  memset(book,0,sizeof(book));
  book[v]=1;
  for(i=0;i<=n;i++)
    dis[i]=map[v][i];
  for(i=0;i<n;i++)
  {
    minn=INF;
    for(j=0;j<=n;j++)
    {
      if(book[j]==0&&dis[j]<minn)
      {
        u=j;
        minn=dis[j];
      }
    }
    book[u]=1;
    for(k=0;k<=n;k++)
    {
      if(map[u][k]<INF)
      {
        if(dis[k]>dis[u]+map[u][k])
          dis[k]=dis[u]+map[u][k];
      }
    }
  }
}
int main()
{
  int i,j,x,y,t,a,b;
  while(~scanf("%d %d %d",&n,&m,&s))
  {
    for(i=0;i<=n;i++)
    {
      for(j=0;j<=n;j++)
      {
        if(i==j)
          map[i][j]=0;
        else
          map[i][j]=INF;
      }
    }
    while(m--)
    {
      scanf("%d %d %d",&x,&y,&t);
      if(map[x][y]>t)
        map[x][y]=t;
    }
    scanf("%d",&a);
    while(a--)
    {
      scanf("%d",&b);
      map[0][b]=map[b][0]=0;
    }
    Dijkstra(0);
    if(dis[s]==INF)
      printf("-1\n");
    else
      printf("%d\n",dis[s]);
  }
  return 0;
}

 


相关文章
|
移动开发 小程序
IRS应用发布系统基本概念
服务侧负责将应用发布至浙里办APP和政务服务网,应用发布类型不同,应用发布流程也不同:
|
XML 数据格式
3MF/GLTF格式在线转换
3D模型在线转换是一个可以进行3D模型格式转换的在线工具,支持多种3D模型格式进行在线预览和互相转换。
610 0
3MF/GLTF格式在线转换
|
8月前
|
安全 API
鸿蒙开发:实现AOP代码插桩能力
正确的运用AOP,可以提升代码的模块化、复用性、可维护性和灵活性,同时降低了耦合度,使系统更易于扩展和维护。
199 13
鸿蒙开发:实现AOP代码插桩能力
|
9月前
|
供应链 搜索推荐 数据挖掘
数据爬取对电商运营有何帮助?
数据爬取在电商运营中至关重要,助力商家了解市场动态、优化策略、提升用户体验。具体表现为:市场分析与竞争情报,如商品信息、促销活动、用户评价等;用户行为分析,构建用户画像,分析留存与流失;商品管理与优化,如定价策略、个性化推荐、库存管理;营销与推广,精准营销、社交媒体分析、广告优化;用户体验优化,如网站性能、客户服务;供应链管理,供应商评估、物流优化。通过数据爬取,商家能提高竞争力和盈利能力,实现商业目标。
|
11月前
|
SQL DataWorks 数据可视化
DataWorks产品体验与评测
在当今数字化时代,数据处理的重要性不言而喻。DataWorks作为一款数据开发治理平台,在数据处理领域占据着重要的地位。通过对DataWorks产品的体验使用,我们可以深入了解其功能、优势以及存在的问题,并且与其他数据处理工具进行对比,从而为企业、工作或学习中的数据处理提供有价值的参考。
458 6
DataWorks产品体验与评测
|
Java
Java Socket编程与多线程:提升客户端-服务器通信的并发性能
【6月更文挑战第21天】Java网络编程中,Socket结合多线程提升并发性能,服务器对每个客户端连接启动新线程处理,如示例所示,实现每个客户端的独立操作。多线程利用多核处理器能力,避免串行等待,提升响应速度。防止死锁需减少共享资源,统一锁定顺序,使用超时和重试策略。使用synchronized、ReentrantLock等维持数据一致性。多线程带来性能提升的同时,也伴随复杂性和挑战。
372 0
|
SQL Oracle 关系型数据库
[oracle]使用impdp导入数据时卡在视图
[oracle]使用impdp导入数据时卡在视图
521 2
|
数据安全/隐私保护
阿里云商标注册流程
很多用户有注册商标的需求,又不知道怎么注册商标。特别是他们想在阿里云注册商标,其实注册商标很简单。商标类型,又分为:文字商标,图形商标,文字图形组合商标。无论你在阿里云是要买域名,买服务器,还是干嘛,首先你都需要注册阿里云账号的。
|
算法
基于DSP的音频信号降噪技术
基于DSP的音频信号降噪技术
641 4
|
SQL 关系型数据库 MySQL
【MySQL进阶之路 | 基础篇】子查询之一(单行子查询, 多行子查询)
【MySQL进阶之路 | 基础篇】子查询之一(单行子查询, 多行子查询)