zoukankan      html  css  js  c++  java
  • POJ 3255 Roadblocks (次短路模板)

    Roadblocks
    Time Limit: 2000MS   Memory Limit: 65536K
         

    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 intersectionN.

    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)
     
    题意:求严格次短路
     
    次短路一定是由最短路上出去经过其他的边在回到最短路上
    所以先求出每个点到1的最短路dis1,每个点到n的最短路dis2
    枚举每一条边
    那么经过这条边u-->v的最短路为dis1[u]+w+dis2[v]
    如果它大于最短路,更新答案
     
    #include<queue>
    #include<cstdio>
    #include<cstring>
    #define N 5001
    #define M 100001
    using namespace std;
    int front[N],to[M<<1],nxt[M<<1],val[M<<1],from[M<<1],tot;
    int dis1[N],dis2[N];
    bool vis[N];
    queue<int>q;
    void add(int u,int v,int w)
    {
        to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; val[tot]=w; from[tot]=u;
        to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; val[tot]=w; from[tot]=v;
    }
    void spfa(int s,int *dis)
    {
        //memset(dis,63,sizeof(dis));
        memset(vis,false,sizeof(vis));
        q.push(s);
        vis[s]=true;
        dis[s]=0;
        int now;
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            vis[now]=false;
            for(int i=front[now];i;i=nxt[i])
                if(dis[to[i]]>dis[now]+val[i])
                {
                    dis[to[i]]=dis[now]+val[i];
                    if(!vis[to[i]])
                    {
                        vis[to[i]]=true;
                        q.push(to[i]);
                    }
                }
        }
    }
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        int u,v,w;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
        }
        memset(dis1,63,sizeof(dis1));
        memset(dis2,63,sizeof(dis2));
        spfa(1,dis1);
        spfa(n,dis2);
        int minn=dis1[n],tmp,ans=0x7fffffff;
        for(int i=1;i<=tot;i++)
        {
            u=from[i]; v=to[i];
            tmp=dis1[u]+val[i]+dis2[v];
            if(tmp>minn && tmp<ans) ans=tmp;
        }
        printf("%d",ans);
    }
  • 相关阅读:
    hdu 2141 二分搜索
    什么是你的核心竞争力之一?
    Ubuntu系统开机后显示器提示“不能显示此视频模式,请将电脑显示输入设置为1920×1080@60Hz”
    [置顶] java高级工程师struts的内部运行机制详解
    一步步理解Linux进程(3)内核中进程的实现
    2013年4月19日佳都新太笔试题+解答
    Android 通过Service单独进程模仿离线推送 Server Push
    缓存研究
    解决MDK4以上版本没法对STM32软件仿真
    windows调试器之Visual C++
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/7416264.html
Copyright © 2011-2022 走看看