Roadblocks
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5508   Accepted: 2088

Description

Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.

The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.

The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).

Input

Line 1: Two space-separated integers: N and R
Lines 2..
R+1: Each line contains three space-separated integers:
A,
B, and
D that describe a road that connects intersections
A and
B and has length
D (1 ≤
D ≤ 5000)

Output

Line 1: The length of the second shortest path between node 1 and node
N

Sample Input

  1. 4 4
  2. 1 2 100
  3. 2 4 200
  4. 2 3 250
  5. 3 4 100

Sample Output

  1. 450

Hint

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

Source

USACO 2006 November Gold

题目大意:在一个图上有许多个农场,有个人从1农场出发,到他的朋友n农场去,他不想走一条最短路径,这次他想换条路走,要你帮他找一条次短路径,次短路的定义是,比最短路径长度短(可能有多条),但是不会比其他的路径长度长。而且告诉你数据中一定存在至少一条次短路。

思路 :暴力每一条边 

求出从起点到达该边的起点的最短距离 求终点到达该边的终点距离 相加再加上边长  判断是否比最短路长 且最接近最短路

  1. #include<stdio.h>
  2. #include<queue>
  3. #include<algorithm>
  4. #include<vector>
  5. #include<string.h>
  6. using namespace std;
  7. const int INF=10000000;
  8. const int maxn=5002;
  9. struct Edge{
  10. int from,to,dis;
  11. };
  12. struct node{
  13. int d,u;
  14. bool operator <(const node &rhs) const {
  15. return d>rhs.d;//从小到大排列
  16. }
  17. };
  18. int mn;
  19. struct Dij
  20. {
  21. int n,m;//点数和边数
  22. vector<Edge>edges;//边列表
  23. vector<int>nd[maxn];//从每个节点出发的边编号 从0开始
  24. bool vis[maxn];//是否参观过
  25. int mmin[maxn];//s到各个点的最小距离
  26. void init(int n)
  27. {
  28. this->n=n;
  29. for(int i=0;i<n;i++) nd[i].clear();//邻接表清空
  30. edges.clear();//清空边表
  31. }
  32.  
  33. void addedge(int from,int to,int dis)
  34. //如果是无向图 每条无向边需要调用2次
  35. {
  36. edges.push_back((Edge){from,to,dis});//是{ 不是(
  37. m=edges.size();
  38. nd[from].push_back(m-1);
  39. }
  40.  
  41. void dij(int s)//求s到所有点的距离
  42. {
  43. priority_queue<node>que;
  44. for(int i=0;i<n;i++) mmin[i]=INF;
  45. mmin[s]=0;
  46. memset(vis,0,sizeof(vis));
  47. que.push((node){0,s});//注意符号 不是括号
  48. while(!que.empty())
  49. {
  50. node x=que.top();que.pop();
  51. int u=x.u;
  52. if(vis[u]) continue;
  53. vis[u]=true;
  54. for(int i=0;i<nd[u].size();i++)
  55. {
  56. Edge& e=edges[nd[u][i]];
  57. int max1=0,max2=0;
  58. if(mmin[e.to]>mmin[u]+e.dis)
  59. {
  60. // printf("mmin[e.to]=%d\n",mmin[e.to]);
  61. max1=mmin[e.to];
  62. mmin[e.to]=mmin[u]+e.dis;
  63. max2=mmin[e.to];
  64. if(max1!=INF&&max2!=INF&&mn>max1-max2) mn=max1-max2;
  65. que.push((node){mmin[e.to],e.to});
  66. }
  67.  
  68. }
  69. }
  70. }
  71. };
  72.  
  73. vector<int>path;
  74. int main()
  75. {
  76. int n,m,x,y,z,s,e;
  77. while(scanf("%d %d",&n,&m)!=EOF)
  78. {
  79. mn=999999999;
  80. s=0;e=n-1;
  81. Dij solve[2];
  82. solve[0].init(n);
  83. solve[1].init(n);
  84. while(m--)
  85. {
  86. scanf("%d %d %d",&x,&y,&z);
  87. x--;y--;
  88. solve[0].addedge(x,y,z);solve[0].addedge(y,x,z);
  89. solve[1].addedge(x,y,z);solve[1].addedge(y,x,z);
  90. }
  91. solve[0].dij(s);//求1到所有点的距离
  92. solve[1].dij(e);//求n到所有点的距离
  93. int ans=INF;
  94. for(int i=0;i<solve[0].edges.size();i++)
  95. {
  96. Edge &edge=solve[0].edges[i];
  97. int st=edge.from,ed=edge.to;
  98. int mid=solve[0].mmin[st]+solve[1].mmin[ed]+edge.dis;
  99. if(mid>solve[0].mmin[e])
  100. {
  101. if(ans>mid) ans=mid;
  102. }
  103. }
  104. printf("%d\n",ans);
  105. }
  106. return 0;
  107. }

