图的最短路径
dfs:
public:
//shortest为最短路径 edge[100][100]初始化一个二维数组作为路线值
//matrix作为记录是否访问过
int shortest,edge[100][100],n,m,en,matrix[i];
void dfs(int cur,int dst){
if(shortest<dst){return;}
if(cur==en){
if(shortest>dst){
shortest = dst;
return;
}
}
for(int i=0;i<=n;i++){
if(mark[i]==0&&edge[cur][i]!=INT_MAX&&edge[cur][i]!=0){
mark[i]=1;
dfs(i,dst+edge[cur][i]);
mark[i]=0;
}
}
return;
}
bfs:
public:
int shortest=0,edge[100][100],n,m,en,matrix[i];
void bfs(){
queue<int> q;
q.push(1);
matrix[1]=1;
while(!q.empty()){
int v = q.front();
for(int i=1;i<=n;i++){
if(matrix[i]==0&&edge[v][i]!=INT_MAX&&edge[v][i]!=0){
q.push(i);
matrix[i]=1;
}
}
cout<<v<<endl;
q.pop();
}
}