标签:span one spl names end bit nbsp fine gif
#include<bits/stdc++.h> using namespace std; #define Max 100 #define INF 0x3f3f3f3f int M[Max][Max]; int n,m; void prim() { int u,minv; int d[Max],p[Max],vis[Max]; for(int i=1;i<=n;i++) { d[i]=INF; p[i]=-1; vis[i]=0; } d[1]=0; while(1) { u=-1; minv=INF; for(int i=1;i<=n;i++) { if(d[i]<minv&&vis[i]==0) { u=i; minv=d[i]; } } if(u==-1) break; vis[u]=1; for(int v=1;v<=n;v++) { if(vis[v]==0&&d[v]>M[u][v]) { d[v]=M[u][v]; p[v]=u; } } } int sum=0; for(int i=1;i<=n;i++) { if(p[i]!=-1) { sum+=M[i][p[i]]; cout<<p[i]<<" "<<i<<" "<<d[i]<<endl; } } } int main() { while(cin>>n>>m) { memset(M,INF,sizeof(M)); for(int i=1;i<=m;i++) { int g,v,t; cin>>g>>v>>t; M[g][v]=t,M[v][g]=t; } prim(); } }
标签:span one spl names end bit nbsp fine gif
原文地址:https://www.cnblogs.com/ww123/p/9569104.html