题目大意: 已知n 个点,m条路线,s为终点;给出m条路线及其权值;给出w个起点,求最短路!

思路:基础的dijkstra,有向无环正权最短路,只要把终点和起点 reverse考虑便可。

AC代码如下:

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. using namespace std;
  5. #define INF 1000000
  6. #define M 1010
  7. int n, m, s;
  8. int d[M];
  9. int w[M][M];
  10. int v[M];
  11.  
  12. void dijkstra(int s)
  13. {
  14. memset(v,0,sizeof(v));
  15. memset(d,INF,sizeof(d));
  16. d[s] = 0;
  17. for(int i = 0; i < n; i++)
  18. {
  19. int x, m = INF; //WA了半天找不到的bug,就是这里;开始忘了放在循环里了,ORZ。。。所以m会一直是0.。不WA才怪。
  20. for(int j = 1; j <= n; j++)
  21. if(!v[j]&&d[j] <= m) m = d[x = j];
  22. v[x] = 1;
  23.  
  24. for(int j = 1; j <= n; j++)
  25. d[j] = d[j] < d[x] + w[x][j]? d[j]:d[x] + w[x][j];
  26.  
  27. }
  28. }
  29.  
  30. void init(int n)
  31. {
  32. for(int i = 1; i <= n; i++)
  33. for(int j = 1; j <= n; j++)
  34. w[i][j] = INF;
  35. }
  36.  
  37. int main()
  38. {
  39. while(scanf("%d%d%d",&n, &m, &s) == 3)
  40. {
  41. init(n);
  42. for(int i = 0; i < m; i++)
  43. {
  44. int a,b,c;
  45. scanf("%d%d%d",&a,&b,&c);
  46. if(w[b][a] > c)
  47. w[b][a] = c;
  48. }
  49. dijkstra(s);
  50.  
  51. int la,st,Min = INF;
  52. scanf("%d",&la);
  53.  
  54. for(int i = 0; i < la; i++)
  55. {
  56. scanf("%d",&st);
  57. if(d[st] < Min) Min = d[st];
  58. //cout<<st<<"*"<<d[st]<<endl;
  59. }
  60. if(Min < INF)
  61. printf("%d\n",Min);
  62. else
  63. cout<<"-1"<<endl;
  64. }
  65. return 0;
  66.  
  67. }

作者:u011652573 发表于2014-3-6 0:18:03 原文链接
阅读:92 评论:0 查看评论

[原]poj-2680-Choose the best route-dijkstra(基础最短路)的更多相关文章

  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 (dijkstra算法)

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

  4. hdoj 2680 choose the best route

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

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

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

  6. HDU 2680 Choose the best route(SPFA)

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

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

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

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

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

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

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

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

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

随机推荐

  1. bzoj 3232 01分数规划+最大权封闭子图判定

    我们的目标是使v/c最小化,所以构造函数g(x)=v-x*c,那么 二分一个X,判断当时的v-x*c的值是多少,然后根据g(x)函数的 单调递减性来二分,判断,直到g(x)=0的时候当前的X就是答案. ...

  2. poj 1300 Door Man 欧拉回路

    题目链接:http://poj.org/problem?id=1300 You are a butler in a large mansion. This mansion has so many ro ...

  3. 2014 ACM/ICPC Asia Regional Guangzhou Online

    Wang Xifeng's Little Plot http://acm.hdu.edu.cn/showproblem.php?pid=5024 预处理出每个点八个方向能走的最远距离,然后枚举起点,枚 ...

  4. FormCreate & FormActivate & FormShow执行顺序演示

    procedure TForm1.FormCreate(Sender: TObject);begin  form1.Caption:=form1.Caption +'+Create'; end; pr ...

  5. Codeforces Round #FF (Div. 2)

    又一场中国场,果然注定被虐的场... A,B:都很水,差不多模拟就好了: C题:CF难得的题意简洁, 我们可以预处理出从左到右递增的数列个数, 举个例子:1 3 2 4 5 7 L[I]       ...

  6. 在JavaScript中实现yield,实用简洁实现方式。

    原题还是老赵的: http://blog.zhaojie.me/2010/06/code-for-fun-iterator-generator-yield-in-javascript.html 原以为 ...

  7. javascript中的JSON序列化与反序列化

    简单粗暴上代码: function create() { this.name = "jack"; this.sex = "man"; } create.prot ...

  8. Android内存泄漏问题(一)

    前言 不少人认为JAVA程序,因为有垃圾回收机制,应该没有内存泄露. 其实如果我们一个程序中,已经不再使用某个对象,但是因为仍然有引用指向它,垃圾回收器就无法回收它,当然该对象占用的内存就无法被使用, ...

  9. typedef (还需经常看看加深理解)

    看了 c++primer 1,typedef名字 typedef定义以关键字typedef开始,后面是 数据类型+标示符. 并未引入新的类型,只是现有数据类型的同义词 例: typedef doubl ...

  10. 华为上机:求2的N次幂的值

    求2的N次幂的值 描述: 求2的N次幂的值(N最大不超过31,用位运算计算,结果以十六进制进行显示). 运行时间限制: 无限制 内存限制: 无限制 输入: 数字N 输出: 2的N次方(16进制,需要按 ...