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. java程序(一)----HashMap同时获取键值

    快速会用: HashMap<Integer,String> maps=new HashMap<Integer,String>(); maps.put(1,"xiaom ...

  2. Rhel6-mailsystem配置文档

    (postfix+dovecot+mysql+extmail) 理论基础:

  3. jpcap

    1.System.out.println( System.getProperty("java.library.path")); 2.将jpcap.dll放到上边打印的路径中

  4. 学好C++必须要注意的十八个问题

    转自  http://blog.chinaunix.net/uid-7396260-id-2056691.html 一.#include "filename.h"和#i nclud ...

  5. 第八章 标准IO库

    1.IO对象时不可复制或者赋值的:也就是说形参或者返回类型也不能为流类型.如果非要传递或者返回IO对象的的话,则必须传递或者返回指向对象的指针或者引用.如:  ofstream &print( ...

  6. 关于java的static语句块

    声明:转载请注明出处 static{}(即static块),会在类被加载的时候执行且仅会被执行一次,一般用来初始化静态变量和调用静态方法,下面我们详细的讨论一下该语句块的特性及应用. 一.在程序的一次 ...

  7. C#获取项目程序及运行路径的方法

    1.asp.net webform用“Request.PhysicalApplicationPath获取站点所在虚拟目录的物理路径,最后包含“\”: 2.c# winform用 A:“Applicat ...

  8. hdu 2050

    PS:真是醉了..之前觉得这题很难..然后在网上找到了大神给的深入解析,跪谢...这里贴大神博客的网址:http://blog.csdn.net/lishuhuakai/article/details ...

  9. leetcode 238 Product of Array Except Self

    这题看似简单,不过两个要求很有意思: 1.不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨. 2.空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里 ...

  10. UIkit框架之UIScrollView

    1.继承链:UIview:UIresponder:NSObject 引言 UIScrollView的是几个UIKit类包括的UITableView和UITextView中的超类. 一个UIScroll ...