写到一半发现不会计算路数 求大神指导
#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
struct node{
int to;
int val;
};
vector <node>g[1000010];
vector <int >v[1000010];
queue <int> q;
int dis[1000010],dis2[1000010];
bool vis[1000010];
int n,m,a,b;
void spfa(int s){
for(int x=1;x<=n;x++){
dis[x] = inf;
}
dis[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty()){
int e=q.front();
q.pop();
vis[e]=0;
for(int x=0;x<g[e].size();x++){
node to=g[e][x];
if(dis[to.to] > dis[e]+to.val){
dis[to.to] = dis[e]+to.val;
if(vis[to.to] == 0){
vis[to.to] = 1;
q.push(to.to);
}
}
}
}
}
int main(){
cin >> n >> m;
for(int x=1;x<=m;x++){
cin >> a >> b;
g[a].push_back((node){b,1});
g[b].push_back((node){a,1});
v[a][b]++;
}
spfa(1);
for(int x=1;x<=n;x++){
cout << dis[x] << ' ';
}//调试
return 0;
}