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]);
        } 
        
    }
  • 相关阅读:
    I/O中断处理详细过程
    移动端事件touchstart、touchmove、touchend
    页面刷新整理
    transform:rotate在手机上显示有锯齿的解决方案大全
    CSS3盒模型温故
    CSS3颜色特征温故
    CSS3文本温故
    CSS3背景温故
    怪诞咖啡的简介
    CSS3边框温故
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11296624.html
Copyright © 2011-2022 走看看