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

题意

kiki想坐公交车去朋友家,给你公交车线路图(有向),kiki有w个地方可以坐共交车,问到朋友家的最短路,若不能到输出-1

题解

这题就是kiki从w个起点坐到终点的最短路,算是单源最短路,可以每个起点跑一遍SPFA(这样太费时间了)

假如我们把kiki家看成出发点0,可以到w个距离为0的公交站,这样我们就把多个起点归1了,跑一遍SPFA就行了

代码

 #include<bits/stdc++.h>
using namespace std; #define INF 0x3f3f3f3f
typedef pair<int,int> pii;
vector<pii> G[];
int Vis[],Dist[];
void spfa()
{
memset(Vis,,sizeof(Vis));
memset(Dist,INF,sizeof(Dist));
queue<int> Q;
Q.push();
Dist[]=;//从家里0出发
while(!Q.empty())
{
int u=Q.front();Q.pop();
Vis[u]=;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i].first,w=G[u][i].second;
if(Dist[v]>Dist[u]+w)
{
Dist[v]=Dist[u]+w;
if(Vis[v]==)
{
Vis[v]=;
Q.push(v);
}
}
}
}
}
int main()
{
int n,m,s,k,u,v,w;
while(scanf("%d%d%d",&n,&m,&s)!=EOF)
{
for(int i=;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(pii(v,w));
}
scanf("%d",&k);
for(int i=;i<k;i++)
{
scanf("%d",&v);
G[].push_back(pii(v,));//从家到公交站距离为0
}
spfa();
printf("%d\n",Dist[s]==INF?-:Dist[s]);
for(int i=;i<n;i++)
G[i].clear();
}
return ;
}

HDU 2680 Choose the best route(SPFA)的更多相关文章

  1. hdu 2680 Choose the best route

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

  2. 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 ( ...

  3. hdu 2680 Choose the best route (dijkstra算法)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...

  4. hdu 2680 Choose the best route 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...

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

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

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

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

  7. HDU 2068 Choose the best route

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

  8. hdoj 2680 choose the best route

    Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...

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

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

随机推荐

  1. subprocess.Popen模块

    该类用于在一个新的进程中执行一个子程序.subprocess模块底层的进程创建和管理是由Popen类来处理的. 1.subprocess.Popen的构造函数 class subprocess.Pop ...

  2. js 正则函数初级

    1.test :正则匹配字符串,如果成功则返回true,若果失败则返回false 格式:/正则表达式/.test(字符串) 默认吗匹配规则,区分大小写:如果不区分大小写,则加修饰符 i 例子: < ...

  3. Linux主题:获取帮助

    Linux有多种方式获取帮助,这些帮助通过不同的命令,获得不同详细程度和文字量的帮助. help help方式有两种用法,一种是help command,另一种是command --help.前一种是 ...

  4. UGUI的text赋值问题-速度

    仅是简单的给一个ugui.text组件不断的赋值字符串,就会带来很高的CPU消耗,约0.5MS左右. 这个过程主要是消耗在字体的MESH顶点重建. 在游戏中变化的字体一般不多,聊天面板虽然变化,刷新率 ...

  5. Java IO流学习总结二:File

    Java File类的功能非常强大,利用java基本上可以对文件进行所有操作.首先来看File类的构造函数的源码 /** * Internal constructor for already-norm ...

  6. ReactiveX 学习笔记(19)使用 RxSwift + RxCocoa 进行 GUI 编程

    课题 程序界面由3个文本编辑框和1个文本标签组成. 要求文本标签实时显示3个文本编辑框所输入的数字之和. 文本编辑框输入的不是合法数字时,将其值视为0. 3个文本编辑框的初值分别为1,2,3. 创建工 ...

  7. ReactiveX 学习笔记(3)转换数据流

    Transforming Observables 本文的主题为转换 Observable 的操作符. 这里的 Observable 实质上是可观察的数据流. RxJava操作符(二)Transform ...

  8. [namespace]PHP命名空间的使用基础

    -------------------------------------------------------------------------------------------------- 一 ...

  9. Android DevArt6:Android中IPC的六种方式

    Android中IPC的六种方式 1.使用Bundle 最简单的进程间通信方式:Intent + Bundle: 支持三大组件:Activity.Service.BroadcastReceiver : ...

  10. 24.类的加载机制和反射.md

    目录 1类的加载连接和初始化 1.1类的加载过程 1.2类的加载器 1.2.1类的加载机制 1类的加载连接和初始化 1.1类的加载过程 类的加载过程简单为分为三步:加载->连接->初始化 ...