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. BBS-文章详情页、评论、评论树

    1.简单的实现评论功能 article_detail.html,拿到数据  由路--给视图函数--写入数据库 <p>评论内容:</p> <textarea name=&q ...

  2. mui init 出现无法引入子页面问题

    1. 检查项目中是否重复出现了 mui.init() 函数; mui.init({ subpages: [{ styles: { // top: "44px", top: &quo ...

  3. linux终端发送邮件

    使用mail: echo "This is message to send" | mail -a /tmp/attachment.txt -s "This is Subj ...

  4. hdoj 1003 学习思路

    基本解题思路:动态规划,不考虑穷举,分治. 根据网上,状态转移方程是:MaxSum[i] = Max{ MaxSum[i-1] + A[i], A[i]} 翻译公式:到当前位置i 时,最大子序列和为: ...

  5. 如何让cxgrid自动调整列宽

    1.选中cxgridview,在属性中找OptionsView--->ColumAutoWidth,把这个属性设为True; 2.在FDMemtable的open之后加上如下代码即可 [delp ...

  6. Linux命令:unzip

    语法: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir] 默认行为将zip文件中的内容全部解压缩到当前目录下. ...

  7. unity admob

    插件地址:https://github.com/unity-plugins/Unity-Admob 2017.04.11测试使用发现GoogleMobileAds.framework有问题,导致出现U ...

  8. centos 下修改mysql 默认字符集

    解决办法: CentOS 7下修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 mysql  -u root - ...

  9. 0.1Linux系统开发Angular项目一一首次运行环境的安装(chrome ,terminator,git,node)

    首先,保证你已经安装了虚拟机(虚拟机可以用virturalbox或者VM)并安装了ubuntu镜像! 安装Chrome浏览器 安装terminator(可以多开)代替原来的命令行工具 sudo apt ...

  10. PPT的感想

    ①double:使用double类型的数值进行计算, 其结果是不精确的.因为double类型的数值占用64个二进制数,除去最高位表示正负符号的位,在最低位上一定会与实际数据存在误差. 这个涉及到二进制 ...