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
#include <iostream>
#include <queue>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;
#define MAXV 1010
#define INF 0x3f3f3f3f
int map[MAXV][MAXV],n,m,s,W;
int vis[MAXV],cast[MAXV];void Dijkstra()
{int minn,pos;memset(vis,0,sizeof(vis));vis[0] = 1;for(int i = 0; i<=n; i++)cast[i] = map[0][i];for(int i = 0; i<=n; i++){minn = INF;for(int j = 0; j<=n; j++){if(cast[j]<minn && !vis[j]){pos = j;minn = cast[j];}}vis[pos] = 1;for(int j = 0; j<=n; j++){if(cast[pos]+map[pos][j]<cast[j] && !vis[j])cast[j] = cast[pos]+map[pos][j];}}
}int main()
{while(~scanf("%d%d%d",&n,&m,&s)){for(int i=0; i<=n; i++)for(int j=0;j<=n;j++)if(i==j) map[i][j]=0;else map[i][j]=INF;int p,q,t;for(int i=1;i<=m;i++){scanf("%d%d%d",&p,&q,&t);if(t<map[p][q])map[p][q] = t;}scanf("%d",&W);int x;while(W--){scanf("%d",&x);map[0][x] = 0;}Dijkstra();if(cast[s]==INF)cout << -1 << endl;else cout << cast[s] << endl;}//cout << "Hello world!" << endl;return 0;
}

Choose the best route相关推荐

  1. HDU 2068 Choose the best route

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

  2. hdu 2680 Choose the best route

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2680 简单最短路问题..... 运行结果: Accepted 2680 265MS 4164K 138 ...

  3. AcWing 1137. Choose the best route(朴素dijkstra反向建图 or 虚拟源点法)

    题目比较简单,讲两种做法 法一.二都是用的朴素dijkstra算法 法一:反向建图 求终点s到每个起点的最短距离 O(T * (n^2 + n))(T表示多组测试数据)820ms #include & ...

  4. What are HANA's models of cloud computing, and which should I choose?

    What are HANA's models of cloud computing, and which should I choose? http://searchsap.techtarget.co ...

  5. 我如何使用深度学习通过Fast.ai对医学图像进行分类

    by James Dietle 詹姆斯·迪特尔(James Dietle) Convolutional Neural Networks (CNNs) have rapidly advanced the ...

  6. Logstash Introduction

    https://www.cnblogs.com/aresxin/p/8035137.html Elasticsearch是个开源分布式搜索引擎,提供搜集.分析.存储数据三大功能.它的特点有:分布式,零 ...

  7. 【HDOJ图论题集】【转】

    1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...

  8. 一系列图论问题[转]

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  9. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

最新文章

  1. 简单的实现购物车功能,还有不到位的地方,加油!!!
  2. go python java_一文助你搞懂参数传递原理解析(java、go、python、c++)
  3. java 字符流 utf8,JAVA基础(字符流设置编码读写字符)
  4. pydev工程linux运行,Linux平台下Python的安装及IDE开发环境搭建
  5. Eclipse Git下载问题:Internal error; consult Eclipse error log.
  6. CUDA C编程权威指南 第六章 流和并发
  7. union和union all区别
  8. Windows字体拯救计划(雅黑+monaco+mactype)
  9. 电脑键盘灯光的调节方法
  10. linux 繁体转简体,linux2 简体中文转繁体
  11. 如何修改CryEngine5.5着色器
  12. 态度决定高度,高度决定命运。对自己要狠一点,再狠一点,因为,你要的比别人多,就必须付出得比别人多。
  13. 【已解决】Failed to discover available identity versions when contacting http://controller:5000/v3.
  14. 全网营销如何落地?全网营销的途径有哪些?
  15. 强制域名使用 HTTPS(SSL)
  16. 软件工程可行性研究报告
  17. 1 简历该怎么写?注意事项--绝密,程序员大厂面试求职大揭秘!
  18. python实现文本读写功能
  19. 网页上的微服务—微前端架构实践
  20. 我的世界java版地图结构_我的世界地图种子竟然有42亿个 那些神奇的建筑都是怎么生成的...

热门文章

  1. 数据包格式_理解MQTT协议数据包结构
  2. Python爬取网站用户手机号_利用python爬取慕课网站上面课程
  3. 云大计算机专业录取分数线,2016年云南大学艺术类专业录取分数线
  4. mysql ibatis xml配置 like_iBatis学习方法及入门总结
  5. 计算机一级b考试理论知识,全国计算机等级考试一级b知识点
  6. C语言在建筑专业的应用,新工科背景下基于OBE的《C语言程序设计》课程建设
  7. java求100以内的a2 b2=c2,Java语言程序设计Ⅱ-中国大学mooc-试题题目及答案
  8. java异常什么时候抛出异常,java - 什么时候应该抛出IllegalArgumentException?
  9. 十九、面试必考,Java中的this关键字
  10. 如何遍历一个JS对象中的所有属性,输出键值对--我居然犯错半个小时