<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    精彩的人生

    好好工作,好好生活

    BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
      147 Posts :: 0 Stories :: 250 Comments :: 0 Trackbacks

    聲明:代碼是本人原創(chuàng)作品,請不要作商業(yè)用途。
    轉(zhuǎn)載請注明出處。


    Computer networking requires that the computers in the network be linked.

    This problem considers a ``linear" network in which the computers are chained together so that each is connected to exactly two others except for the two computers on the ends of the chain which are connected to only one other computer. A picture is shown below. Here the computers are the black dots and their locations in the network are identified by planar coordinates (relative to a coordinate system not shown in the picture).

    Distances between linked computers in the network are shown in feet.

    For various reasons it is desirable to minimize the length of cable used.

    Your problem is to determine how the computers should be connected into such a chain to minimize the total amount of cable needed. In the installation being constructed, the cabling will run beneath the floor, so the amount of cable used to join 2 adjacent computers on the network will be equal to the distance between the computers plus 16 additional feet of cable to connect from the floor to the computers and provide some slack for ease of installation.

    The picture below shows the optimal way of connecting the computers shown above, and the total length of cable required for this configuration is (4+16)+ (5+16) + (5.83+16) + (11.18+16) = 90.01 feet.

    Input

    The input file will consist of a series of data sets. Each data set will begin with a line consisting of a single number indicating the number of computers in a network. Each network has at least 2 and at most 8 computers. A value of 0 for the number of computers indicates the end of input.

    After the initial line in a data set specifying the number of computers in a network, each additional line in the data set will give the coordinates of a computer in the network. These coordinates will be integers in the range 0 to 150. No two computers are at identical locations and each computer will be listed once.

    Output

    The output for each network should include a line which tells the number of the network (as determined by its position in the input data), and one line for each length of cable to be cut to connect each adjacent pair of computers in the network. The final line should be a sentence indicating the total amount of cable used.

    In listing the lengths of cable to be cut, traverse the network from one end to the other. (It makes no difference at which end you start.) Use a format similar to the one shown in the sample output, with a line of asterisks separating output for different networks and with distances in feet printed to 2 decimal places.

    Sample Input

    6
    5 19
    55 28
    38 101
    28 62
    111 84
    43 116
    5
    11 27
    84 99
    142 81
    88 30
    95 38
    3
    132 73
    49 86
    72 111
    0

    Sample Output

    **********************************************************
    Network #1
    Cable requirement to connect (5,19) to (55,28) is 66.80 feet.
    Cable requirement to connect (55,28) to (28,62) is 59.42 feet.
    Cable requirement to connect (28,62) to (38,101) is 56.26 feet.
    Cable requirement to connect (38,101) to (43,116) is 31.81 feet.
    Cable requirement to connect (43,116) to (111,84) is 91.15 feet.
    Number of feet of cable required is 305.45.
    **********************************************************
    Network #2
    Cable requirement to connect (11,27) to (88,30) is 93.06 feet.
    Cable requirement to connect (88,30) to (95,38) is 26.63 feet.
    Cable requirement to connect (95,38) to (84,99) is 77.98 feet.
    Cable requirement to connect (84,99) to (142,81) is 76.73 feet.
    Number of feet of cable required is 274.40.
    **********************************************************
    Network #3
    Cable requirement to connect (132,73) to (72,111) is 87.02 feet.
    Cable requirement to connect (72,111) to (49,86) is 49.97 feet.
    Number of feet of cable required is 136.99.





    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;

    public class Problem216 {
        
    static int currentNum = 1;

        
    /**
         * 
    @param args
         
    */

        
    public static void main(String[] args) {
            
    int num=0;
            
    int[][] params = new int[8][2];
            
    int index=0;

            
    try {
                
    while(true){
                    BufferedReader cin 
    = new BufferedReader( new InputStreamReader( System.in ) );
                    String s 
    = cin.readLine();
                    
    if(s==null || s.length()<1)
                        
    continue;
                    
                    
    if(s.equals("0")){
                        
    if(num!=index){
                            System.out.println(
    "param not enough!!!");
                            
    return;
                        }

                        calculate(params, num);
                        
    return;
                    }

                    
                    
    if(isNewPeoject(s)){
                        
    if(num==0){
                            num 
    = Integer.parseInt(s);
                            
    if(num>=2|| num<=8)
                                
    continue;
                            
    else{
                                System.out.println(
    "wrong point num!!");
                                
    return;
                            }

                        }

                        
    if(num!=index){
                            System.out.println(
    "param not enough!!!");
                            
    return;
                        }

                        calculate(params, num);
                        index
    =0;
                        num 
    = Integer.parseInt(s);
                        
    return;
                    }

                    
                    
    if(isParam(s)){
                        params 
    = setParams(params, s, index);
                        index
    ++;
                    }
    else{
                        System.out.println(
    "error param!");
                        
    return;
                    }

                }

            }
     catch (IOException e) {
                
    // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }


        
    private static int[][] setParams(int[][] params, String s, int index) {
            String[] strs 
    = s.split(" ");
            
    int parama=0;
            
    int paramb=0;
            
    for(int i=0; i<strs.length; i++){
                String temp 
    = strs[i];
                
    if(temp== null || temp.length()<1){
                    
    continue;
                }
    else{
                    
    if(parama==0)
                        parama 
    = Integer.parseInt(temp);
                    
    else
                        paramb 
    = Integer.parseInt(temp);
                }

            }

            
            params[index][
    0= parama;
            params[index][
    1= paramb;
            
    return params;
        }


        
    private static boolean isParam(String s) {
            String[] strs 
    = s.split(" ");
            
    if(strs.length<2){
                
    return false;
            }

            
            
    int len=0;
            
    for(int i=0; i<strs.length; i++){
                String temp 
    = strs[i];
                
    if(temp== null || temp.length()<1){
                    
    continue;
                }
    else{
                    len
    ++;
                    
    if(len>2)
                        
    return false;
                    
    if(!isNewPeoject(temp)){
                        
    return false;
                    }

                    
                    
    if(Integer.parseInt(temp) <0 || Integer.parseInt(temp)>150){
                        
    return false;
                    }

                }

            }

            
    return true;
        }


        
    private static boolean isNewPeoject(String s) {
            
    boolean result = true;
            
    for(int i=0; i<s.length(); i++){
                
    if(s.charAt(i)>='0' && s.charAt(i)<='9'){
                    
    continue;
                }
    else{
                    result
    =false;
                    
    break;
                }

            }

            
    return result;
        }


        
    private static void calculate(int[][] params, int num) {
            
    if(num==0)
                
    return;
            
            
    int[][] result = new int[num-1][4];
            
    int[][] source = params;
            
    double[] lenArr = new double[num-1];
            
            ArrayList al 
    = new ArrayList();
            
            
    for(int i= 0; i<num-1; i++){
                al 
    = refreshResult(result, source, i, lenArr, num);
    //            source = refreshSource(source, result, i);
            }

            
            result 
    = (int[][]) al.get(0);
            lenArr 
    = (double[]) al.get(1);
            
            System.out.println(
    "******************************************");
            System.out.println(
    "Network #" + currentNum);
            currentNum
    ++;
            
            
    double tot = 0;
            
    for(int k=0; k<num-1; k++){
                tot 
    += lenArr[k] + 16;
                System.out.println(
    "Cable requirement to connect (" + result[k][0+ "," + result[k][1+ ") to (" + result[k][2+ "," + result[k][3+ ") is " + (lenArr[k]+16+ "feet.");
            }

            System.out.println(
    "Number of feet of cable required is " + tot);
            System.out.println(
    "******************************************");
        }


        
    private static ArrayList refreshResult(int[][] result, int[][] source, int i, double[] lenArr, int num) {
            
    int sp=0;
            
    int tp=0;
            
    double templ=1000000000000000000000000000000000000000000000000000000.0;
            
            
    for(int k=0; k<num; k++){
                
    for(int j=0; j<num; j++){
                    
    if(j==k)
                        
    continue;
                    
    int x1 = source[k][0];
                    
    int y1 = source[k][1];
                    
    int x2 = source[j][0];
                    
    int y2 = source[j][1];
                    
    if(containsInResult(result, x1, y1, x2, y2, i)){
                        
    continue;
                    }

                    
    double len = getLen(x1, y1, x2, y2);
                    
    if(len<templ){
                        templ
    =len;
                        sp
    =k;
                        tp
    =j;
                    }

                }

            }

            
    //after get the shortest points
            
            result[i][
    0= source[sp][0];
            result[i][
    1= source[sp][1];
            result[i][
    2= source[tp][0];
            result[i][
    3= source[tp][1];
            lenArr[i]
    = templ;
            ArrayList al 
    = new ArrayList();
            al.add(
    0, result);
            al.add(
    1, lenArr);
            
    return al;
        }


        
    private static boolean containsInResult(int[][] result, int x1, int y1, int x2, int y2, int i) {
            
    for(int t=0; t<i; t++){
                
    if((result[t][0]==x1 && result[t][1]==y1 && result[t][2]==x2 && result[t][3]==y2) || (result[t][0]==x2 && result[t][1]==y2 && result[t][2]==x1 && result[t][3]==y1)){
                    
    return true;
                }

            }

            
            
    int k=0;
            
    for(int t=0; t<i; t++){
                
    if(result[t][2]==x2 && result[t][3]==y2){
                    k
    ++;
                }

            }

            
    if(k==1)
                
    return true;
            
            k
    =0;
            
    for(int t=0; t<i; t++){
                
    if(result[t][0]==x1 && result[t][1]==y1){
                    k
    ++;
                }

            }

            
    if(k==1)
                
    return true;
            
            
    return false;
        }


        
    private static double getLen(int x1, int y1, int x2, int y2) {
            
    int with = Math.abs(x1-x2);
            
    int high = Math.abs(y1-y2);
            
            
    return Math.sqrt(with*with + high*high);
        }


    }



    歡迎批評(píng)指正





    posted on 2005-12-07 15:17 hopeshared 閱讀(708) 評(píng)論(0)  編輯  收藏 所屬分類: Google Code Jam

    只有注冊用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
    相關(guān)文章:
     
    主站蜘蛛池模板: 2022中文字字幕久亚洲| 一个人免费观看视频www| heyzo亚洲精品日韩| 亚洲第一街区偷拍街拍| 91成人免费在线视频| 亚洲一级视频在线观看| 一级女人18毛片免费| 亚洲欧洲另类春色校园网站| av无码免费一区二区三区| 亚洲中文字幕久久精品无码2021| 国产精品视频免费观看| 日韩亚洲国产高清免费视频| 好吊妞视频免费视频| 偷自拍亚洲视频在线观看| 超pen个人视频国产免费观看| 男人的天堂av亚洲一区2区| 国产a级特黄的片子视频免费| 深夜a级毛片免费无码| 国产午夜亚洲精品理论片不卡 | 五月婷婷在线免费观看| 亚洲乱码一区av春药高潮| 日韩毛片免费无码无毒视频观看 | 无码人妻久久一区二区三区免费丨 | 亚洲国产精品一区二区三区在线观看| 野花高清在线电影观看免费视频 | 国产在线播放线91免费| 亚洲无删减国产精品一区| 成年黄网站色大免费全看| 亚洲av无码一区二区三区天堂 | 亚洲AV无码乱码在线观看| 波霸在线精品视频免费观看| 91亚洲精品视频| 日韩免费无砖专区2020狼| 亚洲精品黄色视频在线观看免费资源| 亚洲AV日韩精品久久久久| 成人性生交大片免费看无遮挡| 老司机午夜精品视频在线观看免费| 久久久久久亚洲精品不卡| 99久久99久久精品免费观看| 亚洲欧美日韩国产成人| 亚洲成av人在线视|