USACO 2.4 Bessie Come Home

TASK: comehome
LANG: C++

Compiling...
Compile: OK

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

All tests OK.
1 /*
2 PROG: comehome
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 int name_to_num[129];
15 int num_to_name[129];
16 long diam[129][129];
17 int road_num;
18 int top;
19 int main()
20 {
21 ifstream fin("comehome.in");
22 ofstream fout("comehome.out");
23
24 for(int i = 0;i!=129;++i) name_to_num[i] = -1;
25 int road_num;
26
27 for(int i = 0;i!=129;++i)
28 for(int j = 0 ; j!=129;++j)
29 {
30 if(i!=j)
31 diam[i][j] = 1000000;
32 else
33 diam[i][j] = 0;
34 }
35 fin>>road_num;
36 char c1,c2;
37 long teml;
38 for(int i = 0;i!=road_num;++i)
39 {
40 fin>>c1>>c2>>teml;
41 if(name_to_num[c1]==-1)
42 {
43 name_to_num[c1] = top;
44 num_to_name[top] = c1;
45 ++top;
46 }
47 if(name_to_num[c2]==-1)
48 {
49 name_to_num[c2] = top;
50 num_to_name[top] = c2;
51 ++top;
52 }
53 if(diam[name_to_num[c1]][name_to_num[c2]]>teml)
54 diam[name_to_num[c2]][name_to_num[c1]] = diam[name_to_num[c1]][name_to_num[c2]] = teml;
55 }
56
57 int source = name_to_num['Z'];
58 long dist[129];
59 bool haveGo[129]={false};
60 haveGo[source] = true;
61 for(int i = 0 ;i!=top;++i)
62 {
63 dist[i] = diam[source][i];
64 }
65
66 for(int time = 1;time<=top-1;++time)
67 {
68 int minN = 1000000000;
69 int num;
70 for(int i = 0;i!=top;++i)
71 {
72 if(!haveGo[i] && dist[i]<minN)
73 {
74 minN = dist[i];
75 num = i;
76 }
77 haveGo[i] = true;
78 for(int j = 0;j!=top;++j)
79 {
80 if(dist[j]>dist[i]+diam[i][j])
81 dist[j] = dist[i]+diam[i][j];
82 }
83 }
84 }
85
86
87 int minN = 1000000000;
88 int num;
89 for(int i = 0;i!=top;++i)
90 {
91 if(i!=source && dist[i]<minN && num_to_name[i]<='Y' && num_to_name[i]>='A')
92 {
93 minN = dist[i];
94 num = i;
95 }
96 }
97 fout<<(char)num_to_name[num]<<" "<<minN<<endl;
98 fin.close();
99 fout.close();
100
101 return 0;
102 }
posted @ 2011-05-08 11:39  幻魇  阅读(252)  评论(0编辑  收藏  举报