輸入當中有起點和終點相同的,所以使用map時要注意
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int N=205;
const int inf=1000;
mapa;
int main()
{
int tot,i,j,k,n,ans,t,st,ed,g[N][N];
char s1[50],s2[50];
while(~scanf("%d",&n))
{
if(n==-1) break;
for(i=0; i<=200; i++)
for(j=0; j<=200; j++)
{
if(i==j) g[i][j]=0;
else g[i][j]=inf;
}
tot=1;
a.clear();
scanf("%s %s ",s1,s2);
if(a[s1]==0)
{
a[s1]=tot;
tot++;
}
st=a[s1];
if(a[s2]==0)
{
a[s2]=tot;
tot++;
}
ed=a[s2];
for(i=0; ig[i][k]+g[k][j])
g[i][j]=g[i][k]+g[k][j];
}
if(g[st][ed]==inf)
printf("-1\n");
else printf("%d\n",g[st][ed]);
}
return 0;
}