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中回车换行符

    这里使用了,sql 函数.replace(string_expression , string_pattern , string_replacement), 第一个参数:要查找的字段. 第二个参数:要 ...

  2. FB分别编译各个项目

    FB里面有个 ActionScript模块 功能, 可以将 不同模块分别编译成一个个swf,这样会将各个独立的模块从主swf中分离出来.如果玩家没使用过这个模块,就不会加到内存中去,这样可以减少不必要 ...

  3. [pjsip]板砖理解pjsip体系结构

    在pjsip的官方开发向导中给出两张体系结构图,分别是消息流程图和类图,如下所示: 图1:消息流程图 图2:类图

  4. Android布局文件layout.xml的一些属性值

        第一类:属性值 true或者 false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 andr ...

  5. Ubuntu 14.10 下卸载MySQL

    前面讲了Mysql的简单安装方式,通过sudo apt-get install mysql-server 等脚本,安装之后如何卸载? 1 通过下面命令删除MySQL sudo apt-get auto ...

  6. (转)HTML5开发学习(3):本地存储之Web Sql Database

    原文:http://www.cnblogs.com/xumingxiang/archive/2012/03/25/2416386.html HTML5开发学习(3):本地存储之Web Sql Data ...

  7. SQL Server LEFT Functions

    LEFT(string, n)函数,是处理字符数据获取子字符串.第一个参数是将要处理的字符串,第二个参数,是从字符串的左边开始截取的字符个数. 例子: DECLARE @string NVARCHAR ...

  8. codevs 5429 完全背包

    单调队列优化. 好像有点烦...调了许久. #include<iostream> #include<cstdio> #include<cstring> #inclu ...

  9. 日历控件修改的JS代码

    var bMoveable=true; var _VersionInfo=" " ; //============================================= ...

  10. Swift:网络库Alamofire

    一,Alamofire的说明与配置 1,什么是Alamofire (1)Alamofire 的前身是 AFNetworking.AFNetworking 是 iOS 和 OS X 上很受欢迎的第三方H ...