Sicily 1090 Highways

最小生成树

#include <stdio.h>
int main()
{
    int ctr,t,n,i,j,mi,ma,po;
    int a[502][502],data[502];
    int flag=0;

    scanf( "%d",&t );
    while ( t-- ) {
        scanf( "%d",&n );
        for ( i=0;i<n;i++ ) {
            for ( j=0;j<n;j++ ) {
                scanf( "%d",&a[i][j] );
                if ( a[i][j]==0 )
                    a[i][j]=65537;
            }
        }
        ctr=0;
        while ( ctr<n-1 ) {
            mi=65537;
            for ( i=1;i<n;i++ ) {
                if ( a[0][i]<mi ) {
                    mi=a[0][i];
                    po=i;
                }
            }
            data[ctr]=mi;
            ctr++;
            for ( i=1;i<n;i++ ) {
                if( a[po][i]<a[0][i] ) 
                    a[0][i]=a[po][i];
                if ( a[i][po]<a[i][0] )
                    a[i][0]=a[i][po];
                a[i][po]=65537;
                a[po][i]=65537;
            }
            a[0][po]=65537;

        }
        ma=data[0];
        for ( i=0;i<ctr;i++ ) {
            if ( data[i]>ma )
                ma=data[i];
        }
        if ( flag ==0 )
            flag=1;
        else 
            printf( "\n" );

        printf( "%d\n",ma );

    }
    return 0;
}

你可能感兴趣的:(ini)