[Cpp]
Description: Water question. I will not explain it ......
# Include <iostream>
# Include <cstdio>
# Include <cmath>
# Include <cstring>
Using namespace std;
Double count;
Int n, step [10];
Void dfs (int cur, double sum, int * flag, int (* p) [10])
{
If (cur> n)
{
If (sum <count)
{
Count = sum; www.2cto.com
For (int I = 1; I <= n; I ++) step [I] = flag [I];
}
Return;
}
Else for (int I = 1; I <= n; I ++)
{
Int j;
For (j = 1; j <= n; j ++)
If (flag [j] = I) break;
If (flag [j] = I) continue;
Flag [cur] = I;
If (cur> = 2)
{
Int x = p [0] [flag [cur-1], y = p [1] [flag [cur-1];
Double c = sqrt (p [0] [I]-x) * (p [0] [I]-x) + (p [1] [I]-y) * (p [1] [I]-y ));
Dfs (cur + 1, sum + c, flag, p );
}
Else dfs (cur + 1, sum, flag, p );
Flag [cur] = 0;
}
}
Int main ()
{
# Ifndef ONLINE_JUDGE
Freopen ("a.txt", "r", stdin );
# Endif
Int j (0), num [2] [10], flag [10];
While (scanf ("% d", & n )! = EOF)
{
If (! N) break;
Memset (num, 0, sizeof (num ));
Memset (flag, 0, sizeof (flag ));
Printf ("************************************* * ******************** \ n ");
Printf ("Network # % d \ n", ++ j );
Count = 100000;
For (int I = 1; I <= n; I ++)
Scanf ("% d", & num [0] [I], & num [1] [I]);
Dfs (1, 0, flag, num );
For (int I = 1; I <n; I ++)
{
Int x1 = num [0] [step [I], y1 = num [1] [step [I], x2 = num [0] [step [I + 1], y2 = num [1] [step [I + 1];
Printf ("Cable requirement to connect (% d, % d) to (% d, % d) is %. 2lf feet. \ n ", x1, y1, x2, y2, sqrt (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) + 16 );
}
Printf ("Number of feet of cable required is %. 2lf. \ n", count + (n-1) * 16 );
}
Return 0;
}