POJ 2387 Shortest Path Dijkstra algorithm

Source: Internet
Author: User

Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 33607 Accepted: 11383

Description

Bessie is out of 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 she beauty sleep, so she wants to get back as quickly as possible.

Farmer John ' s field has N (2 <= n <=) 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 isn't confident of her navigation ability, so she's always stays on a trail from its start to its end once she start S it.

Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It's 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 and 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 this 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 is five landmarks.

OUTPUT DETAILS:

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

Source

Usaco 2004 November
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace STD; #define N 1005#define INF 0x3f3f3f3fint g[n][n],d[n],vis[n];int main () {int n,m,a,b,val;while (scanf ("%d%d", &m, &n) ==2) {memset (g,inf,sizeof (G)), while (m--) {scanf ("%d%d%d", &a,&b,&val), if (Val<g[a][b]) g[a][b ]=g[b][a]=val;} Memset (d,0,sizeof (d)), memset (vis,0,sizeof (VIS)), for (int i=1;i<=n;i++) d[i]= (I==n?0:inf), for (int i=1;i<=n;i+ +) {int x=n,mm=inf;for (int y=1;y<=n;y++) if (!vis[y]&&d[y]<=mm) mm=d[x=y];vis[x]=1;for (int y=1;y<=n; y++) D[y]=min (D[y],d[x]+g[x][y]);} printf ("%d\n", D[1]);} return 0;}

POJ 2387 Shortest Path Dijkstra algorithm

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.