POJ 3255 Roadblocks

来源:互联网 发布:it行业发展前景分析 编辑:程序博客网 时间:2024/06/11 16:54

原题链接
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: A, B, 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)

思路:求次短路径。

AC代码:

#include <iostream>#include <cstdio> #include <algorithm>#include <cstdlib>#include <queue>#include <functional>  //greater#define INF 10000000  //INF为无穷大 using namespace std;//《挑战》 P108 struct edge  //定义边 {    int from,to,cost;};typedef pair<int,int> P;  //first是路径距离d,second是点v vector<edge> G[5002];  //G[v]是从v点出发的所有边 int dist[5002],dist2[5002];  //最短路径和次短路径int N,R;  //点数和边数 int main(){    int i,t;    int A,B,D;  //边的起点,终点,长度     priority_queue<P,vector<P>,greater<P> > que;  //从起点到到任意一点的路径的优先队列    cin>>N>>R;    fill(dist,dist+N,INF);    fill(dist2,dist2+N,INF);    getchar();    for(i=0;i<R;i++){        cin>>A>>B>>D;        getchar();        edge e;        e.from=A-1;        e.to=B-1;        e.cost=D;        G[e.from].push_back(e);        e.from=B-1;        e.to=A-1;        G[e.from].push_back(e);    }     dist[0]=0;    que.push(P(0,0));  //初始化起点的路径距离为0     while(!que.empty()){        P p=que.top();        que.pop();  //从队列中取出最小值        int v=p.second;        int d=p.first;        if(dist2[v]<d)  continue;          //若到v点的次短路径比该路径短,则该路径既不是次短路径也不是最短路径        for(i=0;i<G[v].size();i++){            edge &e=G[v][i];   //从v出发的所有边中的第i个             int d2=d+e.cost;  //到点i的路径距离             if(dist[e.to]>d2){                t=dist[e.to];                dist[e.to]=d2;                d2=t;                que.push(P(dist[e.to],e.to));;            }              if(dist2[e.to]>d2 && dist[e.to]<d2){                dist2[e.to]=d2;                que.push(P(dist2[e.to],e.to));            }         }    }     cout<<dist2[N-1]<<endl;    return 0;}
1 0