Choose the best route

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13229    Accepted Submission(s): 4280

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
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #define MAX 0x3f3f3f3f
  5. using namespace std;
  6. int map[][],d[],n,m,s,t;
  7. void dijkstra(int x)
  8. {
  9. int i,j,min,mark,used[];
  10. for(i=;i<=n;i++)
  11. {
  12. used[i]=;
  13. d[i]=map[x][i];
  14. }
  15. d[x]=;
  16. used[x]=;
  17. for(i=;i<=n;i++)
  18. {
  19. min=MAX;
  20. mark=-;
  21. for(j=;j<=n;j++)
  22. {
  23. if(!used[j]&&d[j]<min)
  24. {
  25. min=d[j];
  26. mark=j;
  27. }
  28. }
  29. if(mark==-)
  30. break;
  31. used[mark]=;
  32. for(j=;j<=n;j++)
  33. {
  34. if(!used[j]&&d[j]>d[mark]+map[mark][j])
  35. d[j]=d[mark]+map[mark][j];
  36. }
  37. }
  38. }
  39. int main()
  40. {
  41. int a,b,c,i,j;
  42. while(scanf("%d%d%d",&n,&m,&s)!=EOF)
  43. {
  44. memset(map,MAX,sizeof(map));
  45. for(i=;i<m;i++)
  46. {
  47. scanf("%d%d%d",&a,&b,&c);
  48. if(map[b][a]>c)
  49. map[b][a]=c;
  50. }
  51. dijkstra(s);
  52. int start;
  53. int mi=MAX;
  54. scanf("%d",&t);
  55. for(i=;i<t;i++)
  56. {
  57. scanf("%d",&start);
  58. mi=mi<d[start]?mi:d[start];
  59. }
  60. if(mi==MAX)
  61. printf("-1\n");
  62. else
  63. printf("%d\n",mi);
  64. }
  65. return;
  66. }

hdu 2680 Dijstra的更多相关文章

  1. HDU - 2680 最短路 spfa 模板

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少..只有可以运用和hdu2066 ...

  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. HDU 2680(最短路)(多个起始点)

    这道题也是死命TLE.. http://acm.hdu.edu.cn/showproblem.php?pid=2680 /* 使用pair代替结构 */ #include <iostream&g ...

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

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

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

  7. UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)

    UVA 题意:两个绿洲之间是沙漠,沙漠的温度不同,告诉起点,终点,求使得从起点到终点的最高温度最小的路径,如果有多条,输出长度最短的路径: 思路:用最小费用(最短路径)最大流(最小温度)也能搞吧,但因 ...

  8. HDU 2680 最短路 迪杰斯特拉算法 添加超级源点

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  9. hdu 2680 最短路径(dijkstra算法+多源最短路径单源化求最小值)这题有点意思

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

随机推荐

  1. Windows下MongoDB的安装过程及基本配置

    首先当然是到官网下载 MongoDB 了,点击进入后会看到下载页面,如下图所示: 安装有2种方式: 一种是通过直接点击下载的安装文件进行安装: 另一种是通过命令提示符来安装,下面我将直接通过第一种方式 ...

  2. Sublime Text 全局搜索Ctrl+Shift+F快捷键不能用

      Sublime Text 全局搜索Ctrl+Shift+F快捷键不能用   和微软输入法的简繁体切换冲突了,关闭输入法的简繁体切换快捷键就好了! 文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢 ...

  3. kotlin陪伴对象

    在kotlin中并没有静态类成员的概念,但并不等于实现类似于静态类成员的功能,陪伴对象可以解决这个问题 fun main(arg: Array<String>) { val create ...

  4. Goland 激活码

    实测有效,分享下 Goland

  5. [C++]单源最短路径:迪杰斯特拉(Dijkstra)算法(贪心算法)

    1 Dijkstra算法 1.1 算法基本信息 解决问题/提出背景 单源最短路径(在带权有向图中,求从某顶点到其余各顶点的最短路径) 算法思想 贪心算法 按路径长度递增的次序,依次产生最短路径的算法 ...

  6. 关于比较js中两个对象相等 ==

    “如果两个操作数都是对象,则比较他们是不是同一个对象(引用的对象在内存中的地址一样),如果两个操作数都指向同一个对象,则相等操作符返回true,否则,返回false”. 我做了一个例子 functio ...

  7. iOS-调用网页聊天、拨打电话

    @property (nonatomic,strong) UIButton *but;@property (nonatomic,strong) UIButton *but1;@property (st ...

  8. WebsSocket

    本篇阅读目录 一.Websocket原理(握手.解密.加密) 二.基于Python实现简单示例 回到顶部 一.Websocket原理(握手.解密.加密) WebSocket协议是基于TCP的一种新的协 ...

  9. 【POJ - 3046】Ant Counting(多重集组合数)

    Ant Counting 直接翻译了 Descriptions 贝西有T种蚂蚁共A只,每种蚂蚁有Ni只,同种蚂蚁不能区分,不同种蚂蚁可以区分,记Sum_i为i只蚂蚁构成不同的集合的方案数,问Sum_k ...

  10. linux netstat 查看网络连接状况

    netstat -lnpnetstat -an |grep 127.0.0.1 tcp 0.0.0.0:* LISTEN tcp 0.0.0.0:* LISTEN [root@wang /]# net ...