Acceteped : 23   Submit : 61
Time Limit : 5000 MS   Memory Limit : 65536 KB

Description

题目描述

N(3≤N≤1,000)个城市(编号从1~N),M(N-1≤M≤10,000)条公路连接这些城市,每条公路都是双向通车的。 你想从1号城市出发,到达N号城市,期间你希望通过按顺序经过K(0≤K≤3)个指定的城市(这K+2个城市只允许达到1次),求最短的里程。

输入

存在多个样例。 每个样例的第一行是三个整数N,M,K。如果N,M,K为0,则表示输入结束。 以后是M行表示M条公路,每行三个整数x(1≤x≤N),y(1≤y≤N),c(1≤c≤1,000),表示城市x与城市y之间有一条距离为c的公路。输入保证任意两座城市之间至少存在一条路。然后的一行包含K个城市的序号,序号属于[2,N-1]。

输出

每行输出一个样例的结果,为一个整数。如果不存在这样的方案,输出“Impossible”。

样例输入

  1. 3 3 1
    1 2 3
    2 3 4
    1 3 2
    2
    0 0 0

样例输出

  1. 7
 

Sample Input

 
 

Sample Output

 
 

Source

解题:题目说了,只限起点终点,以及要求的几个点只能访问一次,其他点嘛,呵呵!选择微软的C++编译器,速度快,g++慢得不行

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <vector>
  8. #include <queue>
  9. #include <cstdlib>
  10. #include <string>
  11. #include <set>
  12. #include <stack>
  13. #define LL long long
  14. #define pii pair<int,int>
  15. #define INF 0x3f3f3f3f
  16. using namespace std;
  17. const int maxn = ;
  18. struct arc {
  19. int to,next;
  20. };
  21. int head[maxn],mp[maxn][maxn],tot;
  22. int n,m,k,city[],d[maxn];
  23. bool done[maxn];
  24. arc g[maxn<<];
  25. priority_queue< pii,vector< pii >,greater< pii > >q;
  26. void add(int u,int v) {
  27. g[tot].to = v;
  28. g[tot].next = head[u];
  29. head[u] = tot++;
  30. }
  31. void dijkstra(int s,int t) {
  32. int i,u,v;
  33. for(i = ; i <= n; i++) {
  34. d[i] = INF;
  35. done[i] = false;
  36. }
  37. for(i = ; i <= k+; i++)
  38. if(city[i] != s && city[i] != t)
  39. done[city[i]] = true;
  40. while(!q.empty()) q.pop();
  41. d[s] = ;
  42. q.push(make_pair(d[s],s));
  43. while(!q.empty()) {
  44. u = q.top().second;
  45. q.pop();
  46. if(done[u]) continue;
  47. done[u] = true;
  48. for(i = head[u]; i != -; i = g[i].next) {
  49. if(d[g[i].to] > d[u]+mp[u][g[i].to]) {
  50. d[g[i].to] = d[u]+mp[u][g[i].to];
  51. q.push(make_pair(d[g[i].to],g[i].to));
  52. }
  53. }
  54. if(done[t]) return;
  55. }
  56. }
  57. int main() {
  58. int i,j,u,v,w,sum;
  59. while(scanf("%d %d %d",&n,&m,&k),n+m+k) {
  60. for(i = ; i <= n; i++) {
  61. head[i] = -;
  62. for(j = ; j <= n; j++)
  63. mp[i][j] = INF;
  64. }
  65. for(tot = i = ; i < m; i++) {
  66. scanf("%d %d %d",&u,&v,&w);
  67. if(mp[u][v] == INF) {
  68. mp[u][v] = mp[v][u] = w;
  69. add(u,v);
  70. add(v,u);
  71. } else if(mp[u][v] > w) {
  72. mp[u][v] = mp[v][u] = w;
  73. }
  74. }
  75. city[] = ;
  76. for(i = ; i <= k; i++) scanf("%d",city+i);
  77. city[i] = n;
  78. bool flag = true;
  79. for(sum = i = ; i <= k; i++) {
  80. dijkstra(city[i],city[i+]);
  81. if(d[city[i+]] == INF) {flag = false;break;}
  82. sum += d[city[i+]];
  83. }
  84. flag?printf("%d\n",sum):puts("Impossible");
  85. }
  86. return ;
  87. }

xtu Shortest Path的更多相关文章

  1. hdu-----(2807)The Shortest Path(矩阵+Floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  3. The Shortest Path in Nya Graph

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  4. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  5. Shortest Path(思维,dfs)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  6. Shortest Path

    Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  7. (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  8. 【ZOJ2760】How Many Shortest Path

    How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...

  9. [Swift]LeetCode847. 访问所有节点的最短路径 | Shortest Path Visiting All Nodes

    An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...

随机推荐

  1. 二分查找+数学 HDOJ 4342 History repeat itself

    题目传送门 题意:计算从1开始到第n个非完全平方数的开方和 分析:设第n个非完全平方数的值为a,x * x < a < (x+1) * (x+1),而且易得(tmp = sqrt (a) ...

  2. VS2010下安装Opencv 分类: Opencv 2014-11-02 13:51 778人阅读 评论(0) 收藏

    Opencv作为一种跨平台计算机视觉库,在图像处理领域得到广泛的应用,下面介绍如何在VS2010中安装配置Opencv 一.下载Opencv 下载网址:http://sourceforge.net/p ...

  3. java getDocumentBase() 得到的文件夹路径

    参考一个百度知道上的回答 举例说来,假设你的项目文件是xx,而这个xx文件夹是在D盘下的yy文件夹里,即项目文件的完整路径D:\yy\xx,则编译运行文件后,在xx文件夹里会产生名为build的文件夹 ...

  4. windwsform登录页面

    简单登录设计: 读取用户名密码 数据库表 实体类 数据访问类: 隐藏登录页面: 回车快捷键: 传值到main窗口:

  5. 一个简单的Java代码生成工具—根据数据源自动生成bean、dao、mapper.xml、service、serviceImpl

    目录结构 核心思想 通过properties文件获取数据源—>获取数据表的字段名称.字段类型等—>生成相应的bean实体类(po.model).dao接口(基本的增删改查).mapper. ...

  6. 学习笔记 第十三章 使用CSS3新布局

    第13章   使用CSS3新布局 [学习重点] 设计多列布局 设计弹性盒布局样式 使用CSS3布局技术设计适用移动需求的网页 13.1  多列布局 CSS3使用columns属性定义多列布局,用法如下 ...

  7. springmvc 的配置 annotation-config/annotation-drive/ component-scan 区别

    1. <context:annotation-config /> 作用隐式的配置注解的加载类,默认的加载了AutowiredAnnotationBeanPostProcessor(auto ...

  8. scala基础篇-03 if与for

    一.Scala中的if是表达式** 1.定义方式 2.例子 二.for 的用法 1.定义方式: for{ x <- xs y=x+ ) }yield y 2.例子:

  9. zookeeper启动

    Zookeeper启动总结1.实际项目用的是Linux,问题不大,本地开发学习用Windows,问题多多.2.Zookeeper3.5.1-alpha,和本地JDK1.7,有冲突,无法正常启动.3.Z ...

  10. adobe开发软件激活

    稳定支持至2017版本系列的adobe开发软件破解激活 本内容属原创,转载请注明出处!   以激活AE CC2017为例演示: 第一步打开软件第二步在产品列表中选择你所安装的产品(注意区分 32 位和 ...