zoukankan      html  css  js  c++  java
  • 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]);
        } 
        
    }
  • 相关阅读:
    js动态添加select菜单 联动菜单
    用户控件的缓存技术之一【共三篇】
    删除根节点下的所有子节点
    兼容FF和IE的tooltip 鼠标提示框
    【ABAP系列】SAP ABAP常用正则表达式大全
    【ABAP系列】SAP ABAP中关于commit的一点解释
    【SD系列】SAP SD模块-创建供应商主数据BAPI
    【SQL系列】从SQL语言的分类谈COMMIT和ROLLBACK的用法
    【ABAP系列】SAP ABAP控制单元格是否可编辑
    【ABAP系列】SAP ABAP 关于BAPI的EXTENSIONIN 一点解释
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11296624.html
Copyright © 2011-2022 走看看