• POJ


    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)
    代码:
    SPFA;
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<map>
    #include<vector>
    #include<cmath>
    #define Inf 0x3f3f3f3f
    
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    
    struct Edge
    {
        int u,v;
        int w;
        int next;
    }edge[200005];
    int head[5005],vis[5005],tot;
    int n,m;
    int  dis[5005][2];
    void init()
    {
        memset(head,-1,sizeof(head));
        memset(vis,0,sizeof(vis));
        memset(dis,Inf,sizeof(dis));
    }
    void add(int u,int v,int w)
    {
        edge[tot].u=u;
        edge[tot].v=v;
        edge[tot].w=w;
        edge[tot].next=head[u];
        head[u]=tot++;
        return;
    }
    
    void SPFA(int s)
    {
        dis[s][0]=0;
        vis[s]=true;
        queue<int>q;
        q.push(s);
        while(!q.empty())
        {
            int now=q.front();
            q.pop();
            vis[now]=false;
            for(int i=head[now];i!=-1;i=edge[i].next)
            {
                if(dis[now][0]+edge[i].w<dis[edge[i].v][0])
                {
                    dis[edge[i].v][1]=dis[edge[i].v][0];
                    dis[edge[i].v][0]= dis[now][0]+edge[i].w;
                    int y=edge[i].v;
                    if(vis[y]==false)
                    {
                      vis[y]=true;
                      q.push(y);
                    }
                }
                if(dis[now][1]+edge[i].w<dis[edge[i].v][1])
                {
                    dis[edge[i].v][1]= dis[now][1]+edge[i].w;
                    int y=edge[i].v;
                    if(vis[y]==false)
                    {
                      vis[y]=true;
                      q.push(y);
                    }
                }
                if(dis[now][0]+edge[i].w>dis[edge[i].v][0]&&dis[now][0]+edge[i].w<dis[edge[i].v][1])
                {
                    dis[edge[i].v][1]= dis[now][0]+edge[i].w;
                    int y=edge[i].v;
                    if(vis[y]==false)
                    {
                      vis[y]=true;
                      q.push(y);
                    }
                }
                
            }
        }
        return ;
    }
    
    int main()
    {
    
        while(scanf("%d%d",&n,&m)!=EOF)
        {
          tot=0;
          init();
          int u,v,w;
          for(int t=0;t<m;t++)
          {
              scanf("%d%d%d",&u,&v,&w);
              add(u,v,w);
              add(v,u,w);
          }
          SPFA(1);
          printf("%d
    ",dis[n][1]);
        } 
        
    }
  • 相关阅读:
    动态库 DLL 封装二:dll封装方法
    动态库 DLL 封装一:dll分类
    C++读取配置文件ini
    WCHAR 字符串拼接
    关于web桌面应用的集成解决方案
    CSS Grid网格布局(转)
    使用 antd 的 form 组件来自定义提交的数据格式
    syslogd日志的一些作用
    我的三年感悟——避免无尽的自我内耗
    《被讨厌的勇气》读后感
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11296624.html
Copyright © 2020-2023  润新知