Roadblocks

題目

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

題解&分析

一道次短路的板題,Dijkstra如果可以鬆弛就進隊

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
#define ll long long
const int MAXN = 5003;
int n , m;
struct node{
    int v;
    ll w;
    node(){}
    node( int V , ll W ){
        v = V;
        w = W;
    }
};
vector<node>G[MAXN];
struct edge{
    int x;
    ll w;
    friend bool operator < ( edge a , edge b ){
        return a.w > b.w;
    }
};
bool vis[MAXN];
ll dis[MAXN] , dis2[MAXN];
void Dijk(){
    priority_queue<edge>q;
    memset( dis , 0x7f ,sizeof( dis ) );
    memset( dis2 , 0x7f, sizeof( dis2) ) ;
    edge tx;tx.x = 1 , tx.w = 0;
    dis[1] = 0;
    q.push( tx );
    while( !q.empty() ){
        tx = q.top();
        q.pop();
        int x = tx.x;
        for( int i = 0 ; i < G[x].size() ; i ++ ){
            int v = G[x][i].v ;ll w = G[x][i].w;
            if( dis[v] > tx.w + w ){
                dis2[v] = dis[v];
                dis[v] = tx.w + w;
                edge nx;nx.x = v , nx.w = dis[v];
                q.push( nx );
            }
            else if( dis2[v] > tx.w + w && tx.w + w > dis[v] ){
                dis2[v] = tx.w + w;
                edge nx;nx.x = v , nx.w = dis2[v];
                q.push( nx );
            }
        }
    }
}
int main()
{
    scanf( "%d%d" , &n , &m );
    for( int i = 1 ; i <= m ; i ++ ){
        int x , y;ll z;
        scanf( "%d%d%lld" , &x , &y , &z );
        G[x].push_back( node( y , z ) );
        G[y].push_back( node( x , z ) ) ;
    }
    Dijk();
    printf( "%lld\n" , dis2[n] );
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章