codevs.cn 最优布线问题
#include<cstdio>
#include<cstring>
bool u[101];
int g[101][101],minn[101];
int main()
{
int n,m,q,p,total=0;
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
scanf("%d%d",&q,&p);
scanf("%d",&g[q][p]);
}
memset(minn,0x7f,sizeof(minn)); //初始化
minn[1]=0;
memset(u,1,sizeof(u));
for (int i=1;i<=n;i++)
{
int k=0;
for (int j=1;j<=n;j++)
if (u[j]&&(minn[j]<minn[k]))
k=j;
u[k]=false;
for (int j=1;j<=n;j++)
if (u[j]&&(g[k][j]<minn[j]))
minn[j]=g[k][j];
}
for (int i=1;i<=n;i++)
total=total+minn[i];
printf("%d",total);
return 0;
}
时间: 2024-10-10 13:07:07