| Bytes | Lang | Time | Link |
|---|---|---|---|
| 318 | Python 2.x | 120116T000215Z | Christop |
| 207 | Haskell | 120114T103820Z | hammar |
| 390 | C | 120113T015601Z | Ali1S232 |
Python (2.x), 382 369 358 338 323 318 characters
All tips and comments welcome!
import os;u=str.split;l=u(os.read(0,1e9),'\n')
p,g,c=l.index(''),{},map;o=g.setdefault
def f(g,s,e,q=[]):q=q+[s];y=([],[q])[s==e];[c(y.append,f(g,n,e,q))for n in set(o(s,[]))-set(q)];return y
for k,v in c(u,l[:p]):o(k,[]);g[k]+=v
for w,e in c(u,l[p+1:]):h=sorted(f(g,w,e));print''if not h else' '.join(min(h,key=len))
Should pass the tests in this form. Feed input via stdin, e.g. python shortestroute.py < test.txt.
Haskell, 223 207 characters
main=interact$unlines.f.break null.map words.lines
s%[f,t]=[[f]]#t where[]#_="";x#t|y@(_:_)<-[z|z<-x,last z==t]=unwords$minimum y|1<3=s&x#t
s&z=[x++[b]|x<-z,[a,b]<-s,last x==a,notElem b x];f(s,_:q)=map(s%)q
C:450,437,404,390 characters
#include<stdio.h>
#include <string.h>
r[99][99],p[99],q[99],m[99],i,j,x,y,s;
char t[9],e[9999];
F(k)
{
m[k]^s?r[p[k]=q[i]][k]?m[q[j++]=k]=s:0:0;
if(++k<99)F(k);
}
f()
{
F(0);
if(++i^j)f();
}
P(o)
{
if(p[o])P(p[o]);
*t=m[o]^s?0:o;
strcat(e,t);
}
w()
{
gets(t);
r[*t][t[2]]=*t?w():0;
}
W()
{
gets(t);
x=*t,x?y=t[j=2],s=x+y*99,m[q[t[2]=i=p[x]=0]=x]=s,f(),P(y),strcat(e,"\n"),W():0;
}
main()
{
w();
W();
puts(e);
}