精彩的人生

好好工作,好好生活

BlogJava 首页 新随笔 联系 聚合 管理
  147 Posts :: 0 Stories :: 250 Comments :: 0 Trackbacks

声明:代码是本人原创作品,请不要作商业用途。
转载请注明出处。


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);
    }


}



欢迎批评指正





posted on 2005-12-07 15:17 hopeshared 阅读(679) 评论(0)  编辑  收藏 所属分类: Google Code Jam

只有注册用户登录后才能发表评论。


网站导航: