road blocks

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: <i>N</i> and <i>R</i> <br>Lines 2..<i>R</i>+1: Each line contains three space-separated integers: <i>A</i>, <i>B</i>, and <i>D</i> that describe a road that connects intersections <i>A</i> and <i>B</i> and has length <i>D</i> (1 ≤ <i>D</i> ≤ 5000)

Output

Line 1: The length of the second shortest path between node 1 and node <i>N</i>

Sample Input

 

4 4 1 2 100 2 4 200 2 3 250 3 4 100

Sample Output

 

450

题目思路:寻找次短路

求次短路,可以通过求最短路得到次短路长度
   1到n的次短路长度必然产生于:从1走到x的最短路 + edge[x][y] +  y到n的最短路
  首先预处理好1到每一个节点的最短路,和n到每一个节点的最短路
  然后枚举每一条边作为中间边(x,y)或者(y,x),如果加起来长度大于等于最短路长度则跳过,否则更新
    

  从1走到x的最短路 + edge[x][y] +  y到n的最短路  给dist[n] 比较 找大于dist[n] 且是最小的那一个

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstring>
using namespace std;
#define r 100010
#define inf 0x3f3f3f3f
struct node
{int v,w,next;
}edge[r*2];
int head[r/2];
int dis1[r/2];
int dis2[r/2];
int cnt;
bool vis[r/2];
void init()
{cnt=0;
    memset(head,-1,sizeof(head));
    memset(dis1,0x3f,sizeof(dis1));
    memset(dis2,0x3f,sizeof(dis2));

}
void add(int u,int v,int w)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void spfa(int u,int *dis)
{
    int i,v,w;
    queue<int>q;
    memset(vis,false,sizeof(vis));
    dis[u]=0;
    vis[u]=true;
    q.push(u);
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        vis[u]=false;
        for(i=head[u];i!=-1;i=edge[i].next)
        {v=edge[i].v;
        w=edge[i].w;
        if(dis[v]>dis[u]+w)
      {

       dis[v]=dis[u]+w;
        if(!vis[v])
        {
            vis[v]=true;
            q.push(v);
        }

        }
    }
}
}
int main()
{int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {int i,j,u,v,w;
    init();
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);

        }
        spfa(1,dis1);
        spfa(n,dis2);
        int ans=inf;
        for(i=1;i<=n;i++)
        {
            for(j=head[i];j!=-1;j=edge[j].next)
            {
                 v=edge[j].v;
                w=edge[j].w;
                int temp=dis1[i]+dis2[v]+w;
                if(temp>dis1[n]&&temp<ans)
                {
                    ans=temp;
                }

            }
        }
        printf("%d\n",ans);

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/81120069