求各结点到根结点的距离
  • 板块题目总版
  • 楼主ANGELA2002
  • 当前回复9
  • 已保存回复9
  • 发布时间2021/5/30 14:28
  • 上次更新2023/11/4 22:30:26
查看原帖
求各结点到根结点的距离
369767
ANGELA2002楼主2021/5/30 14:28

这题怎么做?求助

题目传送门

已有代码

#include<bits/stdc++.h>
using namespace std;
#define MAXSIZE 105;
int v[MAXSIZE],w[MAXSIZE],head[MAXSIZE],next[MAXSIZE],tot=0;
int d[MAXSIZE];
void bfs(int x){
	queue<int>q;
	q.push(x);d[x]=1;
	while(!q.empty()){
		int u=q.front();
		q.pop();
		for(int i=head[u];i;i=next[i]){
			int y=v[i];
			if(d[y])continue;
			d[y]=d[u]+1;
			q.push(y);
		}
	}
}
void add_edge(int x,int y,int z){//建立(x,y)边,权值为z 
	v[++tot]=y;
	w[tot]=z;//权值 
	next[tot]=head[x],head[x]=tot;//在链首x处插入节点y 
}
int main(){
	int n,m,a,b,c;//c为权值 
	cin>>n>>m;
	for(int i=1;i<=m;i++){//输入边 
		cin>>a>>b>>c;
		add_edge(a,b,c)
	}
}
2021/5/30 14:28
加载中...