sicily 1083. Networking
#include<iostream> //Prim算法
#include<cstring>
using namespace std;
int edge[55][55];
struct MST
{
int st,ed,w;
}mst[100];
int main()
{
int p,r;
while(cin>>p,p)
{
cin>>r;
for(int i=1;i<=p;++i)
for(int j=1;j<=p;++j)
edge[i][j]=200;
int a,b,c;
while(r--)
{
cin>>a>>b>>c;
edge[b][a]=edge[a][b]=min(c,edge[a][b]); //边权值取较小值
}
int sum=0;
for(int i=1;i<p;++i)
{
mst[i].st=1;mst[i].ed=i+1;
mst[i].w=edge[1][i+1];
}
for(int i=1;i<=p-1;++i)
{
int MIN=200,u;
for(int j=i;j<=p-1;++j)
if(mst[j].w<MIN)
{
MIN=mst[j].w;
u=j;
}
swap(mst[i],mst[u]);
sum+=mst[i].w;
int v=mst[i].ed;
for(int j=i+1;j<=p-1;++j)
{
if(mst[j].w>edge[v][mst[j].ed])
{
mst[j].w=edge[v][mst[j].ed];
mst[j].st=v;
}
}
}
cout<<sum<<endl;
}
return 0;
}
#include<cstring>
using namespace std;
int edge[55][55];
struct MST
{
int st,ed,w;
}mst[100];
int main()
{
int p,r;
while(cin>>p,p)
{
cin>>r;
for(int i=1;i<=p;++i)
for(int j=1;j<=p;++j)
edge[i][j]=200;
int a,b,c;
while(r--)
{
cin>>a>>b>>c;
edge[b][a]=edge[a][b]=min(c,edge[a][b]); //边权值取较小值
}
int sum=0;
for(int i=1;i<p;++i)
{
mst[i].st=1;mst[i].ed=i+1;
mst[i].w=edge[1][i+1];
}
for(int i=1;i<=p-1;++i)
{
int MIN=200,u;
for(int j=i;j<=p-1;++j)
if(mst[j].w<MIN)
{
MIN=mst[j].w;
u=j;
}
swap(mst[i],mst[u]);
sum+=mst[i].w;
int v=mst[i].ed;
for(int j=i+1;j<=p-1;++j)
{
if(mst[j].w>edge[v][mst[j].ed])
{
mst[j].w=edge[v][mst[j].ed];
mst[j].st=v;
}
}
}
cout<<sum<<endl;
}
return 0;
}