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

题目意思:实质就是给定一个多源点到单一终点的最短路。

卑鄙题~~~有向图。初始化map时 千万不要写成 map[i][j] = map[j][i] = X。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. #define INF 0xfffffff
  8. const int maxn = + ;
  9. int dist[maxn], map[maxn][maxn], vis[maxn];
  10. int n, m, s;
  11.  
  12. void Init()
  13. {
  14. for (int i = ; i <= n; i++)
  15. {
  16. for (int j = ; j <= n; j++)
  17. map[i][j] = (i == j ? : INF);
  18. }
  19. int p, q, t, w;
  20. for (int i = ; i < m; i++)
  21. {
  22. scanf("%d%d%d", &p, &q, &t);
  23. if (map[p][q] > t)
  24. map[p][q] = t; // 写成map[p][q] = map[q][p] = t 会wa的!!!
  25. }
  26. scanf("%d", &w);
  27. for (int i = ; i < w; i++)
  28. {
  29. scanf("%d", &p);
  30. map[][p] = map[p][] = ;
  31. }
  32. for (int i = ; i <= n; i++)
  33. dist[i] = map[][i];
  34. }
  35.  
  36. void Dijkstra()
  37. {
  38. int u, maxx;
  39. memset(vis, , sizeof(vis));
  40. for (int i = ; i <= n; i++)
  41. {
  42. maxx = INF;
  43. for (int j = ; j <= n; j++)
  44. {
  45. if (!vis[j] && dist[j] < maxx)
  46. maxx = dist[u=j];
  47. }
  48. vis[u] = ;
  49. for (int j = ; j <= n; j++)
  50. {
  51. if (dist[j] > dist[u] + map[u][j])
  52. dist[j] = dist[u] + map[u][j];
  53. }
  54. }
  55. }
  56.  
  57. int main()
  58. {
  59. while (scanf("%d%d%d", &n, &m, &s) != EOF)
  60. {
  61. Init();
  62. Dijkstra();
  63. printf("%d\n", dist[s] == INF ? - : dist[s]);
  64. }
  65. return ;
  66. }

hdu 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 最短路问题

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

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

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

  5. HDU 2680 Choose the best route(SPFA)

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

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

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

  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. hdu 1002.A + B Problem II 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. #include <stdi ...

  9. hdoj 2680 choose the best route

    Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...

随机推荐

  1. MongoDB_语法命令

    可以通过MongoDB shell 来连接MongoDB服务: ./mongo   进入交互 数据库-->集合-->文档 几个文档就组成了集合,可以设置固定大小的集合,集合就会有过期机制, ...

  2. R语言入门---杂记(一)---R的常用函数

    1.nchar():查看字符串长度. 2.rev(): 给你的数据翻个个 3.sort():给你数据排个序(默认从小到大依次排列) 4.runif():产生均匀分布的随机数 #runif

  3. Working with multiple environments

    ASP.NET Core引入了对多个环境(例如开发,暂存和生产环境)的支持. 可以用环境变量来指示应用程序正在运行的环境,从而让app来做相应的配置. Development, Staging, Pr ...

  4. 小程序-列表块/类式ul-li格式(1)

    摘要 目前列表能布局出来,但是目前我个人还没解决的问题是:如果每个列表块都有详情页怎么解决呢? 1:我的效果图 2.正常的每个都能点击的html 注:上面的代码确实能够实现我的每个[menu2_vie ...

  5. cart树剪枝

    当前子树的损失函数: $C_a(T) = C(T) + a|T|$, 其中$C(T)$为对训练数据的预测误差,$|T|$为树的叶子结点数目,反映模型的复杂度.对固定的$a$,一定存在使损失函数$C_a ...

  6. 转: memcached Java客户端spymemcached的一致性Hash算法

    转自:http://colobu.com/2015/04/13/consistent-hash-algorithm-in-java-memcached-client/ memcached Java客户 ...

  7. CellularAutomation(细胞自己主动机)

    CellularAutomation(细胞自己主动机) 细胞自己主动机(英语:Cellular automaton).又称格状自己主动机.元胞自己主动机,是一种离散模型,在可算性理论.数学及理论生物学 ...

  8. UVA 11246 - K-Multiple Free set(数论推理)

    UVA 11246 - K-Multiple Free set 题目链接 题意:一个{1..n}的集合.求一个子集合.使得元素个数最多,而且不存在有两个元素x1 * k = x2,求出最多的元素个数是 ...

  9. BFS和DFS记录路径

    DFS记录路径的意义好像不大,因为不一定是最短的,但是实现起来却很简单. #include<math.h> #include<stdio.h> #include<queu ...

  10. 【dotnet跨平台】&quot;dotnet restore&quot;和&quot;dotnet run&quot;都做了些什么?

    [dotnet跨平台]"dotnet restore"和"dotnet run"都做了些什么? 前言: 关于dotnet跨平台的相关内容.能够參考:跨平台.NE ...