您的位置:首页 > 其它

USACO2.4.4--Bessie Come Home

2013-02-16 00:26 197 查看
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

/*
ID:spcjv51
PROG:comehome
LANG:C
*/
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define MAXDIS 100000000
#define MAXN 200
long d[10005];
int visit[10005];
int f[MAXN];
int w[MAXN][MAXN];
long n,min;
void Dijkstra(int s)
{
int i,j,k;
memset(visit,0,sizeof(visit));
for(i=0; i<51; i++)
d[i]=w[s][i];
d[s]=0;
for(i=1; i<n; i++)
{
min=MAXDIS;
for(j=0; j<51; j++)
if(!visit[j]&&d[j]<min)
{
min=d[j];
k=j;

}
visit[k]=1;
for(j=0; j<51;j++)
{
if(!visit[j]&&d[k]+w[k][j]<d[j])
d[j]=d[k]+w[k][j];
}
}

}
int main(void)
{
freopen("comehome.in","r",stdin);
freopen("comehome.out","w",stdout);
int i,j,k,ans;
char a,b,c;
scanf("%d",&ans);
memset(f,0,sizeof(f));
for(i=0; i<=MAXN; i++)
for(j=0; j<=MAXN; j++)
w[i][j]=MAXDIS;
n=0;
while(ans--)
{
getchar();
scanf("%c %c %d",&a,&b,&k);
if(islower(a))
i=a-'a';
else
i=a-'A'+26;
if(!f[i])
{
f[i]=1;
n++;
}

if(islower(b))
j=b-'a';
else
j=b-'A'+26;
if(!f[j])
{
f[j]=1;
n++;
}
if(i!=j&&k<w[i][j])
{
w[i][j]=k;
w[j][i]=k;
}
}
Dijkstra(51);
min=MAXDIS;
for(i=26;i<51;i++)
if(f[i]&&d[i]<min)
{
c=i+'A'-26;
min=d[i];
}
printf("%c %ld\n",c,min);
return 0;
}


顺便附上wikipedia上Dijkstra的伪代码

function Dijkstra(Graph, source):
for each vertex v in Graph:                                // Initializations
dist[v] := infinity ;                                  // Unknown distance function from
// source to v
previous[v] := undefined ;                             // Previous node in optimal path
end for                                                    // from source

dist[source] := 0 ;                                        // Distance from source to source
Q := the set of all nodes in Graph ;                       // All nodes in the graph are
// unoptimized - thus are in Q
while Q is not empty:                                      // The main loop
u := vertex in Q with smallest distance in dist[] ;    // Start node in first case
remove u from Q ;
if dist[u] = infinity:
break ;                                            // all remaining vertices are
end if                                                 // inaccessible from source
for each neighbor v of u:                              // where v has not yet been
// removed from Q.
alt := dist[u] + dist_between(u, v) ;
if alt < dist[v]:                                  // Relax (u,v,a)
dist[v] := alt ;
previous[v] := u ;
decrease-key v in Q;                           // Reorder v in the Queue
end if
end for
end while
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:

S := empty sequence
u := target
while previous[u] is defined:                                   // Construct the shortest path with a stack S
insert u at the beginning of S                              // Push the vertex into the stack
u := previous[u]                                            // Traverse from target to source
end while ;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: