0
点赞
收藏
分享

微信扫一扫

POJ 3255 Roadblocks 最短路Dijkstra+堆优化


Roadblocks
       
       
       
       
        
        


Time Limit: 2000MS
 

 

Memory Limit: 65536K
 



Total Submissions: 10618
 

 

Accepted: 3772
 



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)
       
       
Source
       
       
        
        USACO 2006 November Gold

错因:没有熟练掌握STL的应用

解答:1:核心算法:到某点v的次短路只能是到另外一点u的最短路再加上u到v的权值cost[u][v]

因此需要设置两个数组dist[maxn]和dist2[maxn]分别代表达到该点的最短路和次短路

2:易错点:如代码所示不能将dist2[0]的初值也一同设为0,比如假设只有两个点,权值为1,那么次短路显然只能为2,而不是0.。。

3:需要掌握的知识点:

STL知识:

(1).pair的优先队列的使用以及排序

typedef pair<int,int> P;

priority_queue<P,vector<P>,greater<P> > q;

(2).赋初值fill函数的使用

fill(dist,dist+5005,inf);

(3).邻接表的使用

vector<edge> G[5005];

#include<iostream>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define inf 1e9
typedef pair<int,int> P;
struct edge{
int t,c;
};
vector<edge> G[5005];
int dist[5005],dist2[5005];
int main()
{
int n,r;
while(~scanf("%d %d",&n,&r))
{
fill(dist,dist+5005,inf);
fill(dist2,dist2+5005,inf);
        priority_queue<P,vector<P>,greater<P> > q;
int x;
        edge  w;
        dist[0]=0;//2  赋初值dist2[0]=0将出错。
for(int i=1;i<=r;i++)
{
scanf("%d %d %d",&x,&w.t,&w.c);
                x--;
                w.t--;
                G[x].push_back(w);
swap(w.t,x);
                G[x].push_back(w);
}
        q.push(P(0,0));
while(!q.empty())
{
            P p=q.top();
            q.pop();
int v=p.second,d=p.first;
if(d>dist2[v]) continue;
for(int i=0; i<G[v].size(); i++)
{
            edge e=G[v][i];
int d2=d+e.c;
if(dist[e.t]>d2)
{
swap(dist[e.t],d2);
                q.push(P(dist[e.t],e.t));
}
if(dist2[e.t]>d2&&dist[e.t]<d2)
{
                dist2[e.t]=d2;
                q.push(P(dist2[e.t],e.t));
}
}
}
printf("%d\n",dist2[n-1]);
}
return 0;
}


举报

相关推荐

0 条评论