Choose the best route

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7062    Accepted Submission(s): 2301

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
 
Author
dandelion
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2112 1874 2544 1217 2066 
 
AC代码及重点讲解:
#include<stdio.h>
#include<string.h>
#define max 0x3f3f3f3f
int map[1002][1002];
int dist[1002];
void dijkstra(int n,int v)
{
 bool vis[1002];
 int i,j;
 for(i=0;i<=n;i++)//i从0开始了
 {
  dist[i]=map[v][i];
  vis[i]=0;
 }
 dist[v]=0;
 vis[v]=1;
 for(i=0;i<=n;i++)//i从0开始了
 {
  int tmp=max;
  int u=v;
  for(j=0;j<=n;j++)//j从0开始了
   if((!vis[j])&&dist[j]<tmp)
   {
    u=j;
    tmp=dist[j];
   }
  vis[u]=1;
  for(j=0;j<=n;j++)//j从0开始了
   if((!vis[j])&&map[u][j]<max)
   {
    int newdist=dist[u]+map[u][j];
    if(newdist<dist[j])
     dist[j]=newdist;
   }
 }
}
int main()
{
 int t,m,s;
 while(scanf("%d%d%d",&t,&m,&s)!=EOF)
 {
  memset(map,max,sizeof(map));
  int i,j,a,b,time;
  for(i=0;i<m;i++)
  {
   scanf("%d%d%d",&a,&b,&time);
   if(map[a][b]>time)
    map[a][b]=time;//该题为有向图,所以在赋值上是单向的
  }
  int x;
  scanf("%d",&x);
  for(i=0;i<x;i++)//重点,这题刚开始给人的感觉就是多源最短路径求最小值,但是如果按照上述的方向写代码的话会出现超时的情况,所以
{            此处是该题对多源最短路径单源化(自己命名)的一种简化方式,由于该题说有s个车站时紧邻着主人公Kiki的家的
   int start;          所以我们自然会将s个车站看成是起点,但是那样超时,所以我们将之单源化,因为不论从哪个车站出发  
  scanf("%d",&start);         都可以看成是从家出发到车站栽倒目的地的,则将0点作为家  由于实际上的起点应该是车站,家只是我们用来简化这道题的
    map[0][start]=0;      解法的一种方式,所以0点到车站所对应的点的距离全部赋值为0而到其他点的距离全赋值为无穷大。相应的在dijkstra() 函数中也会有变化。
}                  
  dijkstra(t,0);         
  if(dist[s]==max)        
  printf("-1\n");         
  else                    
  printf("%d\n",dist[s]);      
}
 return 0;
}

hdu 2680 最短路径(dijkstra算法+多源最短路径单源化求最小值)这题有点意思的更多相关文章

  1. 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)

    关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...

  2. 单源最短路径Dijkstra算法,多源最短路径Floyd算法

    1.单源最短路径 (1)无权图的单源最短路径 /*无权单源最短路径*/ void UnWeighted(LGraph Graph, Vertex S) { std::queue<Vertex&g ...

  3. 求两点之间最短路径-Dijkstra算法

     Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.D ...

  4. 最短路径—Dijkstra算法

    Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...

  5. 最短路径-Dijkstra算法(转载)

    注意:以下代码 只是描述思路,没有测试过!! Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始 ...

  6. 最短路径——Dijkstra算法以及二叉堆优化(含证明)

    一般最短路径算法习惯性的分为两种:单源最短路径算法和全顶点之间最短路径.前者是计算出从一个点出发,到达所有其余可到达顶点的距离.后者是计算出图中所有点之间的路径距离. 单源最短路径 Dijkstra算 ...

  7. 有向网络(带权的有向图)的最短路径Dijkstra算法

    什么是最短路径? 单源最短路径(所谓单源最短路径就是只指定一个顶点,最短路径是指其他顶点和这个顶点之间的路径的权值的最小值) 什么是最短路径问题? 给定一带权图,图中每条边的权值是非负的,代表着两顶点 ...

  8. Python数据结构与算法之图的最短路径(Dijkstra算法)完整实例

    本文实例讲述了Python数据结构与算法之图的最短路径(Dijkstra算法).分享给大家供大家参考,具体如下: # coding:utf-8 # Dijkstra算法--通过边实现松弛 # 指定一个 ...

  9. 网络最短路径Dijkstra算法

    最近在学习算法,看到有人写过的这样一个算法,我决定摘抄过来作为我的学习笔记: <span style="font-size:18px;">/* * File: shor ...

  10. 最短路径-Dijkstra算法与Floyd算法

    一.最短路径 ①在非网图中,最短路径是指两顶点之间经历的边数最少的路径. AE:1    ADE:2   ADCE:3   ABCE:3 ②在网图中,最短路径是指两顶点之间经历的边上权值之和最短的路径 ...

随机推荐

  1. warning malformed '#pragma pack(push[, id], n)' - ignored

    bmp.c:8: warning: malformed '#pragma pack(push[, id], <n>)' - ignored bmp.c:33: warning: #prag ...

  2. validator

    http://rickharrison.github.io/validate.js/validate.js rules: 'required|callback_check_password' vali ...

  3. android button text属性中英文大小写问题

    Android版本升级的原因,需要手动添加属性android:textAllCaps="false"

  4. CodeForces 546A-Soldier and Bananas

    题意: 有n dollar,the first banana cost  k dollars,第i个就需cost k*i,问买w个bananas是否需要借钱:借钱需要多少? 分析:首先计算w个bana ...

  5. [转]VS2005 Debug时提示"没有找到MSVCR80D.dll"的解决办法

    总结各种解决方法如下: 原因:(不知道在说啥)由于VS.net 2005 采用了一种新的DLL方案,搞成一个exe还要配有一个manifest文件(一般在嵌入文件里了,所以看不到,不过也可以不嵌入,这 ...

  6. pig

    1.Pig是基于hadoop的一个数据处理的框架. MapReduce是使用java进行开发的,Pig有一套自己的数据处理语言,Pig的数据处理过程要转化为MR来运行.2.Pig的数据处理语言是数据流 ...

  7. 修改Centos SSH远程端口

    1. 在防火墙添加开放端口10000:本机防火墙和云防火墙 本机防火墙:-A INPUT -m state --state NEW -m tcp -p tcp --dport 10000 -j ACC ...

  8. HTML--10Jquery

    在<网页制作Dreamweaver(悬浮动态分层导航)>中,运用到了jQuery的技术,轻松实现了菜单的下拉.显示.隐藏的效果,不必再用样式表一点点地修改,省去了很多麻烦,那么jQuery ...

  9. 2013年8月份第1周51Aspx源码发布详情

    校企工作室OA源码  2013-8-9 [VS2010]源码描述:主要模块及系统管理功能说明:一.考勤功能模块:考勤分成三个功能,显示签到功能,查询功能,管理功能.1.签到功能分析:在签到功能中,我们 ...

  10. 转 15款免费WiFi(入侵破解)安全测试工具

    转:http://www.ctocio.com/security/cloudsecurity/6594.html 一.Vistumbler扫描器 WiFi 扫描器能能发现附近AP的详细信息,例如信号强 ...