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 ;
}
 

最新文章

  1. log4net使用手册
  2. FMS 4中multicast脚本的小修正
  3. SQL Server数字辅助表的实现
  4. Java算法-选择排序
  5. 3.UNION
  6. Asp.Net验证码3
  7. 【USACO 3.2.5】魔板
  8. [置顶] Linux下文件和目录权限说明
  9. 导出Excel数据
  10. 猎八哥FLY——将数据库中的某一表中的某一列或者多列添加到另一张表的某一列中
  11. PHP curl 常用操作
  12. Http多线程版本
  13. 【38】java的集合框架(容器框架)
  14. centos 下的 clamav 安装使用
  15. C#中自定义高精度Timer定时器的实例教程
  16. 【转载】C#工具类:FTP操作辅助类FTPHelper
  17. mysql查询出现In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column &#39;zhibo.a.id&#39;;
  18. BFS求解迷宫的最短路径问题
  19. npm常用功能
  20. POJ3111 K Best 2017-05-11 18:12 31人阅读 评论(0) 收藏

热门文章

  1. Vue(十)---路由
  2. Jupyter Notebooks usage
  3. 在windows7 64位上安装selenium2library问题解决
  4. 编码解码:UrlDecode解码及UrlEncode编码的jQuery方法
  5. 【LeetCode】反转每对括号间的子串
  6. Elasticsearch开启试用x-pack license
  7. Thread start0 启动分析 一图看懂
  8. 二十四、JavaScript之取字符串长度
  9. 移动MAS短信平台发送短信
  10. 解决Spring Mvc中接受参数绑定重名的方法