zoukankan      html  css  js  c++  java
  • Roadblocks_次短路

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 12283   Accepted: 4342

    Description

    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)
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<vector>
    #include<queue>
    using namespace std;
    const int inf=0x7777777;
    const int N=5010;
    struct node
    {
        int u,v,w;
    }g[200010];
    int n,m;
    int dis[2][N],vis[N];
    vector<int>v[N];//N比较大,开二维数组会比较大,所以用vector记录路径
    void init()
    {
        for(int i=1;i<=n;i++) v[i].clear();
        for(int i=1;i<=n;i++)
            dis[0][i]=dis[1][i]=inf;
        dis[0][1]=0,dis[1][n]=0;
    }
    void spfa(int first)
    {
        queue<int>qu;
        memset(vis,0,sizeof(vis));
        int k;
        if(first==1) k=0;else k=1;
        qu.push(first);
        vis[first]=1;
        while(!qu.empty())
        {
            int t=qu.front();
            qu.pop();
            vis[t]=0;
            for(int i=0;i<v[t].size();i++)
            {
                int id=v[t][i];
                int cost=g[id].w,to=g[id].v;
                if(dis[k][to]>dis[k][t]+cost)
                {
                    dis[k][to]=dis[k][t]+cost;
                    if(!vis[to]) {vis[to]=1;qu.push(to);}
                }
    
            }
        }
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            init();
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d%d",&g[i].u,&g[i].v,&g[i].w);
                v[g[i].u].push_back(i);
                g[i+m].u=g[i].v;
                g[i+m].v=g[i].u;
                g[i+m].w=g[i].w;
                v[g[i+m].u].push_back(i+m);//反向的路径记录
            }
            spfa(1);spfa(n);
            int f=dis[0][n];
            int ans=inf;
            for(int i=1;i<=2*m;i++)//一条条边开始遍历,如边(u,v)从1到u的最小路径加上边长加上v到n的最小路径,跳过最小值,剩余路径求最小
            {
                int u=g[i].u,v=g[i].v;
                if(dis[0][u]+g[i].w+dis[1][v]==f) continue;//跳过最小路径,
                ans=min(ans,dis[0][u]+g[i].w+dis[1][v]);
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    25-[jQuery]-ajax
    25-[jQuery]-事件
    24-[jQuery]-属性操作,文档操作
    2016.8.16 JQuery学习记录
    移动端开发之图片上传与显示
    2016.8.16 HTML5重要标签及其属性学习
    2016.8.14 HTML5重要标签以及属性学习
    2016.8.14 HTML5重要标签及其属性学习
    HTML5 重要标签以及属性学习
    HTML5 重要标签及其属性学习
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5763961.html
Copyright © 2011-2022 走看看