题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间。

思路:用Floyd超时,Dijkstra遍历,但是也超时。仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转置邻接矩阵,从终点找最小的起点,转换成了单源最短路问题。

代码:

  1. #include<cstdio>
  2. #include<set>
  3. #include<cmath>
  4. #include<stack>
  5. #include<cstring>
  6. #include<algorithm>
  7. #define ll long long
  8. using namespace std;
  9. const int maxn = +;
  10. const int INF = 0x3f3f3f3f;
  11. int mp[maxn][maxn];
  12. int dis[maxn];
  13. int vis[maxn];
  14. int n,m;
  15. void dijkstra(int st){
  16. memset(vis,,sizeof(vis));
  17. memset(dis,INF,sizeof(dis));
  18. dis[st] = ;
  19. for(int i = ;i <= n;i++){
  20. int Min = INF,k = ;
  21. for(int j = ;j <= n;j++){
  22. if(!vis[j] && dis[j] < Min){
  23. Min = dis[j];
  24. k = j;
  25. }
  26. }
  27. vis[k] = ;
  28. for(int j = ;j <= n;j++){
  29. if(dis[j] > dis[k] + mp[k][j]){
  30. dis[j] = dis[k] + mp[k][j];
  31. }
  32. }
  33. }
  34. }
  35.  
  36. int main(){
  37. int s;
  38. while(scanf("%d%d%d",&n,&m,&s) != EOF){
  39. memset(mp,INF,sizeof(mp));
  40. while(m--){
  41. int u,v,w;
  42. scanf("%d%d%d",&u,&v,&w);
  43. mp[v][u] = min(mp[v][u],w);
  44. }
  45. dijkstra(s);
  46. int ans = INF;
  47. scanf("%d",&m);
  48. while(m--){
  49. int u;
  50. scanf("%d",&u);
  51. ans = min(ans,dis[u]);
  52. }
  53. if(ans == INF) printf("-1\n");
  54. else printf("%d\n",ans);
  55. }
  56. return ;
  57. }

HDU 2680 Choose the best route(多起点单终点最短路问题)题解的更多相关文章

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

  2. hdu 2680 Choose the best route

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...

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

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

  4. HDU 2680 Choose the best route(SPFA)

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

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

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

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

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

  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. HDU 2612 Find a way【多起点多终点BFS/两次BFS】

    Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

随机推荐

  1. vscode中的vue文件中emmet进行tab键不起作用

    文件--首选项---设置 搜索: emmet.includeLanguages在右边修改 "emmet.triggerExpansionOnTab": true, "em ...

  2. bootstrap之辅助类

    类 描述 .pull-left 元素浮动到左边 .pull-right 元素浮动到右边 .center-block 设置元素为 display:block 并居中显示 .clearfix 清除浮动 . ...

  3. 【BZOJ2957】楼房重建 分块

    [BZOJ2957]楼房重建 Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子 ...

  4. Css--input输入框点击时去掉外框outline:medium;(chrome)

    .search input[type='search']{ background:no-repeat 0 0 scroll #EEEEEE; border:none; outline:medium; ...

  5. kafka简介【转】

    一.为什么需要消息系统 () 解耦 在项目启动之初来预测将来项目会碰到什么需求,是极其困难的.消息系统在处理过程中间插入了一个隐含的.基于数据的接口层,两边的处理过程都要实现这一接口.这允许你独立的扩 ...

  6. 基于JDK1.8的String源码学习笔记

    String,可能是学习Java一上来就学习的,经常用,但是却往往只是一知半解,甚至API有时也得现查.所以还是老规矩,倒腾源码. 一.java doc 这次首先关注String的doc,因为其实作为 ...

  7. pta 习题集 5-2 找出不是两个数组共有的元素 (5分)

    给定两个整型数组,本题要求找出不是两者共有的元素. 输入格式: 输入分别在两行中给出两个整型数组,每行先给出正整数NN(≤20≤20),随后是NN个整数,其间以空格分隔. 输出格式: 在一行中按照数字 ...

  8. python redis基本概念简单操作

    转自:http://www.cnblogs.com/melonjiang/p/5342383.html 一.redis redis是一个key-value存储系统.和Memcached类似,它支持存储 ...

  9. Django - 常用配置

    一.logging配置 Django项目常用的logging配置 settings.py LOGGING = { 'version': 1, 'disable_existing_loggers': F ...

  10. Tickets---hdu1260 (简单dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260 题意就是有n个人排队买票,每个人需要的时间是a[i] (1=< i <=N),但是现 ...