rt 最外部的while的次数不是n吗 附参考代码```cpp #include <bits/stdc++.h> using namespace std; const int INF=1e7; int a[105][105],dis[105],p[105],n,m; bool flag[105]; struct node{ int u,step; node(){}; node(int a,int sp){ u=a; step=sp; } bool operator < (const node& a)const{ //重载 < return step>a.step; } }; void Dijkstra(int u){ priority_queue q; //优先队列优化 q.push(node(u,0)); memset(flag,0,sizeof(flag)); //初始化flag为0 for(int i=1; i<=n; i++) dis[i]=INF; //初始化所有距离为无穷大 dis[u]=0; while(!q.empty()){ node now=q.top(); //优先队列队头元素为最小值 q.pop(); int t=now.u; if(flag[t]) continue; //说明已经找到最短距离,该节点是队列里面的重复元素 flag[t]=1; for(int i=1; i<=n; i++){ if(!flag[i]&&a[t][i]<INF){ //判断与当前节点有关系的点,并且自己不能到自己 if(dis[i]>dis[t]+a[t][i]){ //求距离当前节点的每个点的最短距离,进行松弛操作 dis[i]=dis[t]+a[t][i]; q.push(node(i,dis[i])); //把更新后的最短距离压入优先队列,注意,里面的元素有重复 } } } } } int main(){ int u,v,w,st; cin >> n >> m; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) a[i][j]=INF; //初始化邻接矩阵为无穷大 while(m--){ cin >> u >> v >> w; a[u][v]=min(a[u][v],w); //邻接矩阵储存,保留最小的距离 } Dijkstra(1); //源点为1 for(int i=1;i<=n;i++) cout << dis[i] << " ";