nyoj 545 第五届河南省程序设计大赛F

Metric Matrice

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 1
描述

Given as input a square distance matrix, where a[i][j] is the distance between point i and point j, determine if the distance matrix is "a  metric" or not.

A distance matrix a[i][j] is a metric if and only if

    1.  a[i][i] = 0

    2, a[i][j]> 0  if i != j

    3.  a[i][j] = a[j][i]

    4.  a[i][j] + a[j][k] >= a[i][k]  i ¹ j ¹ k

输入
The first line of input gives a single integer, 1 ≤ N ≤ 5, the number of test cases. Then follow, for each test case,
* Line 1: One integer, N, the rows and number of columns, 2 <= N <= 30
* Line 2..N+1: N lines, each with N space-separated integers 
(-32000 <=each integer <= 32000).
输出
Output for each test case , a single line with a single digit, which is the lowest digit of the possible facts on this list:
* 0: The matrix is a metric
* 1: The matrix is not a metric, it violates rule 1 above
* 2: The matrix is not a metric, it violates rule 2 above
* 3: The matrix is not a metric, it violates rule 3 above
* 4: The matrix is not a metric, it violates rule 4 above
样例输入
2
4
0 1 2 3
1 0 1 2
2 1 0 1
3 2 1 0
2
0 3
2 0

样例输出
0
3

来源
第五届河南省程序设计大赛
上传者
rihkddd



只能说英文第一题是签到题-.-

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int shu[32][32],n;
bool fafe[5];
int main()
{
    int t;scanf("%d",&t);
    while (t--)
    {
        scanf("%d",&n);
        memset(fafe,true,sizeof(fafe));
        for (int i=0;i<n;i++)
        for (int j=0;j<n;j++)
        {
            scanf("%d",&shu[i][j]);
            if (i==j)
            {
                if (shu[i][j]!=0)
                fafe[1]=false;
            }
            else if (shu[i][j]<=0)
            fafe[2]=false;
        }
        if (!fafe[1])
        {
            printf("1\n");continue;
        }
        if (!fafe[2])
        {
            printf("2\n");continue;
        }
        for (int i=0;i<n-1;i++)
        for (int j=i+1;j<n;j++)
        if (shu[i][j]!=shu[j][i])
            fafe[3]=false;
        if (!fafe[3])
        {
            printf("3\n");continue;
        }
        for (int i=0;i<n;i++)
        {
            for (int j=0;j<n;j++)
            {
                if (i==j) continue;
                for (int k=0;k<n;k++)
                {
                    if (k==i||k==j) continue;
                    if (shu[i][j]+shu[j][k]<shu[i][k])
                    fafe[4]=false;
                    break;
                }
                if (!fafe[4])
                break;
            }
            if (!fafe[4])
            break;
        }
        if (!fafe[4])
        {
            printf("4\n");continue;
        }
        printf("0\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值