poj 3255 Roadblocks 次短路 spfa 解题报告

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)

思路:先算出每个点到1的最短路dis[i],再算出每个点到n点的最短路disr[i],然后枚举所有边,计算次短路dis[u]+disr[v]+edge[u][v]

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#define MAXN 5010
#define MAXM 101000
#define INF 0x3f3f3f3f
using namespace std;

struct node
{
    int v, w, next;
} edge[MAXM * 2];

int n, m, e, p, q;
int vis[MAXN], dis[MAXN], disr[MAXN], head[MAXN];

void insert(int x, int y, int w)
{
    edge[e].v = y;
    edge[e].w = w;
    edge[e].next = head[x];
    head[x] = e++;
}

void spfa(int src, int d[])
{
    memset(vis, 0, sizeof(vis));
    vis[src] = 1;
    d[src] = 0;
    queue Q;
    Q.push(src);
    while(!Q.empty())
    {
        p = Q.front();
        Q.pop();
        vis[p] = 0;
        for(int i = head[p]; i != -1; i = edge[i].next)
        {
            int v = edge[i].v;
            int w = edge[i].w;
            if(d[v] > d[p] + w)
            {
                d[v] = d[p] + w;
                if(!vis[v])
                {
                    vis[v] = 1;
                    Q.push(v);
                }
            }
        }
    }
}

int main()
{
    int a, b, c;
    while(scanf("%d%d", &n, &m) != EOF)
    {
        e = 0;
        memset(head, -1, sizeof(head));
        memset(dis, 0x3f, sizeof(dis));
        memset(disr, 0x3f, sizeof(dis));
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &a, &b, &c);
            insert(a, b, c);
            insert(b, a, c);
        }
        spfa(1, dis);
        spfa(n, disr);
        int mini = dis[n];
        int ans = INF;
        for(int i = 1; i <= n; i++)
        {
            for(int j = head[i]; j != -1; j = edge[j].next)
            {
                int v = edge[j].v;
                int w = edge[j].w;
                if(dis[i] + disr[v] + w > mini && dis[i] + disr[v] + w < ans)
                    ans = dis[i] + disr[v] + w;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(最短路,图论)