POJ 3255 —— 次短路dijkstra

Roadblocks
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5828   Accepted: 2201

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)

Source

USACO 2006 November Gold

Dijkstra 同时去维护最短路和次短路

PS:题目中给出的是双向边。。。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 5000 + 50;
const int MAXS = 100000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-8
const int MOD = (int)1e9 + 7;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int n , m;
struct edge
{
    int to , cost;
    edge(){}
    edge(int v , int w){to = v ; cost = w;}
};
vector <edge> G[MAXN];
int dis[MAXN] , dis2[MAXN];
void addedge(int u , int v , int cost)
{
    G[u].push_back(edge(v , cost));
}
void solve()
{
    priority_queue < pii , vector<pii> , greater<pii> > pq;
    fill(dis , dis + n , inf);
    fill(dis2 , dis2 + n , inf);
    dis[0] = 0;
    pq.push(pii(0 , 0));
    while(!pq.empty())
    {
//        outstars
        pii p = pq.top() ; pq.pop();
        int v = p.second , d = p.first;
        if(dis2[v] < d)continue;
//        outstars
        for(int i = 0 ; i < G[v].size() ; i++)
        {
//            outstars
            edge &e = G[v][i];
            int d2 = d + e.cost;
            if(dis[e.to] > d2)
            {
                swap(dis[e.to] , d2);
                pq.push(pii(dis[e.to] , e.to));
            }
            if(dis2[e.to] > d2 && dis[e.to] < d2)
            {
                dis2[e.to] = d2;
                pq.push(pii(dis2[e.to] , e.to));
            }
//            Bug(dis2[e.to]);
        }
    }
    printf("%d\n" , dis2[n - 1]);
}
int main()
{
    while(~scanf("%d%d" , &n , &m))
    {
        clr(G , 0);
        for(int i = 0;  i < m ; i++)
        {
            int u ,v ,w;
            scanf("%d%d%d" , &u , &v , &w);
            addedge(u - 1, v - 1, w);
            addedge(v - 1, u - 1, w);
        }
//        for(int i = 0 ; i < n ; i++)
//        {
//            for(int j = 0 ; j < G[i].size() ; j++)
//            {
//                printf("%d ->  %d : %d\n" , i , G[i][j].to , G[i][j].cost);
//            }
//        }
        solve();
    }
    return 0;
}


你可能感兴趣的:(ACM,次短路)