Agri-Net

输入:

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
输出:

28
代码
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int map[100][100],dis[10000],mark[10000];
void prime(int n)
{
    int min=65535,pos,ans=0;
    for(int i=0; i<n; i++)
    {
        mark[i]=0;
        dis[i]=map[0][i];
    }
    mark[0]=1;
    for(int i=1; i<n; i++)
    {
        min=65535;
        for(int j=0; j<n; j++)
        {
            if(mark[j]==0&&dis[j]<min)
            {
                min=dis[j];
                pos=j;
            }
        }
        mark[pos]=1;
        ans=ans+dis[pos];
        for(int j=0; j<n; j++)
        {
            if(mark[j]==0&&dis[j]>map[pos][j])
            {
                dis[j]=map[pos][j];
            }
        }

    }
    cout<<ans<<endl;
}
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                cin>>map[i][j];
            }
        }
        prime(n);
    }
    return 0;
}

你可能感兴趣的:(Agri-Net)