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. SQL Server 自定义字符串分割函数

    一.按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果(标量值函数)   create function Func_StrArrayL ...

  2. iOS app调试的黑魔法--第三方库

    http://www.cocoachina.com/ios/20140928/9785.html

  3. [开发笔记]-js判断用户的浏览设备是移动设备还是PC

    最近做的一个网站页面中需要根据用户的访问设备的不同来显示不同的页面样式,主要是判断移动设备还是电脑浏览器访问的. 下面给出js判断处理代码,以作参考. <script type="te ...

  4. switch… case 语句的用法(二)

    总结来说:switch的用法是判断case后面的表达式和switch后面的表达式是否相匹配,一旦case匹配,就会顺序执行后面的程序代码,而不管后面的case是否匹配,直到遇见break.都不匹配找d ...

  5. JS小问题总结

    1. 超链接中href=#与href=javascript:void(0) 的区别  #包含了一个位置信息.默认的锚是#top 也就是网页的上端:而javascript:void(0)   仅仅表示一 ...

  6. 深入剖析HADOOP程序日志

    深入剖析HADOOP程序日志 前提 本文来自于 博客园 逖靖寒的世界 http://gpcuster.cnblogs.com 了解log4j的使用. 正文 本文来自于 博客园 逖靖寒的世界 http: ...

  7. printf的格式输出

    格式字符 意义 d 以十进制形式输出带符号整数(正数不输出符号) o 以八进制形式输出无符号整数(不输出前缀0) x,X 以十六进制形式输出无符号整数(不输出前缀Ox) u 以十进制形式输出无符号整数 ...

  8. ansible quick start

    1. ansible默认开启ControlPersist,也就是持续化ssh的socket连接,以跳过每次task都需要进行主机认证. 2. 但是centos的openssh版本太老了,不支持Cont ...

  9. Integer 和int

    获取Integer对象有两种方式:Integer x = 100:或者Integer x = new Integer(100): Integer x = 100:等价于Integer x = Inte ...

  10. Codeforces Round #247 (Div. 2)

    A.水题. 遍历字符串对所给的对应数字求和即可. B.简单题. 对5个编号全排列,然后计算每种情况的高兴度,取最大值. C.dp. 设dp[n][is]表示对于k-trees边和等于n时,如果is== ...