Bzoj 1726: [Usaco2006 nov]roadblocks second short circuit (shortest way)

Source: Internet
Author: User

Run the shortest path from the start and end points, then enumerate each edge and update the answer

-----------------------------------------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <queue>#define REP (i, n) for (int i = 0; i < n; i++)#define CLR (x, C) memset (x, C, sizeof (x))#define REP (i, n) for (int i = 1; I <= n; ++i)using namespace std;const INT MAXN = + 5;const int MAXM = INT (1e5) + 5;const int inf = 0X3F3F3F3F;struct Edge {int to, Dist;edge* Next;};edge* head[MAXN];edge* pt;Edge EDGE[MAXM << 1];void init () {pt = EDGE;CLR (head, 0);}void Add (int u, int v, int d) {pt-to = V;PT-dist = D;pt-Next = head[u];head[U] = pt++;} #define Add_edge (U, V, d) Add (U, V, D), add (V, U, D)struct Node {int x, D;BOOL Operator < (const Node &o) Const {return d > o.d;}};priority_queue< Node > Q;int d[2 [MAXN];int cur = 0;void Dijkstra (int S) {CLR (d[cur], INF);d[cur] [S] = 0;Q.push (Node) {S, 0});While (! Q.empty ()) {Node o = q.top ();Q.pop ();int x = o.x, dist = O.D;if (d[cur [x]! = dist)continue;For (edge* e = head[x]; e; e = e = next) {int to = e -to;if (d[cur] [to] > dist + e-dist) {d[cur] [to] = dist + e-dist;Q.push (Node) {To, d[cur] [to]});}}}}int main () {//freopen ("test.in", "R", stdin);init ();int n, m;cin >> n >> m;While (m--) {int U, V, D;scanf ("%d%d%d", &u, &v, &d);u--, v--;Add_edge (U, V, d);}Dijkstra (0);cur ^= 1;Dijkstra (n-1);int MIN = d[cur] [0];int ans = inf;Rep (i, N)For (edge* e = head[i]; e; e = e = next) { int dist = d[0] [i] + d[1 [e-and to] + e-dist;if (Dist > MIN)ans = min (dist, ans);    }cout << ans << "\ n";return 0;}

-----------------------------------------------------------------------------------------------------

1726: [Usaco2006 nov]roadblocks second short circuit time limit: 5 Sec Memory Limit:
Submit: 861 Solved: 414
[Submit] [Status] [Discuss] Description

Bessie moved home to a small farm, but she often went back to FJ's farm to visit her friends. Bessie liked the roadside scenery so much that she did not want to end her journey so quickly, so every time she returned to the farm, she chose the second shortest path, rather than the shortest one, as we used to. Bessie's Village has R (1<=r<=100,000) Two-way roads, each connecting one of the two of all N (1<=n<=5000) farms. Bessie lived on Farm 1, and her friends lived on Farm N (that is, Bessie's destination for every trip). The second short path that Betsy chooses can contain any road that appears in the shortest path, and a road can be repeated several times. Of course, the length of the second short circuit must be strictly greater than the shortest path (there may be more than one), but its length must not be greater than the length of all paths except the shortest route.

Input

* Line 1th: two integers, N and R, separated by a space

* 2nd. R+1 Line: Each line contains three integers a, b, and D separated by spaces, indicating that a road with a length of D (1 <= d <= 5000) is connected to farm A and farm B

Output

* Line 1th: Output An integer, that is, the length of the second short circuit from farm 1 to farm N

Sample Input4 4
1 2 100
2 4 200
2 3 250
3 4 100


Sample Output450

Output Description:

Shortest: 1--2--4 (length 100+200=300)
Second Short circuit: 1, 2, 3, 4 (length 100+250+100=450)
HINT

Source

Gold

Bzoj 1726: [Usaco2006 nov]roadblocks second short circuit (shortest way)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.