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
 
dijkstra代码:
  1. #include<stdio.h>
  2. #define INF 0x3f3f3f3f
  3. #define N 1010
  4. int vis[N], dis[N], cost[N][N];
  5. int n, m, s, w, p, q, t;
  6. int min(int x, int y)
  7. {
  8.  
  9. return x < y ? x : y;
  10. }
  11. void dijkstra(int beg)
  12. {
  13. int u, v;
  14. for(u = ; u <= n; u++)
  15. {
  16. vis[u] = ;
  17. dis[u] = INF;
  18. }
  19. dis[beg] = ;
  20. while(true)
  21. {
  22. v = -;
  23. for(u = ; u <= n; u++)
  24. if(!vis[u] && (v == - || dis[u] < dis[v]))
  25. v = u;
  26. if(v == -)
  27. break;
  28. vis[v] = ;
  29. for(u = ; u <= n; u++)
  30. dis[u] = min(dis[u], dis[v] + cost[v][u]);
  31. }
  32. }
  33. int main()
  34. {
  35. int i , j;
  36. while(~scanf("%d%d%d", &n, &m, &s))
  37. {
  38. for(i = ; i <= n; i++)
  39. for(j = i; j <= n; j++)
  40. cost[i][j] = cost[j][i] = INF;
  41. while(m--)
  42. {
  43. scanf("%d%d%d", &p, &q, &t);
  44. if(cost[q][p] > t)
  45. cost[q][p] = t;
  46. }
  47. scanf("%d", &w);
  48. int sum = INF, b;
  49. dijkstra(s);
  50. for(i = ; i <= w; i++)
  51. {
  52. scanf("%d", &b);
  53. if(sum > dis[b])
  54. sum = dis[b];
  55. }
  56.  
  57. if(sum == INF)
  58. printf("-1\n");
  59. else
  60. printf("%d\n", sum);
  61. }
  62. return ;
  63. }

spfa代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <queue>
  4. #define N 10000000
  5. #define M 1010
  6. #define INF 0x3f3f3f3f
  7. using namespace std;
  8. int n, m, s, cnt;
  9. int vis[M], head[M], time[M];
  10. queue<int>q;
  11. struct node
  12. {
  13. int from, to, cost, next;
  14. }road[N];
  15. void add(int x, int y, int z)
  16. {
  17. node e = {x, y, z, head[x]};
  18. road[cnt] = e;
  19. head[x] = cnt++;
  20. }
  21. void spfa()
  22. {
  23. while(!q.empty())
  24. {
  25. int u = q.front();
  26. q.pop();
  27. vis[u] = ;
  28. for(int i = head[u]; i != -; i = road[i].next)
  29. {
  30. int v = road[i].to;
  31. if(time[v] > time[u] + road[i].cost)
  32. {
  33. time[v] = time[u] + road[i].cost;
  34. if(!vis[v])
  35. {
  36. vis[v] = ;
  37. q.push(v);
  38. }
  39. }
  40. }
  41. }
  42. }
  43. int main()
  44. {
  45. while(~scanf("%d%d%d", &n, &m, &s))
  46. {
  47.  
  48. memset(head, -, sizeof(head));
  49. memset(vis, , sizeof(vis));
  50. memset(time, INF, sizeof(time));
  51. while(m--)
  52. {
  53. int p, q, t;
  54. scanf("%d%d%d", &p, &q, &t);
  55. add(p, q, t);
  56. //add(q, p, t);
  57. }
  58. int w;
  59. scanf("%d", &w);
  60. while(w--)
  61. {
  62. int posi;
  63. scanf("%d", &posi);
  64. q.push(posi);
  65. time[posi] = ;
  66. vis[posi] = ;
  67. }
  68. spfa();
  69. if(time[s] == INF)
  70. printf("-1\n");
  71. else
  72. printf("%d\n", time[s]);
  73. }
  74. return ;
  75. }

hdoj 2680 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. 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(SPFA)

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

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

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

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

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

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

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

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

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

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

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

  9. 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 ...

随机推荐

  1. Android中RelativeLayout各个属性的含义

    android:layout_above="@id/xxx"  --将控件置于给定ID控件之上android:layout_below="@id/xxx"  - ...

  2. 53. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

  3. 性能测试框架Locust初学笔记

    Locust初探 Locust是一款类似于Jmeter开源负载测试工具,所不同的是它是用python实现,并支持python脚本. locust提供web ui界面,能够方便用户实时监控脚本运行状态. ...

  4. C语言初始化——bss段初始化、跃入C、C与汇编

    1.bss段初始化 变量 存放位置 初始化的全局变量 数据段 局部变量 栈 malloc函数分配的 堆 未初始的全局变量 bss段 说明:全局变量在未赋初值时,会被保留到bss段. 测试: #incl ...

  5. iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】

                   在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...

  6. vsftpd 修改默认目录

    默认配置下: 匿名用户登录 vsftpd 服务后的根目录是 /var/ftp/:系统用户登录 vsftpd 服务后的根目录是系统用户的家目录. 若要修改登录 vsftpd 服务后的根目录,只要修改 / ...

  7. Linux系统man查询命令等级及意义

    1:用户命令,可由任何人启动的 2:系统调用,由内核提供的函数 3:库函数 4:设备,/dev目录下的特殊文件 5:文件格式描述,例如/etc/passwd 6:游戏 7:杂项,例如宏命令包.惯例等 ...

  8. Apache(ApacheHaus)安装配置教程

    1,Apache下载 选择一个版本,点击Download 点击File For Microsoft Windows 由于Apache HTTP Server官方不提供二进制(可执行)的发行版,所以我们 ...

  9. awk用法

    目前虽然有很多工具可以代替awk,但是呢我还是认为awk还是非常重要,比如有时候load数据到hive,mysql发现数据有点问题,这样可以先对比文件和库中数据是否一致,这样awk就发挥用处了,还有从 ...

  10. 三级联动---DropDownList控件

    AutoPostBack属性:意思是自动回传,也就是说此控件值更改后是否和服务器进行交互比如Dropdownlist控件,若设置为True,则你更换下拉列表值时会刷新页面(如果是网页的话),设置为fl ...