• 次短路(内附建图模板和次短路模板)


    题目:https://vjudge.net/problem/POJ-3255

    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

    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)
     
     
     
     
    题意:
    n个路口  r条道路
    问从1到n的第二短距离
     
     
     
    思路:次短路问题,,会求最短路了,就直接用队列存取每个点第二小的数dis2【】就行,,但是依然要把每个点的最小路dis【】更新,,模板题,但是建图比较麻烦,当然会了也很简单的
     
           某个顶点v的次短路要么是到其它某个顶点u的最短路再加上u--->v的边   要么是到u的次短路加上u--->v的边
     
     
    以后可能还有n短路,,估计思路也差不多
     
     
    a代码:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<set>
    #include<algorithm>
    #include<map>
    #define maxn 100005
    #define inf 1e9+7
    using namespace std;
    typedef pair<int,int>P;//first 是最短距离 second 顶点编号
    struct edge{
       int to,cost;
    };
    int n,r;
    int  dis[maxn];
    int dis2[maxn];
    vector<edge>mp[maxn];
    priority_queue<P,vector<P>,greater<P> >que;
    void dijkstra(int n,int s)
    {
        memset(dis,inf,sizeof(dis));
        memset(dis2,inf,sizeof(dis2));
        dis[0]=0;
        que.push(P(0,0));
        while(!que.empty())
        {
          P s=que.top();
          que.pop();
          int v=s.second,d=s.first;
          if(dis2[v]<d)continue;
          for(int i=0;i<mp[v].size();i++)
          {
              edge e=mp[v][i];
              int d2=d+e.cost;
              if(dis[e.to]>d2){swap(dis[e.to],d2);  //找最短路 并且把比最短路大一点的dis【e.to】赋值给d2;
              que.push(P(dis[e.to],e.to));   //用队列存下最短路和那个顶点
          }
          if(dis2[e.to]>d2&&dis[e.to]<d2)
          {
              dis2[e.to]=d2;     //找次短路 如果dis2【】<dis[]就可以
              que.push(P(dis2[e.to],e.to));
          }
          }
        }
    }
    int main()
    {
        cin>>n>>r;
        int x;
          edge y;
        for(int i=0;i<r;i++)         //建图(无向图)模板,,建立带权图板子(边上有属性的)
        {
    
            cin>>x>>y.to>>y.cost;
            x--;
            y.to--;
            mp[x].push_back(y);
            swap(y.to,x);
            mp[x].push_back(y);
        }
        dijkstra(n,0);
        cout<<dis2[n-1]<<endl;//因为是从0开始查的,,但是题目是从1开始
        //cout<<dis[n-1]<<endl;  //最短路
        return 0;
    }
    
    
    
    ////   建立不带权板子
    //
    //
    //for(int i=0;i<r;i++)
    //    {
    //
    //        cin>>x>>y;
    //        x--;y--;
    //        mp[x].push_back(y);
    //        mp[y].push_back(x);
    //    }

    主要是题意难理解,

     
     
     
  • 相关阅读:
    linux常用命令总结-updating
    三次握手、四次挥手的理解
    【WPF学习】第二十八章 程序集资源
    【WPF学习】第二十七章 Application类的任务
    【WPF学习】第二十六章 Application类——应用程序的生命周期
    【WPF学习】第二十五章 日期控件
    【WPF学习】第二十四章 基于范围的控件
    【WPF学习】第二十三章 列表控件
    【WPF学习】第二十二章 文本控件
    【WPF学习】第二十一章 特殊容器
  • 原文地址:https://www.cnblogs.com/huangzzz/p/8350850.html
Copyright © 2020-2023  润新知