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)
     
     
    题解:
      次短路裸题,我们考虑次短路只有可能是某条最短路dis[u]加上边u到v得来的,或者是某条次短路dis2[u]加上边u到v得来的,所以,考虑spfa时顺便更新次短路,顺便用最短了松弛次短路,用次短路松弛次短路,无论哪个松弛成功都加入栈。
    代码:
    #include<iostream>
    #include<cstring>
    #include<stdlib.h>
    #include<stdio.h>
    #include<algorithm>
    #include<queue>
    #define MAXN 300000
    using namespace std;
    struct edge{
        int first;
        int next;
        int quan;
        int to;
    }a[MAXN*2];
    queue<int> q;
    int dis[MAXN],dis2[MAXN],have[MAXN];
    int n,m,num=0;
    
    void addedge(int from,int to,int quan){
        a[++num].to=to;
        a[num].quan=quan;
        a[num].next=a[from].first;
        a[from].first=num;
    }
    
    void spfa(){
        memset(have,0,sizeof(have));
        memset(dis,127,sizeof(dis));
        memset(dis2,127,sizeof(dis2));
        while(!q.empty()) q.pop();
        q.push(1);
        dis[1]=0;
        while(!q.empty()){
            int now=q.front();
            q.pop();
            have[now]=0;
            for(int i=a[now].first;i;i=a[i].next){
                int to=a[i].to,quan=a[i].quan;
                int diss=dis[now]+quan,diss2=dis2[now]+quan;
                if(dis[to]>diss){
                    swap(dis[to],diss);
                    if(!have[to]) {have[to]=1,q.push(to);}
                }
                if(dis2[to]>diss&&dis[to]<diss){
                    dis2[to]=diss;
                    if(!have[to]) {have[to]=1,q.push(to);}
                }
                if(dis2[to]>diss2&&dis[to]<diss2){
                    dis2[to]=diss2;
                    if(!have[to]) {have[to]=1;q.push(to);}
                }
            } 
        }
    }
    
    int main(){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            addedge(x,y,z),addedge(y,x,z);
        }
        spfa();
        printf("%d",dis2[n]);
    }
  • 相关阅读:
    ubuntu 14.04 下试用Sublime Text 3
    闲来无事,温习一下快速排序法
    学艺不精,又被shell的管道给坑了
    ssh登录失败处理步骤
    linux文件权限整理
    使用ssh远程执行命令批量导出数据库到本地
    leetcode-easy-design-384 Shuffle an Array
    leetcode-easy-dynamic-198 House Robber-NO
    leetcode-easy-dynamic-53 Maximum Subarray
    leetcode-easy-dynamic-121 Best Time to Buy and Sell Stock
  • 原文地址:https://www.cnblogs.com/renjianshige/p/7460455.html
Copyright © 2011-2022 走看看