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;
    }
  • 相关阅读:
    Android开发之 Android 的基本组件的概述
    Android开发之 Android应用程序详细解析
    Android开发之 Android应用程序目录结构解析
    第七篇 :微信公众平台开发实战Java版之如何获取微信用户基本信息
    Android 开发之Windows环境下Android Studio安装和使用教程(图文详细步骤)
    Android开发之 Windows环境下通过Eclipse创建的第一个安卓应用程序(图文详细步骤)
    Android 开发之 Android 开发的起步
    Github学习进阶-初露锋芒,通过命令行将本地git仓库推送到Github上面的仓库
    Windows环境下maven 安装与环境变量配置
    Github学习之路-小试牛刀,练习Git 的基本操作
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5763961.html
Copyright © 2011-2022 走看看