#include<iostream> #include<cstring> #include<climits> using namespace std; const int M=500; int cost[M][M]; int Creat(){ int n,m; cin>>n>>m; for(int q=1;q<=n;q++){ for(int w=1;w<=n;w++)cost[q][w]=INT_MAX; } int a,b,c; for(int q=1;q<=m;q++){ cin>>a>>b>>c; cost[a][b]=c; cost[b][a]=c; } return n; } void prim(int n){ int close[M],lowcost[M]; for(int q=1;q<=n;q++){ close[q]=1; lowcost[q]=cost[1][q]; } close[1]=-1; for(int q=1;q<n;q++){ int Min=INT_MAX,u=q; for(int w=1;w<=n;w++)if(close[w]!=-1 && lowcost[w]<Min){ Min=lowcost[w]; u=w; } cout<<"("<<close[u]<<","<<u<<")"<<" "<<lowcost[u]<<endl; for(int w=1;w<=n;w++)if(close[w]!=-1 && lowcost[w]>cost[u][w]){ lowcost[w]=cost[u][w]; close[w]=u; } } return; } int main() { int t; cin>>t; while(t--){ int n=Creat(); prim(n); } return 0; }
原文地址:https://www.cnblogs.com/yangmenda/p/11725595.html
时间: 2024-09-29 09:09:10