• 原题如下:

    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. C#LeetCode刷题之#169-求众数(Majority Element)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4048 访问. 给定一个大小为 n 的数组,找到其中的众数.众数是 ...

  2. Android 开发学习进程0.13 Androidstudio快捷键 xmlns

    xmlns XML namespace xml命名空间 其中主要是定义xml文件定义位置 前缀有三种,android app tools 后面为唯一标识符URI android 表示为引用自安卓系统 ...

  3. ElementUi——el-select下拉框

    <el-select v-model="ruleForm.status" placeholder="请选择状态" @change="styleC ...

  4. golang 递归自己,输出自己的源代码

    问题: [2min 大家自己想想] 一个程序P运行后能否输出自己的源代码?并且格式保持一致(换行.空格等) 思考: 这个问题的本质是一个递归问题,设有P运行后生成G 既P->G &&am ...

  5. mycat数据库集群系列之mysql主从同步设置

    最近在梳理数据库集群的相关操作,现在花点时间整理一下关于mysql数据库集群的操作总结,恰好你又在看这一块,供一份参考.本次系列终结大概包括以下内容:多数据库安装.mycat部署安装.数据库之读写分离 ...

  6. antd-vue的select组件实现既可以输入添加,又可以下拉选择

    最近,项目中碰到需求,要求任务类型可以从下拉框(后台返回的数据)中选择,也可以手动输入添加新项,项目用的是antd-vue,所以最接近的组件就是a-select组件了,废话不多说,改造方法如下: HT ...

  7. phpstudy后门利用复现

    一.漏洞位置 程序自带的PHP的php_xmlrpc.dll模块中有隐藏后门,受影响的版本有phpstudy2016(php5.2/5.4).phpstudy2018(php5.2/5.4)等版本. ...

  8. 简明python教程--读后感--推荐给python新手

    原书名: A Byte of Python作者:  Swaroop, C. H.译者: 沈洁元出版社: 未知  优点     1. 讲解很详细,很基础,适合入门,对编译器也做了简单的介绍     2. ...

  9. spss如何把多个指标合并成一个变量?

    把多个指标合并成一个变量,通常有两种做法: 一.计算平均值 针对问卷量表数据,同时几个题表示一个维度.比如想要将“我在工作中能获得成就感”.“我可以在工作中发挥个人的才能”这两题合并成一个维度(影响因 ...

  10. windows server 2008 r2 环境下,实现域名和IP同时都能访问一个网站

    有时候,用域名访问能得到一个页面,用IP地址访问也可以得到一个页面,比如 www.baidu.com 和 61.135.169.125 都可以打开百度页面.一开始要实现这种功能,还真有点不知所措,想了 ...