USACO 2.4 Cow Tours

TASK: cowtour
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3256 KB]
   Test 2: TEST OK [0.000 secs, 3256 KB]
   Test 3: TEST OK [0.000 secs, 3256 KB]
   Test 4: TEST OK [0.000 secs, 3256 KB]
   Test 5: TEST OK [0.000 secs, 3256 KB]
   Test 6: TEST OK [0.000 secs, 3256 KB]
   Test 7: TEST OK [0.000 secs, 3256 KB]
   Test 8: TEST OK [0.000 secs, 3256 KB]
   Test 9: TEST OK [0.000 secs, 3256 KB]

All tests OK.
1 /*
2 PROG: cowtour
3 ID: jiafeim1
4 LANG: C++
5 */
6
7
8 #include <iostream>
9 #include <fstream>
10 #include <string>
11 #include <iomanip>
12 using namespace std;
13
14 #define sq(x) (((double)x)*(x))
15 #define maxD(x,y) ((x)>(y)?(x):(y))
16 int n;
17 #include <cmath>
18
19 long point[160][3];
20 bool linjie[160][160] = {false};
21 double len[160][160];
22 double longest[160]={0.0};
23 double filed_longest[160]={0.0};
24
25 int main()
26 {
27 ifstream fin("cowtour.in");
28 ofstream fout("cowtour.out");
29
30 fin>>n;
31
32 for(int i = 0 ;i!=n;++i)
33 {
34 fin>>point[i][0]>>point[i][1];
35 }
36
37 for(int i = 0;i!=n;++i)
38 {
39 for(int j = 0 ;j!=n;++j)
40 {
41 if(i!=j)
42 len[i][j] = 999999999999.0;
43 else
44 len[i][j] = 0.0;
45 }
46 }
47 char temp;
48 for(int i = 0;i!=n;++i)
49 {
50 for(int j = 0 ;j!=n;++j)
51 {
52 fin>>temp;
53 if(temp == '1')
54 {
55 linjie[i][j] = true;
56 if(!linjie[j][i])
57 len[j][i] = len[i][j] = sqrt(sq(point[i][0]-point[j][0]) + sq(point[i][1]-point[j][1]));
58 }
59 }
60 }
61 for(int w = 0;w!=n;++w)
62 for(int i = 0;i!=n;++i)
63 for(int j = 0 ;j!=n;++j)
64 {
65 if(len[i][j]>len[i][w]+len[w][j])
66 len[i][j] = len[i][w]+len[w][j];
67 }
68 for(int i = 0;i!=n;++i)
69 for(int j = 0 ;j!=n;++j)
70 {
71 if(longest[i]<len[i][j] && len[i][j]<99999999999.0)
72 longest[i]=len[i][j];
73 }
74
75 for(int i = 0;i!=n;++i)
76 for(int j = 0 ;j!=n;++j)
77 {
78 if(len[i][j]<99999999999.0 && filed_longest[i]<longest[j])
79 filed_longest[i] = longest[j];
80 }
81
82
83 double minD = 999999999999.0;
84 for(int i = 0;i!=n;++i)
85 for(int j = 0 ;j!=n;++j)
86 {
87 if(len[i][j]>99999999999.0)
88 {
89 double l = longest[i]+longest[j]+sqrt(sq(point[i][0]-point[j][0]) + sq(point[i][1]-point[j][1]));
90 double res = maxD(maxD(l,filed_longest[i]),filed_longest[j]);
91 if(res<minD) minD = res;
92 }
93 }
94
95
96 fout << fixed << setprecision(6) << minD << endl;
97
98 fin.close();
99 fout.close();
100
101 return 0;
102 }
posted @ 2011-05-08 10:58  幻魇  阅读(335)  评论(0编辑  收藏  举报