博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3255 Roadblocks 最短路Dijkstra+堆优化
阅读量:4113 次
发布时间:2019-05-25

本文共 3834 字,大约阅读时间需要 12 分钟。

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 
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
错因:没有熟练掌握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
P; priority_queue

,greater

> q; (2).赋初值fill函数的使用 fill(dist,dist+5005,inf);                   (3).邻接表的使用                        vector

G[5005]; #include
#include
#include
#include
#include
#include
#include
using namespace std; #define inf 1e9 typedef pair
P; struct edge{ int t,c; }; vector
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

,greater

> 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
d2) {
swap(dist[e.t],d2); q.push(P(dist[e.t],e.t)); } if(dist2[e.t]>d2&&dist[e.t]

转载地址:http://epgsi.baihongyu.com/

你可能感兴趣的文章
Valid Parentheses --括号匹配
查看>>
Remove Element--原地移除重复元素
查看>>
Remove Duplicates from Sorted Array--从有序数组中移除重复元素
查看>>
Count and Say
查看>>
Gas Station
查看>>
Palindrome Partitioning --回文切割 深搜(重重)
查看>>
Valid Palindrome 简单的回文判断
查看>>
Pascal's Triangle -- 生成杨辉三角
查看>>
Pascal's Triangle II 生成杨辉三角中的某行
查看>>
Minimum Depth of Binary Tree -- 二叉树的最小深度 DFS 加剪枝
查看>>
Climbing Stairs 爬楼梯方法 动态规划
查看>>
Merge Two Sorted Lists 合并两个有序链表
查看>>
pow(x,n) 为什么错这么多次
查看>>
Jump Game 动态规划
查看>>
Binary Tree Maximum Path Sum 自底向上求解(重重重重)
查看>>
Subsets 深搜
查看>>
Subsets II
查看>>
Edit Distance 字符串距离(重重)
查看>>
Gray Code 格雷码
查看>>
对话周鸿袆:从程序员创业谈起
查看>>