• 原题如下:

    Roadblocks
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 19314   Accepted: 6777

    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: AB, 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

    4 4
    1 2 100
    2 4 200
    2 3 250
    3 4 100

    Sample Output

    450

    Hint

    Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)
  • 题解:这题要求的是次短路,根据Dijkstra算法的思路,依次确定尚未确定的顶点中距离最小的顶点可求得最短路,在这个基础上做少许修改,即可求得次短路,到某个顶点v的次短路无非两种情况:①到其它顶点u的最短路再加上u→v的边,②到u的次短路再加上u→v的边。因此,只要求出到所有顶点的最短路和次短路就好了。对于每个顶点,我们记录最短路和次短路,类似Dijkstra算法那样,不断更新这两个距离。(注:这道题中男女之间的二分图结构是无用的陷阱条件,许多问题中,如果有特殊的结构,往往会考虑如何利用这个结构,但这题不是的)
  • 代码:
     #include <cstdio>
    #include <queue>
    #include <vector>
    #include <functional>
    #include <cctype>
    #include <algorithm>
    #define num s-'0' using namespace std;
    typedef pair<int, int> P; struct edge
    {
    int to;
    int cost;
    }; const int MAX_V=;
    const int INF=0x3f3f3f3f;
    int V,E,n;
    vector<edge> G[MAX_V];
    int dist[MAX_V];
    int dist2[MAX_V]; void solve(); void swap(int &x, int &y)
    {
    int t=x;
    x=y;y=t;
    } void read(int &x){
    char s;
    x=;
    bool flag=;
    while(!isdigit(s=getchar()))
    (s=='-')&&(flag=true);
    for(x=num;isdigit(s=getchar());x=x*+num);
    (flag)&&(x=-x);
    } void write(int x)
    {
    if(x<)
    {
    putchar('-');
    x=-x;
    }
    if(x>)
    write(x/);
    putchar(x%+'');
    } int main()
    {
    read(V);read(E);
    for (int i=; i<E; i++)
    {
    edge e;
    int s,t,c;
    read(s);read(t);read(c);
    e.to=t-;e.cost=c;
    G[s-].push_back(e);
    e.to=s-;e.cost=c;
    G[t-].push_back(e);
    }
    solve();
    write(dist2[V-]);
    putchar('\n');
    } void solve()
    {
    priority_queue<P, vector<P>, greater<P> > que;
    fill(dist, dist+V, INF);
    fill(dist2, dist2+V, INF);
    dist[]=;
    que.push(P(,));
    while (!que.empty())
    {
    P p=que.top(); que.pop();
    int v=p.second, d=p.first;
    if (dist2[v]<d) continue;
    for (int i=; i<G[v].size(); i++)
    {
    edge &e=G[v][i];
    int d2=d+e.cost;
    if (dist[e.to]>d2)
    {
    swap(dist[e.to],d2);
    que.push(P(dist[e.to],e.to));
    }
    if (dist2[e.to]>d2 && dist[e.to]<d2)
    {
    dist2[e.to]=d2;
    que.push(P(dist2[e.to],e.to));
    }
    }
    }
    }

Roadblocks(POJ 3255)的更多相关文章

  1. MST:Roadblocks(POJ 3255)

       路上的石头 题目大意:某个街区有R条路,N个路口,道路双向,问你从开始(1)到N路口的次短路经长度,同一条边可以经过多次. 这一题相当有意思,现在不是要你找最短路径,而是要你找次短路经,而且次短 ...

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

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

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

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

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

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

  5. 次最短路径 POJ 3255 Roadblocks

    http://poj.org/problem?id=3255 这道题还是有点难度 要对最短路径的算法非常的了解 明晰 那么做适当的修改 就可以 关键之处 次短的路径: 设u 到 v的边权重为cost ...

  6. poj 3255 Roadblocks

    Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13216 Accepted: 4660 Descripti ...

  7. poj 3255 Roadblocks 次短路(两次dijksta)

    Roadblocks Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  8. POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16425   Accepted: 5797 Descr ...

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

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

随机推荐

  1. 使用 VMware Workstation Pro让 PC 提供云桌面服务——学习笔记(三)

    目标 当在前面两篇博客后,我们已经创建了一个能当服务器的虚拟机,这时我们需要通过复制虚拟机来让创建更多虚拟机 操作步骤 1.创建克隆 这里主要是VMware软件的操作 虚拟机->管理->克 ...

  2. 为什么 java.util.Stack不被官方所推荐使用!

    Java 为什么不推荐使用 Stack 呢? 因为 Stack 是 JDK 1.0 的产物.它继承自 Vector,Vector 都不被推荐使用了,你说 Stack 还会被推荐吗? 当初 JDK1.0 ...

  3. springmvc中get和post区别和应用

    最近对post和get什么时候使用 和 应该怎么使用  他俩的区别  有了一些疑问 根据本人的了解   post是对数据进行了封装保护 get是安全性较差的 可以看到数据的信息    post是用来改 ...

  4. MapReduce框架原理

    MapReduce框架原理 3.1 InputFormat数据输入 3.1.1 切片与MapTask并行度决定机制 1.问题引出 MapTask的并行度决定Map阶段的任务处理并发度,进而影响到整个J ...

  5. 1 Spark概述

     第1章 Spark概述 1.1 什么是Spark Spark是一种快速.通用.可扩展的大数据分析引擎,2009年诞生于加州大学伯克利分校AMPLab,2010年开源,2013年6月成为Apache孵 ...

  6. Ceph的Python接口

    参考文章 ceph的python_api文档: http://docs.ceph.com/docs/master/rados/api/python/ 连接ceph集群 import rados clu ...

  7. 记一次因为Gradle与Lombok不兼容导致编译时的内存溢出 Expiring Daemon because JVM heap space is exhausted

    1.现象 版本 Gradel:6.1.1 / 6.5.1 Lombok:1.8.6 / 1.8.10 截图 解决过程 调大idea的堆内存 不行 × idea安装目录中找到 idea64.exe.vm ...

  8. elementUI的隐藏组件el-scroll--滚动条

    由于原生的滚动条存在兼容性问题并且样式不是太美观,在项目中经常使用的是elementui,发现elementui中有个隐藏的组件,就是element官网使用的滚动条,可以优化滚动条样式.鼠标经过的时候 ...

  9. 接口测试中postman环境和用例集

    postman的环境使用 postman里有环境的设置,就是我们常说的用变量代替一个固定的值,这样做的好处是可以切换不同的域名.不同的环境变量,不同的线上线下账户等等场景.下面就看下怎么用吧. 创建一 ...

  10. go语言之字符串、指针、数组、切片、结构struct、面向对象

    一: 字符串 概述: Go 语言将字符串作为 种原生的基本数据类型,字 符串的初始化可以使用字符串字面量. (1)字符串是常量,可以通过类 数组 索引访问其字节单元,但是不能修改某个字节的值 (2)宇 ...