1. 程式人生 > >poj 3255 Roadblocks (次短路 A星演算法)

poj 3255 Roadblocks (次短路 A星演算法)

Language:Default

Roadblocks

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 20256 Accepted: 7107

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)

相關推薦

poj 3255 Roadblocks 短路 A演算法

題目連結:http://poj.org/problem?id=3255 Language:Default Roadblocks Time Limit: 2000MS

poj 3255 Roadblocks 短路 A演算法

Language:Default Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20256 Accepted: 7107

POJ 3255 Roadblocks短路模板題

http://poj.org/problem?id=3255 寫了還算多的最短路題目發現,沒寫過次短路。順著挑戰刷,就看到了次短路,然後發現一臉懵逼。 其實理解了後發現超級簡單。次短路,每個點都記錄兩個距離就好了,最短的和次短的。然後最後輸出第n個點的次短的即可。 程式碼如

poj 3255 Roadblocks最短路徑

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, be

POJ - 3255Roadblocks短路 Dijkstra演算法

Roadblocks 直接翻譯了 Descriptions Bessie搬到了一個新的農場,有時候他會回去看他的老朋友。但是他不想很快的回去,他喜歡欣賞沿途的風景,所以他會選擇次短路,因為她知道一定有一條次短路。這個鄉村有R(1<=R<=100000)條雙向道路,每一條連線N(1<=N

POJ 3255 Roadblocks Dijkstra求最短路徑的變形Dijkstra求短路徑

ber emp accept backtrack cau scrip 直接 hang input Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16425 Ac

洛谷P2865 [USACO06NOV]路障Roadblocks短路

題目描述 貝茜把家搬到了一個小農場,但她常常回到FJ的農場去拜訪她的朋友。貝茜很喜歡路邊的風景,不想那麼快地結束她的旅途,於是她每次回農場,都會選擇第二短的路徑,而不象我們所習慣的那樣,選擇最短路。 貝茜所在的鄉村有RRR(1&lt;=R&lt;

USACO2007NOV——短路徑Roadblocks短路

描述 貝西所在的牧場一共有 N 個地點。 M 條雙向通行的道路連線這些地點,其中第 i 條道路連線 Ai 和 Bi,長度為 Li。貝西想從第一個地點走到第 N 個地點,由於路上風景不錯,她決定不走最短 路徑,而選擇次短路徑。次短路徑的長度嚴格大於最短路徑。如果

A*演算法A演算法DFS BFS

BFS演算法過程: 1.取出佇列首元素作為當前節點,檢查是否為目的地。 2.對當前點進行展開,檢查是否與佇列重複,依次進入佇列。 3.當隊首指標與隊尾指標重合時,表示所有路徑已經探索完畢。 優點:獲得最短路徑(探索深度而不是權值) 缺點:極為巨大的記憶體需求(能夠高達幾十M

CCF-交通規劃短路變形->dj演算法

問題描述   G國國王來中國參觀後,被中國的高速鐵路深深的震撼,決定為自己的國家也建設一個高速鐵路系統。   建設高速鐵路投入非常大,為了節約建設成本,G國國王決定不新建鐵路,而是將已有的鐵路改造

POJ-3255 Roadblocks---Dijkstra隊列優化+短路

continue sin bsp 無向圖 -s http IT 如果 pro 題目鏈接: https://vjudge.net/problem/POJ-3255 題目大意: 給無向圖,求1到n的次短路長度 思路: 由於邊數較多,應該使用dijkstra的隊列優化 用d數組存

POJ 3255 Roadblocks(短路,Dijkstra變形+鄰接表儲存)

POJ 3255 Roadblocks Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want t

poj 3255 Roadblocks 短路 spfa 解題報告

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, be

POJ 3255短路+SPFA

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, be

POJ-3255 Roadblocks(短路)

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 o

poj 3463 Sightseeing短路短路

Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way,

POJ 3463 Sightseeing 短路&短路條數問題

題意:給一個有向圖,求從s到f 的最短路+最短路-1的條數。   有重邊。 分析: 程式碼是根據挑戰的次短路改編的。      具體請看程式碼。 #include<iostream> #include<algorithm> #include&l

poj 3463 Sightseeing短路&&短路

Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way,

POJ 3255 Roadblocks(短路徑問題

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, be

POJ3255 Roadblocks短路

back sizeof each field second r+ esp eat string Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7760