Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 10761    Accepted Submission(s): 3484

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
  1. 5 8 5
  2. 1 2 2
  3. 1 5 3
  4. 1 3 4
  5. 2 4 7
  6. 2 5 6
  7. 2 3 5
  8. 3 5 1
  9. 4 5 1
  10. 2
  11. 2 3
  12. 4 3 4
  13. 1 2 3
  14. 1 3 4
  15. 2 3 2
  16. 1
  17. 1
 
Sample Output
  1. 1
  2. -1
  3.  
  4. 该题用到了最短路径万能源点,
  5.  
  6.  
  7. #include<queue>
  8. #include<stdio.h>
  9. #include<string.h>
  10. #define INL 0x3f3f3f3f
  11. using namespace std;
  12. int vid[10000],x[1080][1080],vist[10000];
  13. int N,M,D;
  14. void spfa()
  15. {
  16.     for(int i=0;i<=N;i++)
  17.     {
  18.         vist[i]=INL;vid[i]=0;
  19.     }
  20.     queue<int> q;
  21.     vid[0]=1;
  22.     q.push(0);
  23.     vist[0]=0;
  24.     while(!q.empty())
  25.     {
  26.         int u=q.front();
  27.             q.pop();
  28.            vid[u]=0;
  29.         for(int i=0;i<=N;i++)
  30.         {
  31.             if(vist[i]>vist[u]+x[u][i])
  32.             {
  33.                 vist[i]=vist[u]+x[u][i];
  34.                 if(!vid[i])
  35.                 {
  36.                     q.push(i);
  37.                     vid[i]=1;
  38.                 }
  39.             }
  40.         }
  41.     }
  42. }
  43. int main()
  44. {
  45.     while(scanf("%d%d%d",&N,&M,&D)!=EOF)
  46.     {
  47. 	    memset(x,INL,sizeof(x));
  48.         int a,b,c;
  49.         for(int i=0;i<M;i++)
  50.         {
  51.         	 scanf("%d%d%d",&a,&b,&c);
  52.         	 if(x[a][b]>c)
  53.         	 x[a][b]=c;
  54.         }
  55.         int n,g,min;
  56.         scanf("%d",&n);
  57. 	        for(int i=0;i<n;i++)
  58. 	        {
  59. 	        	scanf("%d",&g);
  60. 	        	x[0][g]=0;//万能源点知识
  61. 	        }
  62.         spfa();
  63.         if(vist[D]==INL)
  64.              printf("-1\n");
  65.           else
  66.             printf("%d\n",vist[D]);
  67.     }
  68.     return 0;
  69. }  



hdoj2680 Choose the best route的更多相关文章

  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. HDU2680 Choose the best route 最短路 分类: ACM 2015-03-18 23:30 37人阅读 评论(0) 收藏

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

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

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

  4. HDU2680 Choose the best route 2017-04-12 18:47 28人阅读 评论(0) 收藏

    Choose the best route Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Othe ...

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

  6. hdu-2680 Choose the best route(最短路)

    题目链接: Choose the best route Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K ( ...

  7. 最短路问题-- Dijkstra Choose the best route

    Choose the best route Problem Description One day , Kiki wants to visit one of her friends. As she i ...

  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. HDU 2680 Choose the best route(SPFA)

    Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...

随机推荐

  1. 厚溥教育1718部数据库连接作业答案,分装一个操作数据库而无需写SQL语句的函数

    <?php header("Content-type:text/html;charset=utf8"); //PHP操作数据库的函数 function phpsql($dbc ...

  2. python 容器 用户注册登录系统

    1. 列表和普通变量有什么区别 列表是数据类型,普通变量是用来存储数据的 可以把列表赋值给普通变量 2.存在列表 a = [11, 22, 33], 如何向列表中添加(增)新元素 44 a.appen ...

  3. c++_凑算式(最新方法)

    凑算式 B DEFA + --- + ------- = 10 C GHI (如果显示有问题,可以参见[图1.jpg]) 这个算式中A~I代表1~9的数字,不同的字母代表不同的数字. 比如:6+8/3 ...

  4. 一个关于vue+mysql+express的全栈项目(三)------ 登录注册功能的实现(已经密码安全的设计)

    本系列文章,主要是一个前端的视角来实现一些后端的功能,所以不会讲太多的前端东西,主要是分享做这个项目学到的一些东西,,,,, 好了闲话不多说,我们开始搭建后端服务,这里我们采用node的express ...

  5. [SQL]数据库中对值为数字,存储格式为varchar类型的字段进行排序

    如果要对数据库中某存储数字的列(存储类型不为int)进行排序,可以在order by 里对该列进行转换, 即如 order by cast(mycolumn as int) desc

  6. Java学习--异常处理及其应用类

    异常是运行时在代码序列中引起的非正常状况,换句话说,异常是运行时错误.在不支持异常处理的计算机语言中,必须手动检查和处理错误----通常是通过使用错误代码,等等.这种方式既笨拙又麻烦.Java的异常处 ...

  7. angularjs自己总结

    1.模块 自定的directive和controller需要在同一个model下,或者另外的model depModules他了. ng-app要等于model的名字,所有的directive要在下面 ...

  8. 使用PL/SQL将sql脚本数据导入数据库

    一. PL/SQL登录到数据库,使用tools工具进行导入.使用plsql登录到需要导入数据的数据库.点击工具栏上[tools]--[Import tables] 二.commit;

  9. Hibernate分页查询报错

    不知道什么原因,就是这里报错的

  10. jenkins 提示No emails were triggered

    发送邮件 Jenkins->系统管理->系统设置,在“邮件通知”里设置smtp服务器地址,以及发送者邮箱地址,在具体的任务构建完成以后,可以设置发送邮件,在某一个任务的"Add ...