[codevs1021] Marika

Source: Internet
Author: User

[codevs1021] Marika

Question Description

Mike got a new girlfriend and Marika was very angry with him and was looking for revenge.

Because she and they live in the same city, she began to prepare for her long journey.

There is a maximum of one route between every two cities in this country, and we know the time it takes to get from one city to another.

Mike overheard in the car that there was a road being repaired, and there was a traffic jam, but didn't hear exactly which way. No matter which route is being repaired, the city where Mike is located can be reached from the city where Marika is located.

Marika will only pass on the road from traffic jams, and she will be driving on the shortest route. Mike wants to know how long it will take for Marika to reach his city in the worst case, so he can make sure his girlfriend is far enough away from the city.

Write a program to help Mike figure out the maximum time (in minutes) that Marika will need to reach his city by the shortest route through the road without traffic jams.

Input

The first line has two numbers of N and M separated by spaces, each representing the number of cities and the number of roads between cities. 1≤n≤1000,1≤m≤n* (N-1)/2. The city is labeled with numbers 1 to N, and Mike is in City 1, Marika in city N.

Each row in the next m row contains three numbers, a, B, and V, separated by spaces. Which 1≤a,b≤n,1≤v≤1000. These numbers indicate that there is a two-way road between A and City B and can be passed within V minutes.

Output

Output file in the first line of the maximum time in minutes, in this time, no matter which road in the traffic jam, Marika should be able to reach Mike, if less than this time, there must be a road, the road once the traffic jam, Marika will not be able to arrive at Mike.

Input example

5 7 1 2 8 1 4 Ten 2 3 9 2 4 Ten 2 5 1 3 4 7 3 5 Ten

Output example

27

Data size and conventions

See " Input "

Exercises

Run from N to the shortest way, and then start back from 1 to find the "most short-circuit diagram", that is, to walk the most short-circuit must pass the edges and points. And then in this "shortest path" above the "bridge", that is, a side to meet the deletion of it, "the shortest-circuit diagram" will become two connected components, obviously only in the original image of the bridge can be deleted only to make it possible to long, then we enumerate to delete which bridge, and then run the shortest way, the final maximum is good

#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack > #include <vector> #include <queue> #include <cstring> #include <string> #include <map > #include <set>using namespace std;const int buffersize = 1 << 16;char buffer[buffersize], *head, *TAIL;INL        ine Char Getchar () {if (Head = = Tail) {int L = fread (buffer, 1, buffersize, stdin);    Tail = (Head = buffer) + L; } return *head++;}    int read () {int x = 0, f = 1; char c = Getchar ();    while (!isdigit (c)) {if (c = = '-') f =-1; c = Getchar ();}    while (IsDigit (c)) {x = x * + C-' 0 '; c = Getchar ();} return x * f;} #define MAXN 1010#define maxm 1000010#define oo 2147483647int N, M, HEAD[MAXN], NEXT[MAXM], TO[MAXM], dist[maxm];void Adde Dge (int A, int b, int c) {To[++m] = b; dist[m] = c; next[m] = Head[a]; Head[a] = M;swap (A, b); to[++m] = b; dist[m] = c; NE XT[M] = Head[a]; Head[a] = M;return;} struct Node {int u, D; NOde () {}node (int _, int __): U (_), D (__) {}bool operator < (const node& t) const {return d > t.d;}}  ;p riority_queue <Node> q;bool vis[maxn], cant[maxm];int d[maxn];void Dijkstra (int s) {for (int i = 1; I <= n; i++) D[i] = oo;memset (Vis, 0, sizeof (VIS));d [s] = 0; Q.push (Node (s, 0)); Q.empty ()) {int u = q.top (). u; Q.pop (); if (Vis[u]) continue;vis[u] = 1;for (int e = head[u]; e; e = next[e]) if (!cant[e] && d[to[e]] > d[u] + di St[e]) {d[to[e]] = D[u] + dist[e];if (!vis[to[e]]) Q.push (Node (To[e], D[to[e]]));}} return;} BOOL ON[MAXM], Vis2[maxn];queue <int> q;void Get (int s) {Q.push (s); Vis2[s] = 1;while (!q.empty ()) {int u = Q.front () ; Q.pop (); for (int e = head[u]; e; e = next[e]) if (D[to[e]] + dist[e] = = D[u] &&!vis2[to[e]]) {vis2[to[e]] = 1;on[e] = On[e+ ((e&1) 1:-1)] = 1;q.push (To[e]);}} return;} int CLO, PRE[MAXN], LOW[MAXN], BRI[MAXN], cb;void build (int u, int fa, int eid) {Low[u] = Pre[u] = ++clo;for (int e = head[ U]; E e = Next[e]) if (on[E] && to[e]! = FA) {if (!pre[to[e]]) {build (To[e], u, e); Low[u] = min (Low[u], low[to[e]]);} else Low[u] = min (Low[u], pre[to[e]]);} if (low[u] = = Pre[u] && eid) BRI[++CB] = Eid;return;} int main () {n = read (); int m = read (); for (int i = 1; I <= m; i++) {int a = read (), B = Read (), C = Read (); Addedge (A, B, c);} Dijkstra (n); Get (1); build (1, 0, 0); int ans = 0;for (int i = 1; I <= CB; i++) {int t = (Bri[i] & 1)? 1: -1;cant[bri[i]] = cant[b Ri[i]+t] = 1;dijkstra (n); ans = max (ans, d[1]); Cant[bri[i]] = cant[bri[i]+t] = 0;} printf ("%d\n", ans); return 0;}

[codevs1021] Marika

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.