bfd適用于所有邊的加權(quán)值相同的情況下
void dfs(int cur,int dis){
queue<int>q;
int road[100];
road[q.front()] = -1;
q.push(cur);
book[1] = 1;
int flag = 0;
while(!q.empty()){
for(int i = 1; i <= n; i++){
if(a[q.front()][i] == 1 && book[i] == 0){
q.push(i);
book[i] = 1;
road[i] = q.front();
}
if(q.back() == dis){//這里是back()取试,即新入隊的
flag = 1;
break;
}
}
if(flag == 1) break;
q.pop();
}
for(int i = q.back(); road[i] != -1; i = road[i]){//鏈表(但是我不會雙端所以倒序幌甘,可以用一個額外的數(shù)組進正序輸出)
printf("%d<--");
}
printf("%d", cur);
}