Choose the best route

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”.
 
$Dijkstra$ 算法:以点为思考中心的最短路径算法。
图结构存储:邻接表
流程:1.初始化
 const int INF = 1e9;
bool hasFind[maxn];
for (int i = ;i<= n ;i++)
dist[i] = INF;
dist[sNode] = ;
memset(hasFind,,sizeof hasFind);
hasFind[sNode] = true;

具体流程为:

 for (int i =  ;i< n- ;i++){
int nId = - ;
for (int j = ;j< n ;j++){
if (!hasFind[j]){
if (nId == -)
nId = j;
else if (dist[j]<dist[nId])
nId = j;
}
}
hasFind[nId] = true;
for (int i = ;i< node[nId].size() ;i++){
int nextId = node[nId][i].nextId;
if (node[nId][i].dist + dist[nId]< dist[nextId]){
dist[nextId] = node[nId][i].dist + dist[nId];
que.push(nextId);
}
}
}

时间复杂度 节点个数 $N$,边个数 $M$ $O$($N\times N$)

举例 • 求所有节点到节点 1 的最短距离

1. 初始化
• 将源节点 1,放入已获取最短路径集合, 集合变为 {1}

• 未获取最短路径节点结合 {2,3,4,5}

• 根据节点 1 来更新所有节点距离源节点的距离 $dist$

2. 流程
(a) $step$ 1:

• 从未获取最短路径节点结合 {2,3,4,5} 中,选取距离源节点最 近的节点 3

• 将节点 3,放入已获取最短路径集合, 集合变为 {1,3}

• 根据节点 3 来更新所有节点距离源节点的距离 $dist$

(b) $step$ 2:

• 从未获取最短路径节点结合 {2,4,5} 中,选取距离源节点最 近的节点 2

• 将节点 2,放入已获取最短路径集合, 集合变为 {1,2,3}

• 根据节点 2 来更新所有节点距离源节点的距离 $dist$

(c) $step$ 3:

• 从未获取最短路径节点结合 {4,5} 中,选取距离源节点最近 的节点 4

• 将节点 4,放入已获取最短路径集合, 集合变为 {1,2,3,4}

• 根据节点 4 来更新所有节点距离源节点的距离 $dist$

(d) $step$ 4:

• 从未获取最短路径节点结合 {5} 中,选取距离源节点最近的 节点 5

• 将节点 5,放入已获取最短路径集合, 集合变为 {1,2,3,4,5}

• 根据节点 5 来更新所有节点距离源节点的距离 $dist$

(e) 终止条件,所有节点都放入到了已获取最短路径集合。

把所有部分合并在一起得到一段代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define Inf 0x3f3f3f3f using namespace std;
int map[][];
int vis[],dis[];
int n,m;//n个点,m条边
void Init ()
{
memset(map,Inf,sizeof(map));
for(int i=;i<=n;i++)
{
map[i][i]=;
}
}
void Getmap()
{
int u,v,w;
for(int t=;t<=m;t++)
{
scanf("%d%d%d",&u,&v,&w);
if(map[u][v]>w)
{
map[u][v]=w;
map[v][u]=w;
}
}
} void Dijkstra(int u)
{
memset(vis,,sizeof(vis));
for(int t=;t<=n;t++)
{
dis[t]=map[u][t];
}
vis[u]=;
for(int t=;t<n;t++)
{
int minn=Inf,temp;
for(int i=;i<=n;i++)
{
if(!vis[i]&&dis[i]<minn)
{
minn=dis[i];
temp=i;
}
}
vis[temp]=;
for(int i=;i<=n;i++)
{
if(map[temp][i]+dis[temp]<dis[i])
{
dis[i]=map[temp][i]+dis[temp];
}
}
}
} int main()
{ scanf("%d%d",&m,&n);
Init();
Getmap();
Dijkstra(n);
printf("%d\n",dis[]);
return ;
}

