1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #include<bits/stdc++.h> using namespace std;
deque<int> x; deque<int> y;
deque<int> x_now; deque<int> y_now;
int n; double shortest;
map<int, bool> flag; deque<int> final_edge;
double calculate( int x1, int y1, int x2, int y2 ){ return sqrt( pow( ( x1 - x2 ) , 2 ) + pow( ( y1 - y2 ) , 2 ) );
}
void dfs( int depth, double path ){
if( depth == n ){ if( path < shortest ){ shortest = path; final_edge.clear();
for( int i = 0; i < n; i++ ){ final_edge.push_back( x_now[ i ] ); final_edge.push_back( y_now[ i ] ); } } return; }
for( int i = 0; i < n; i++ ){
if( flag[i] ){ flag[i] = false; x_now[ depth ] = x[i]; y_now[ depth ] = y[i];
if( depth == 0 ){ dfs( depth + 1, 0 ); } else{ dfs( depth + 1, path + 16 + calculate( x_now[ depth ], y_now[ depth ], x_now[ depth - 1 ], y_now[ depth - 1 ] ) ); } flag[i] = true; } } }
int main(){
int num = 1; while( cin >> n && n ){
int edge;
shortest = 2147483647;
for( int i = 0; i < n; i++ ){
cin >> edge; x.push_back(edge);
cin >> edge; y.push_back(edge);
flag.insert( pair<int, bool>( i, true ) ); }
dfs( 0, 0 );
cout << "**********************************************************" << endl; cout << "Network #" << num << endl;
for( int i = 0; i < ( 2 * n ) - 2; i += 2 ){ cout << "Cable requirement to connect (" << final_edge[i] << "," << final_edge[ i + 1 ] << ") to (" << final_edge[ i + 2 ] << "," << final_edge[ i + 3 ] << ") is " << fixed << setprecision(2) << 16 + calculate( final_edge[i], final_edge[ i + 1 ], final_edge[ i + 2 ], final_edge[ i + 3 ] ) << " feet." << endl; }
cout << "Number of feet of cable required is " << fixed << setprecision(2) << shortest << "." << endl;
x.clear(); y.clear();
x_now.clear(); y_now.clear();
flag.erase( flag.begin(), flag.end() ); final_edge.clear();
num++; } }
|