没什么特别的,最短路径算法,用Floyd是为了简单

Code

/**//*
ID: sdjllyh1
PROG: comehome
LANG: JAVA
complete date: 2008/12/25
complexity: O(P)
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/

import java.io.*;
import java.util.*;

public class comehome


{
private static int[][] distance = new int[52][52];//最短路径,a对应点0,z对应25,A对应26,Z对应51
private static int shortestCow;
private static int shortestDistance;

public static void main(String[] args) throws IOException

{
init();
run();
output();
System.exit(0);
}

private static void run()

{
computeShortestPaths();

shortestDistance = Integer.MAX_VALUE;
for (int i = 26; i < 51; i++)

{
if (shortestDistance > distance[i][51])

{
shortestDistance = distance[i][51];
shortestCow = 'A' + i - 26;
}
}
}

private static void computeShortestPaths()

{
for (int k = 0; k < 52; k++)
for (int i = 0; i < 52; i++)
for (int j = 0; j < 52; j++)

{
distance[i][j] = Math.min(distance[i][j], distance[i][k] + distance[k][j]);
}
}

private static void init() throws IOException

{
BufferedReader f = new BufferedReader(new FileReader("comehome.in"));
for (int i = 0; i < 52; i++)

{
for (int j = 0; j < 52; j++)

{
if (i != j)

{
distance[i][j] = Integer.MAX_VALUE / 2 - 1;
}
}
}

int n = Integer.parseInt(f.readLine());
for (int i = 0; i < n; i++)

{
String st = f.readLine();
int pasture1, pasture2;
int dis = Integer.parseInt(st.substring(4));
if (Character.isUpperCase(st.charAt(0)))

{
pasture1 = st.charAt(0) - 'A' + 26;
}
else

{
pasture1 = st.charAt(0) - 'a';
}
if (Character.isUpperCase(st.charAt(2)))

{
pasture2 = st.charAt(2) - 'A' + 26;
}
else

{
pasture2 = st.charAt(2) - 'a';
}
distance[pasture1][pasture2] = Math.min(distance[pasture1][pasture2], dis);
distance[pasture2][pasture1] = Math.min(distance[pasture2][pasture1], dis);
}
f.close();
}

private static void output() throws IOException

{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("comehome.out")));
out.println((char)shortestCow + " " + shortestDistance);
out.close();
}
}









































































































































