(次短路) poj 3255

Roadblocks
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8706   Accepted: 3145

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)
 
题意:
 
求次短路。。
 
思路
 
dist1 ,dist2分别为 1,n出发的最短路,然后 枚举 u,v dist[u]+dist[v]+w[u][v]中求出次小就好
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#define INF 100000000
using namespace std;
vector<int> e[5001],w[5001];
int dist1[5001],dist2[5001];
bool vis[5001];
int n,m;
void spfa(int st,int dist[])
{
    queue<int> q;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
        dist[i]=INF;
    dist[st]=0;
    vis[st]=1;
    q.push(st);
    while(!q.empty())
    {
        int x;
        x=q.front(),q.pop();
        vis[x]=0;
        for(int i=0;i<e[x].size();i++)
        {
            int v=e[x][i];
            if(dist[x]+w[x][i]<dist[v])
            {
                dist[v]=dist[x]+w[x][i];
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
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);
        e[x].push_back(y);
        e[y].push_back(x);
        w[x].push_back(z);
        w[y].push_back(z);
    }
    spfa(1,dist1);
    spfa(n,dist2);
    int ans1,ans2;
    ans1=ans2=INF;
    for(int u=1;u<=n;u++)
    {
        for(int j=0;j<e[u].size();j++)
        {
            int v=e[u][j];
            if(dist1[u]+dist2[v]+w[u][j]<ans1)
            {
                ans2=ans1;
                ans1=dist1[u]+dist2[v]+w[u][j];
            }
            else if(dist1[u]+dist2[v]+w[u][j]<ans2&&dist1[u]+dist2[v]+w[u][j]!=ans1)
            {
                ans2=dist1[u]+dist2[v]+w[u][j];
            }
        }
    }
    printf("%d
",ans2);
    return 0;
}

  

原文地址:https://www.cnblogs.com/water-full/p/4475350.html