USACO2.4.4--Bessie Come Home

Bessie Come Home
Kolstad & Burch

It's dinner time, and the cows are out in their separate pastures. Farmer John rings the bell so they will start walking to the barn. Your job is to figure out which one cow gets to the barn first (the supplied test data will always have exactly one fastest cow).

Between milkings, each cow is located in her own pasture, though some pastures have no cows in them. Each pasture is connected by a path to one or more other pastures (potentially including itself). Sometimes, two (potentially self-same) pastures are connected by more than one path. One or more of the pastures has a path to the barn. Thus, all cows have a path to the barn and they always know the shortest path. Of course, cows can go either direction on a path and they all walk at the same speed.

The pastures are labeled `a'..`z' and `A'..`Y'. One cow is in each pasture labeled with a capital letter. No cow is in a pasture labeled with a lower case letter. The barn's label is `Z'; no cows are in the barn, though.

PROGRAM NAME: comehome

INPUT FORMAT

Line 1: Integer P (1 <= P <= 10000) the number of paths that interconnect the pastures (and the barn)
Line 2..P+1: Space separated, two letters and an integer: the names of the interconnected pastures/barn and the distance between them (1 <= distance <= 1000)

SAMPLE INPUT (file comehome.in)

5
A d 6
B d 3
C e 9
d Z 8
e Z 3

OUTPUT FORMAT

A single line containing two items: the capital letter name of the pasture of the cow that arrives first back at the barn, the length of the path followed by that cow.

SAMPLE OUTPUT (file comehome.out)

B 11

 题解:赤裸裸的单源最短路,Floyd-Warshall,SPFA,Dijkstra都可以,不过Floyd-Warshall的编程复杂度是最简单的。我用了Dijkstra,因为想熟悉一下Dijkstra算法。此题是求各个牧场的牛到谷仓的最短距离,反过来也就是求谷仓到各个有牛的牧场的距离的最小值。注意:小写字母和大写字母不代表同一个牧场!

View Code
 1 /*
 2 ID:spcjv51
 3 PROG:comehome
 4 LANG:C
 5 */
 6 #include<stdio.h>
 7 #include<ctype.h>
 8 #include<string.h>
 9 #define MAXDIS 100000000
10 #define MAXN 200
11 long d[10005];
12 int visit[10005];
13 int f[MAXN];
14 int w[MAXN][MAXN];
15 long n,min;
16 void Dijkstra(int s)
17 {
18     int i,j,k;
19     memset(visit,0,sizeof(visit));
20     for(i=0; i<51; i++)
21         d[i]=w[s][i];
22     d[s]=0;
23     for(i=1; i<n; i++)
24     {
25         min=MAXDIS;
26         for(j=0; j<51; j++)
27             if(!visit[j]&&d[j]<min)
28             {
29                 min=d[j];
30                 k=j;
31 
32             }
33         visit[k]=1;
34         for(j=0; j<51;j++)
35         {
36             if(!visit[j]&&d[k]+w[k][j]<d[j])
37                 d[j]=d[k]+w[k][j];
38         }
39     }
40 
41 }
42 int main(void)
43 {
44     freopen("comehome.in","r",stdin);
45     freopen("comehome.out","w",stdout);
46     int i,j,k,ans;
47     char a,b,c;
48     scanf("%d",&ans);
49     memset(f,0,sizeof(f));
50     for(i=0; i<=MAXN; i++)
51         for(j=0; j<=MAXN; j++)
52             w[i][j]=MAXDIS;
53     n=0;
54     while(ans--)
55     {
56         getchar();
57         scanf("%c %c %d",&a,&b,&k);
58         if(islower(a))
59             i=a-'a';
60         else
61             i=a-'A'+26;
62         if(!f[i])
63         {
64             f[i]=1;
65             n++;
66         }
67 
68         if(islower(b))
69             j=b-'a';
70         else
71             j=b-'A'+26;
72         if(!f[j])
73         {
74             f[j]=1;
75             n++;
76         }
77         if(i!=j&&k<w[i][j])
78         {
79             w[i][j]=k;
80             w[j][i]=k;
81         }
82     }
83     Dijkstra(51);
84     min=MAXDIS;
85     for(i=26;i<51;i++)
86     if(f[i]&&d[i]<min)
87     {
88         c=i+'A'-26;
89         min=d[i];
90     }
91     printf("%c %ld\n",c,min);
92 return 0;
93 }

顺便附上wikipedia上Dijkstra的伪代码

 1  function Dijkstra(Graph, source):
 2        for each vertex v in Graph:                                // Initializations
 3            dist[v] := infinity ;                                  // Unknown distance function from 
 4                                                                   // source to v
 5            previous[v] := undefined ;                             // Previous node in optimal path
 6        end for                                                    // from source
 7        
 8        dist[source] := 0 ;                                        // Distance from source to source
 9        Q := the set of all nodes in Graph ;                       // All nodes in the graph are
10                                                                  // unoptimized - thus are in Q
11       while Q is not empty:                                      // The main loop
12          u := vertex in Q with smallest distance in dist[] ;    // Start node in first case
13          remove u from Q ;
14          if dist[u] = infinity:
15               break ;                                            // all remaining vertices are
16          end if                                                 // inaccessible from source          
17          for each neighbor v of u:                              // where v has not yet been 
18                                                                 // removed from Q.
19              alt := dist[u] + dist_between(u, v) ;
20               if alt < dist[v]:                                  // Relax (u,v,a)
21                  dist[v] := alt ;
22                  previous[v] := u ;
23                   decrease-key v in Q;                           // Reorder v in the Queue
24              end if
25           end for
26       end while
27   return dist;

If we are only interested in a shortest path between vertices source and target, we can terminate the search at line 13 if u = target. Now we can read the shortest path from source to target by reverse iteration:

1   S := empty sequence
2   u := target
3   while previous[u] is defined:                                   // Construct the shortest path with a stack S
4       insert u at the beginning of S                              // Push the vertex into the stack
5       u := previous[u]                                            // Traverse from target to source
6   end while ;

 

posted on 2013-02-16 00:26  仗剑奔走天涯  阅读(193)  评论(0编辑  收藏  举报

导航