找出有向图中两个顶点的最短路径,深度优先遍历每条路径,找出路径最短的
#include<stdio.h>
int e[50][50],book[50];
int inf=99999999;
int n,m,a,b,c,sum=0,min=99999999 ;
void dfs(int cur,int step)
{
int i;
if(step>min)
return;
if(cur==n)
{
if(step<min)
min=step;
return;
}
for(i=1;i<=n;i++)
{
if(e[cur][i]!=99999999 && book[i]==0)
{
book[i]=1;
dfs(i,step+e[cur][i]);
book[i]=0;
}
}
return;
}
int main()
{
int i,j;
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]=inf;
}
for(i=1;i<=m;i++)
{
scanf(“%d%d%d”,&a,&b,&c);
e[a][b]=c;
}
book[1]=1;
dfs(1,0);
printf(“\n最短路径值为%d”,min);
return 0;
}