问题及答案:
#include<stdio.h>
int min=99999999,n;
int e[101][101];
int book[101];
void dfs(int cur,int dis)
{
int i;
if(dis>min) return;
if(cur==n)
{
if(dis<min) min=dis;
return;
}
for(i=1;i<=n;i++)
{
if(e[cur][i]!=999999 && book[i]==0)
{
book[i]=1;
dfs(i,dis+e[cur][i]);
book[i]=0;
}
}
return;
}
int main()
{
int m,i,j,a,b,c;
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i==j) e[i][j]=0;
else
e[i][j]=99999999;
}
for(i=1;i<=m;i++)
{
scanf("%d %d %d",&a,&b,&c);
e[a][b]=c;
}
book[1]=1;
dfs(1,0);
printf("%d",min);
getchar();getchar();
return 0;
}