# C - Til the Cows Come Home

D e s c r i p t i o n \color{blue}{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.
I n p u t \color{blue}{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 \color{blue}{}

• Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.

H i n t \color{blue}{Hint}
INPUT DETAILS:

There are five landmarks.

OUTPUT DETAILS:

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

• 注意 题目中说 Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. 表明两个点之间可能有多条路径。要注意路径长度，只记录下最短的那一条。
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main{
/*
* [7.27~8.2]最短路专题
* C - Til the Cows Come Home
* Dijkstra:
*/
static int n,m,a,b,c,t,len;
static int d[],g[][];
static final int INF=(int)1e6,M=(int)1e3+10;
static boolean []vis;

public static void main(String []args) {
Scanner sc=new Scanner(System.in);
g=new int [M][M];
d=new int [M];
vis=new boolean [M];
while(sc.hasNext()) {
t=sc.nextInt();n=sc.nextInt();
init();
for(int i=0;i<t;i++) {
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(c<g[a][b])
g[a][b]=g[b][a]=c;
}
Dijkstra(1);
System.out.println(d[n]);
}
}
static void init() {
for(int i=0;i<=n;i++) {
for(int j=0;j<=n;j++) {
g[i][j]=INF;
}
g[i][i]=0;
}
}
static void Dijkstra(int s) {
for(int i=1;i<=n;i++) {
d[i]=g[s][i];
vis[i]=false;
}
d[s]=0;
vis[s]=true;
int j,u,v;
while(true) {
v=-1;
for(u=1;u<=n;u++) {
if(!vis[u]&&(v==-1||d[u]<d[v])) v=u;
}
if(v==-1) break;
vis[v]=true;
for(u=1;u<=n;u++) d[u]=Math.min(d[u], d[v]+g[v][u]);
}
}
}
/*
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100

90
*/


08-14 51
07-29 53 06-15 54
01-22 130
08-14 2266
05-24 77
07-09 331
02-10 532
08-14 105