USACO_2_4_Bessie Come Home

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

ContractedBlock.gif ExpandedBlockStart.gif Code
ExpandedBlockStart.gifContractedBlock.gif/**//*
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
ExpandedBlockStart.gifContractedBlock.gif
{
    
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
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        init();
        run();
        output();
        System.exit(
0);
    }


    
private static void run()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        computeShortestPaths();

        shortestDistance 
= Integer.MAX_VALUE;
        
for (int i = 26; i < 51; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (shortestDistance > distance[i][51])
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                shortestDistance 
= distance[i][51];
                shortestCow 
= 'A' + i - 26;
            }

        }

    }


    
private static void computeShortestPaths()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
for (int k = 0; k < 52; k++)
            
for (int i = 0; i < 52; i++)
                
for (int j = 0; j < 52; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    distance[i][j] 
= Math.min(distance[i][j], distance[i][k] + distance[k][j]);
                }

    }


    
private static void init() throws IOException
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        BufferedReader f 
= new BufferedReader(new FileReader("comehome.in"));
        
for (int i = 0; i < 52; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
for (int j = 0; j < 52; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (i != j)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    distance[i][j] 
= Integer.MAX_VALUE / 2 - 1;
                }

            }

        }


        
int n = Integer.parseInt(f.readLine());
        
for (int i = 0; i < n; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            String st 
= f.readLine();
            
int pasture1, pasture2;
            
int dis = Integer.parseInt(st.substring(4));
            
if (Character.isUpperCase(st.charAt(0)))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                pasture1 
= st.charAt(0- 'A' + 26;
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                pasture1 
= st.charAt(0- 'a';
            }

            
if (Character.isUpperCase(st.charAt(2)))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                pasture2 
= st.charAt(2- 'A' + 26;
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        PrintWriter out 
= new PrintWriter(new BufferedWriter(new FileWriter("comehome.out")));
        out.println((
char)shortestCow + " " + shortestDistance);
        out.close();
    }

}

转载于:https://www.cnblogs.com/SDJL/archive/2008/12/25/1362547.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值