这道题的代码如下:

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std; const int INF=0x3f3f3f3f;
const int N=;
int mp[N][N];
int dis[N];
int vis[N];
int m;
int n;
int dijstra()
{
memset(dis,0x3f,sizeof(dis));
memset(vis,,sizeof(vis));
dis[]=;
for(int i=;i<=n;i++)
{
int k=;
int mini=INF;
for(int j=;j<=n;j++)
{
if(!vis[j]&&mini>dis[j])
mini=dis[k=j];
}
vis[k]=;
if(k==m) return dis[m];
for(int j=;j<=n;j++)
{
if(vis[j]||mp[k][j]==INF) continue;
dis[j]=min(dis[j],dis[k]+mp[k][j]);
}
}
return dis[m];
}
int main()
{
int s; //已修好的路有几条
while(~scanf("%d%d%d",&n,&s,&m)) //终点是m,最远的点是n
{
memset(mp,INF,sizeof(mp));
while(s--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(mp[a][b]>c)
mp[a][b]=c;
}
int d;
scanf("%d",&d);
while(d--)
{
int x;
scanf("%d",&x);
mp[][x]=;
}
int k=dijstra();
if(k==INF) printf("-1\n");
else printf("%d\n",dijstra());
}
return ;
}
 

最短路问题-- Dijkstra Choose the best route的更多相关文章

  1. hdu 2680 Choose the best route (dijkstra算法 最短路问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS ( ...

  2. hdu 2680 Choose the best route

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...

  3. HDU2680 Choose the best route 最短路 分类: ACM 2015-03-18 23:30 37人阅读 评论(0) 收藏

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. HDU2680 Choose the best route 2017-04-12 18:47 28人阅读 评论(0) 收藏

    Choose the best route Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Othe ...

  5. hdu-2680 Choose the best route(最短路)

    题目链接: Choose the best route Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K ( ...

  6. Choose the best route(最短路)dijk

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS (Java/ ...

  7. HDU 2680 Choose the best route(多起点单终点最短路问题)题解

    题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...

  8. Choose the best route HDU杭电2680【dijkstra算法 || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

  9. HDU 2680 Choose the best route 最短路问题

    题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...

随机推荐

  1. 阿里云https+nginx服务搭建

    购买证书 通过控制台进入CA证书服务,点击右上角的购买证书,进入如下图的界面,选择免费的Symantec的DV SSL. 一路点过去,然后回到证书服务主页,会出现一条订单信息,点击补全,如下图所示. ...

  2. 解析underscore中的debounce

    先奉上源码 取自Underscore.js 1.9.1的debounce _.debounce = function(func, wait, immediate) { var timeout, res ...

  3. 一对多关联按照一方的id查找信息的一个笛卡尔积问题

    mapper中,关联的一对多,正确的结果应该是按照一方的id查找,根据映射得到的是一个一方对象,对象里嵌套这list属性,但是结果却出来了多条,在sql中实验 一方中123456789只有一条数据 多 ...

  4. cf 498 B. Name That Tune

    不会不会,,,%%http://hzwer.com/5813.html #include<cstdio> #include<iostream> #include<algo ...

  5. JavaScript.StringObjec.replace

    //StringObject.replace(/regexp/,newContent); //1当newContent为新字符串,就直接用newContent对匹配的内容进行替换. //2当newCo ...

  6. Oracle Exadata 学习笔记之核心特性Part1

    近年来,国内众多厂商都有一体机的产品,不过更多都是围绕硬件本身的堆砌和优化,那么这些产品和Oracle一体机最大的区别在哪里呢?最近读了李亚的<Oracle Exadata技术详解>,系统 ...

  7. Mysql 事务隔离级别分析

    Mysql默认事务隔离级别是:REPEATABLE-READ --查询当前会话事务隔离级别mysql> select @@tx_isolation; +-----------------+ | ...

  8. 062-PHP函数按值传参,交换数值函数

    <?php function swap($x,$y){ //定义交换数值函数 $temp=$x; $x=$y; $y=$temp; } $m=5; $n=15; echo "交换前:& ...

  9. Linux 下 zip 文件解压乱码如何解决

    作者:Latm Ake链接:https://www.zhihu.com/question/20523036/answer/35225920来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...

  10. 八、React实战:可交互待办事务表(表单使用、数据的本地缓存local srtorage、生命同期函数(页面加载就会执行函数名固定为componentDidMount()))

    一.项目功能概述 示例网址:http://www.todolist.cn/ 功能: 输入待做事项,回车,把任务添加到 [正在进行] [正在进行] 任务,勾选之后,变成已[经完成事项] [已完成事务], ...