荣耀8.10第三题
没有提交,试了两个例子还行,大家看看有问题没
#adj_list=[0,1,1,1,2,1,2,3,1] #adj_list=[0,3,5,0,4,9,0,6,8,0,7,5,1,2,6,1,6,3,2,9,7,3,4,3,3,5,3,3,8,3,3,9,3,5,8,3,5,8,9,7,8,9] adj_list=[0,3,5,0,4,9,0,6,8,0,7,5,1,2,6,1,6,3,2,9,7,3,4,3,3,5,3,3,8,3,3,9,3,5,8,9,7,8,9] def print_for(user_list): for i in user_list: print(i) print() def app(m,user,n): #用户邻接矩阵 r = [[0]*m for _ in range(m)] for i in range(0,len(adj_list),3): r[adj_list[i]][adj_list[i+1]]=adj_list[i+2] r[adj_list[i+1]][adj_list[i]]=adj_list[i+2] print_for(r) # 每行表示第i度用户关系分数,从0开始 score = [[0]*m for _ in range(n+1)] # 第i度用户 remain=[user] # 已经访问过的用户 memory = [user] for i in range(1,n+1): for _ in range(len(remain)): s = remain.pop(0) for j in range(m): if r[s][j]!=0 and j not in memory: score[i][j]=r[s][j]+score[i-1][s] print_for(score) remain.append(j) memory.append(j) dic={} for i,j in enumerate(score[n]): dic[i]=j re = sorted(dic.items(), key = lambda kv:kv[1],reverse=True) result = [] for k,v in re: if v!=0: result.append(k) print(result) #app(4,0,2) #app(10,5,2) app(10,0,2)