参考:

http://blog.csdn.net/wangjian8006/article/details/7992280

poj 3255 求次大最短路的更多相关文章

  1. POJ 3255:Roadblocks(次短路)

    题目大意:求无向图的次短路. 分析: 在起点终点各求一次最短路,枚举边,通过该边的最短路为其权值加上到起点和终点最短路之和,找到最短但又比最短路长的路径. 代码: program block; typ ...

  2. 【POJ - 3255】Roadblocks(次短路 Dijkstra算法)

    Roadblocks 直接翻译了 Descriptions Bessie搬到了一个新的农场,有时候他会回去看他的老朋友.但是他不想很快的回去,他喜欢欣赏沿途的风景,所以他会选择次短路,因为她知道一定有 ...

  3. POJ 3255 Roadblocks(A*求次短路)

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12167   Accepted: 4300 Descr ...

  4. POJ 3255 Roadblocks (次级短路问题)

    解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...

  5. Remmarguts' Date POJ - 2449 (A*搜索|k短路)

    "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. ...

  6. poj 1847 Tram【spfa最短路】

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12005   Accepted: 4365 Description ...

  7. POJ - 1062 昂贵的聘礼(最短路Dijkstra)

    昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u SubmitStatus Descr ...

  8. POJ 3255 Roadblocks (次短路模板)

    Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS   Memory Limit: 65536K       Descriptio ...

  9. POJ - 3255 SPFA+邻接表求次短路径

    题意:给出m条边 , n个顶点,u [ i ]到v [ i ] 的距离w [ i ],求除了最短路的那条最短的边的长度. 思路:之前有做过相似的题,使用迪杰斯特拉算法求单源最短路径,并且记录路径,枚举 ...

随机推荐

  1. 电子科大POJ "3*3矩阵的乘法"

    3阶矩阵的乘法 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) c-source ...

  2. Oracle基础学习5-- Oracle权限之”角色”

    不论什么与权限相关的东西都少不了"角色"的概念,Java如此,.Net如此,Oracle当然也不例外. 角色事实上就是权限的集合,将多个权限打包到一个角色中,这样每一个角色有特定的 ...

  3. scrollTo和scrollTo.js

    最近做一个项目前端要用到scrollTo和滚动视觉差.顺便把两个东西拿出来温习一下. HTML DOM里面定义了scrollTo方法,用法:scrollTo(xpos,ypos),把内容滚动到当前的指 ...

  4. Android默认启动程序问题

    参考地址:http://www.cnblogs.com/Lewis/p/3316946.html 怎么让我们自己开发的Android程序设为默认启动呢?其实很简单,只要在AndroidManifest ...

  5. 将数组,表的某一列转换为string字符串的方法

    样例:字符串数组为array,str为字符串数组转换成的字符串 string[] array = { etr, kdgj, 3454, tyt, gff }; string str=string.Jo ...

  6. Java学习——继承

    将学生工人的共性描述提取出来,单独进行描述,只要让学生和工人与单独描述的这个类有关系,就可以了. 继承:1,提高了代码的复用性.2,让类与类之间产生了关系.有了这个关系,才有了多态的特性. 注意:千万 ...

  7. Android的Activity屏幕切换滑动动画

    Activity的切换效果使用的是Android的动画效果,Android的动画在官方有相关资料:http://developer.android.com/guide/topics/graphics/ ...

  8. Unique Binary Search Trees In JAVA

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. core-site.xml配置项:hadoop.tmp.dir

    hadoop.tmp.dir:A base for other temporary directories. 集群运行后,修改该配置项后,发现类似错误: -- ::, INFO org.apache. ...

  10. HDU 2072(单词数)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] “就是统计一篇文章里不同单词的总数”(已经是一句话了..) [题目分析] 明显需要去重,上set,因为按行分析,又没有EOLN用 ...