此题中起点有1000个,边有20000条。用链式前向星建图,再枚举起点用SPFA的话,超时了。(按理说,两千万的复杂度应该没超吧。不过一般说计算机计算速度 1~10 千万次/秒。也许拿最烂的计算机来卡时间)

  有一个技巧,加一个超级源点。也就是加一个点,使得该点连通所有的起点,并且边的权值为0。这个技巧应用蛮多的。网络流、最小树形图都有题目这样做。

 

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5. #include<algorithm>
  6. using namespace std;
  7. const int N = , M=;
  8. const int INF = 0x3f3f3f3f;
  9. struct node
  10. {
  11. int to, w, next;
  12. };
  13. node edge[M];
  14. int head[N], dist[N], outq[N];
  15. bool vis[N];
  16. int tot;
  17. bool SPFA(int s, int n )
  18. {
  19. int i,k;
  20. for(i=;i<=n;i++) dist[i]=INF;
  21. memset(vis,,sizeof(vis));
  22. memset(outq,,sizeof(outq));
  23. queue<int > q;
  24. while(!q.empty()) q.pop();
  25. vis[s]=;
  26. dist[s]=;
  27. q.push(s);
  28. while(!q.empty())
  29. {
  30. int u=q.front();
  31. q.pop();
  32. vis[u]=;
  33. outq[u]++;
  34. if(outq[u]>n) return ;
  35. k=head[u];
  36. while(k>=)
  37. {
  38. if(dist[edge[k].to]-edge[k].w>dist[u])
  39. {
  40. dist[edge[k].to]=dist[u]+edge[k].w;
  41. if(!vis[edge[k].to])
  42. {
  43. vis[edge[k].to]=;
  44. q.push(edge[k].to);
  45. }
  46. }
  47. k=edge[k].next;
  48. }
  49. }
  50. return ;
  51. }
  52. void addedge(int i,int j,int w)
  53. {
  54. edge[tot].to=j;
  55. edge[tot].w=w;
  56. edge[tot].next=head[i];
  57. head[i]=tot++;
  58. }
  59. void init()
  60. {
  61. tot=;
  62. memset(head,-,sizeof(head));
  63. }
  64. int main()
  65. {
  66. //freopen("test.txt","r",stdin);
  67. int i,j,k,n,m,t,s;
  68. while(scanf("%d%d%d",&n,&m,&t)!=EOF)
  69. {
  70. init();
  71. while(m--)
  72. {
  73. scanf("%d%d%d",&i,&j,&k);
  74. addedge(i,j,k);
  75. }
  76. scanf("%d",&k);
  77. for(i=;i<k;i++)
  78. {
  79. scanf("%d",&s);
  80. addedge(n+,s,);
  81. }
  82. SPFA(n+,n+);
  83. if(dist[t]==INF) printf("-1\n");
  84. else printf("%d\n",dist[t]);
  85. }
  86. return ;
  87. }

hdu2680 Choose the best route 最短路(多源转单源)的更多相关文章

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

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

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

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

  4. hdu2680 choose the best route

    题目 题意:给定一个有向图,多个起点,一个终点,求起点到终点的最短路. 这道题TLE了好多次,两侧次的对比主要在于对起点的处理上,法一:最开始是采用的hdu2066--一个人的旅行,这道题的方法做的, ...

  5. HDU-2680 Choose the best route 单向边+反向dijkstra

    https://vjudge.net/problem/HDU-2680 题意:以起始点 终点 长度 给出一个图,已知可以从w个起点出发,求从任一起点到同一个终点s的最短路径.注意是单向边.m<1 ...

  6. 最短路之SPFA(单源)HDU 2544

    #include <iostream> #include <queue> #include <algorithm> #define MAXLEN 1005 #def ...

  7. 最短路之SPFA(单源)HDU 2066

    #include "iostream" #include "cstdio" #include "queue" #include <cs ...

  8. 最短路之Dijkstra(单源)HDU 2544

    #include <iostream> using namespace std; ; ][]; ]; int middist; ]; void dijkstra(int n,int m) ...

  9. 最短路之SPFA(单源)HDU 1317

    #include <iostream> #include<cstdio> #include<cstring> #include<cmath> #incl ...

随机推荐

  1. Android 性能测试初探(二)

    书接前文 Android 性能测试初探(一).上回大体介绍了下在 android 端的性能测试项,现在我们就细节测试项做一些阐述(包括如何自己 DIY 测试). 首先我们来说说启动时间.关于应用的启动 ...

  2. js数组操作find查找特定值结合es6特性

    js数组操作find查找特定值结合es6特性

  3. Emoji表情处理工具类

    import java.util.regex.Matcher; import java.util.regex.Pattern; public class EmojiToString { /** * 将 ...

  4. lua_自己对“lua函数”知识点的总结

    lua_自己对“lua函数”知识点的总结 1.lua函数的定义 --lua中,函数都是function类型的对象.(1)其可以被比较 (2)其可以赋值给一个对象(3)可以传递给函数(4)可以从函数中返 ...

  5. Lua循环结构while循环、repeat 循环、for循环_学习笔记03

    Lua循环结构while循环.repeat 循环.for循环 while语法结构 while 循环条件 do 循环体  end --1.输出1到100 index = do print(index) ...

  6. PAT 1089. Insert or Merge

    Insertion sort iterates, consuming one input element each repetition, and growing a sorted output li ...

  7. Bootstrap关于表单(二):水平表单

    Bootstrap框架默认的表单是垂直显示风格,但很多时候我们需要的水平表单风格(标签居左,表单控件居右) 在Bootstrap框架中要实现水平表单效果,必须满足以下两个条件: 1.在<form ...

  8. BZOJ 1396 识别子串 (后缀自动机、线段树)

    手动博客搬家: 本文发表于20181221 00:58:26, 原地址https://blog.csdn.net/suncongbo/article/details/85150962 嗯,以后博客内容 ...

  9. 【hihoCoder挑战赛28 A】异或排序

    [题目链接]:http://hihocoder.com/problemset/problem/1509 [题意] [题解] 每次找到相邻两个数的二进制形式中; 不同的最高位; 显然S在这一位必然是确定 ...

  10. 元素类型为 "session-factory" 的内容必须匹配 "(property*,mapping*,(class-cach....解决方法

    http://www.cnblogs.com/kisso143/p/3642057.html property必须写在mapping的上面.