Poj 2387 til the cows come home (Shortest Path + Dijkstra)

Source: Internet
Author: User
Til the cows come home
Time limit:1000 ms   Memory limit:65536 K
Total submissions:29550   Accepted:9935

Description

Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. bessie needs her beauty sleep, so she wants to get back as quickly as possible.

Farmer John's field has N (2 <= n <= 1000) landmarks in it, uniquely numbered 1 .. n. landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark n. cows travel in the field using t (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.

Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. it is guaranteed that some such route exists.

Input

* Line 1: two integers: T and N

* Lines 2 .. t + 1: Each line describes a trail as three space-separated integers. the first two integers are the landmarks between which the trail travels. the third integer is the length of the trail, range 1 .. 100.

Output

* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark n to Landmark 1.

Sample Input

5 51 2 202 3 303 4 204 5 201 5 100

Sample output

90

Hint

Input details:

There are five landmarks.

Output details:

Bessie can get home by following trails 4, 3, 2, and 1.

Source

Usaco 2004 November

There are N points, which give the distance from point A to point B. Of course, A and B can reach each other and ask the shortest distance from 1 to n.

Solution: A template question. Note that there is a duplicate edge. The Dijkstra algorithm needs to be considered. This problem can be ignored by Bellman-Ford and spfa.

Code: The experiment proves that the critical data value of this question is 1000. If I open the 1000 dis [0], it will be useless and it will go down... Speechless.

# Include <iostream> # include <algorithm> # include <cstdio> # include <cstring> using namespace STD; # define M 1005 # define INF 9999999 # define min (A, B) (A <B? A: B) int map [m] [m], DIS [m]; int M, N; void Dijkstra () {bool V [m]; int min = inf, s, I, j; memset (v, 0, sizeof V); map [0] [1] = map [1] [0] = 0; // because I want to set a template, it starts from 0. For (I = 0; I <= N; I ++) dis [I] = (I = 0? 0: INF); // The White Book is so simple that I don't know how difficult the Yellow Book is... Is the level different? For (I = 0; I <= N; I ++) {min = inf; For (j = 0; j <= N; j ++) if (! V [J] & dis [J] <min) {min = dis [J]; S = J;} V [s] = 1; for (j = 0; j <= N; j ++) dis [J] = min (DIS [J], DIS [s] + map [s] [J]);} printf ("% d \ n", DIS [N]);} int main () {int I, j; int A, B, Z; scanf ("% d", & M, & N); for (I = 0; I <= N; I ++) for (j = 0; j <= N; j ++) map [I] [J] = (I = J? 0: INF); While (M --) {scanf ("% d", & A, & B, & Z ); map [a] [B] = map [B] [a] = min (Map [a] [B], Z);} Dijkstra (); Return 0 ;}

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.