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. C++11并发之std::thread<转>

    最近技术上没什么大的收获,也是悲催的路过~ 搞一点新东西压压惊吧! C++11并发之std::thread 知识链接: C++11 并发之std::mutex C++11 并发之std::atomic ...

  2. 继承标签extend

    写页面的时候,整体框架是相同的,只有content区是不同的,所以就有了继承的概念: 在content 里面加一个 {%block content%} {% endblock %} 其他框架的继承: ...

  3. ubuntu交换Caps 和 ESC

    https://askubuntu.com/questions/363346/how-to-permanently-switch-caps-lock-and-esc This will allow y ...

  4. C++复习:STL之算法

    算法 1算法基础 1.1算法概述 算法部分主要由头文件<algorithm>,<numeric>和<functional>组成. <algorithm> ...

  5. Unable to connect to zookeeper server within timeout: 5000

    错误 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error crea ...

  6. Nmap结果文件XML文件解析

    对nmap扫描结果xml格式的文件进行解析,无需直接xml解析或读取,可直接使用模块: 1.nmapparser 安装:pip install nmapparser Demo: #!/usr/bin/ ...

  7. RGB图片取大于阈值部分

    #include<opencv2\opencv.hpp> #include <iostream> using namespace cv; using namespace std ...

  8. DNS域名解析中A、AAAA、CNAME、MX、NS、TXT、SRV、SOA、PTR各项记录的作用

    名注册完成后首先需要做域名解析,域名解析就是把域名指向网站所在服务器的IP,让人们通过注册的域名可以访问到网站.IP地址是网络上标识服务器的数字地址,为了方便记忆,使用域名来代替IP地址.域名解析就是 ...

  9. 渲染Keynote

    [渲染Keynote] 1.渲染图元(rendering primitives),可以是点.线.三角. 2.显卡对于显存的访问速度更快,而且大多数显卡对于RAM没有直接的访问权利 . 3.裁剪(Cli ...

  10. 早上STO单紧急寻源处理

    truncate table SAP_SO_STO1_STO2; truncate table SAP_STO1_STO2; INSERT INTO STG.SAP_SO_STO1_STO2(VBEL ...