首页 > 代码库 > hdu 4034 - Graph

hdu 4034 - Graph

题目:给你最短路的集合,判断图最要有多少边。

分析:最短路。这道题目应该是最水的了,只要利用floyd判断成立和更新就解决了;

            比赛开始了好久才去敲了这道题,导致累计时间,幸好最后以题数晋级。

说明:(2011-09-19 00:43)。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int  maps[ 105 ][ 105 ];
bool smap[ 105 ][ 105 ];

int main()
{
    int T,N;
    scanf("%d",&T);
    for ( int t = 1 ; t <= T ; ++ t ) {
        scanf("%d",&N);
        for ( int i = 1 ; i <= N ; ++ i )
        for ( int j = 1 ; j <= N ; ++ j )
            scanf("%d",&maps[ i ][ j ]);
        
        bool flag = true;
        for ( int k = 1 ; k <= N && flag ; ++ k ) 
        for ( int i = 1 ; i <= N && flag ; ++ i )
        for ( int j = 1 ; j <= N && flag ; ++ j )
            if ( maps[ i ][ j ] > maps[ i ][ k ] + maps[ k ][ j ] )
                flag = false;
                
        if ( !flag ) {
            printf("Case %d: impossible\n",t);
            continue;
        }
        
        memset( smap, false, sizeof( smap ) );
        for ( int k = 1 ; k <= N ; ++ k )
        for ( int i = 1 ; i <= N ; ++ i )
        for ( int j = 1 ; j <= N ; ++ j ) {
            if ( i == k || j == k ) continue;
            if ( maps[ i ][ j ] == maps[ i ][ k ] + maps[ k ][ j ] )
                smap[ i ][ j ] = true;
        }
        
        int count = 0;
        for ( int i = 1 ; i <= N ; ++ i )
        for ( int j = 1 ; j <= N ; ++ j )
            if ( i != j && maps[ i ][ j ] && !smap[ i ][ j ] )
                ++ count;
        
        printf("Case %d: %d\n",t,count);
    }
    //system("pause");
    return 0;
}

hdu 4034 - Graph