题解:

简单生成树

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=305;
int n,dis[N],f[N],a[N][N],ans;
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)scanf("%d",&dis[i]);
    for (int i=1;i<=n;i++)
     for (int j=1;j<=n;j++)scanf("%d",&a[i][j]);
    for (int i=1;i<=n;i++)
     {
         int l=-1;
         for (int j=1;j<=n;j++)
          if (!f[j]&&(l==-1||dis[j]<dis[l]))l=j;
         ans+=dis[l];f[l]=1;
        for (int j=1;j<=n;j++)
         if (!f[j]&&dis[j]>a[l][j])
          dis[j]=a[l][j]; 
     } 
    printf("%d",ans); 
}

 

posted on 2018-02-25 13:41  宣毅鸣  阅读(48)  评论(0编辑  收藏  举报