nyoj 545 Metric Matrice

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
樣例輸入
240 1 2 31 0 1 22 1 0 13 2 1 020 32 0
樣例輸出

03

題目不難,關鍵是理解意思,當多個條件不滿足時,取序號最小的條件

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int a[50][50];

int main(){
    int i,j,k,t,n,f;
    scanf("%d",&t);
    while(t--){
        f=0;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            scanf("%d",&a[i][j]);

        for(i=1;i<=n;i++)
            if(a[i][i]!=0&&!f) f=1;

        for(i=1;i<=n;i++)
        for(j=1;j<=n;j++){
            if(i!=j&&!f){
                if(a[i][j]<=0) f=2;
            }
        }

        for(i=1;i<=n;i++)
        for(j=1;j<=n;j++){
            if(i!=j&&!f){
                if(a[i][j]!=a[j][i]) f=3;
            }
        }

        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
        for(k=1;k<=n;k++){
            if((i!=j)&&(j!=k)&&(i!=k)&&!f){
                if(a[i][j]+a[j][k]<a[i][k]){
                    f=4;
                }
            }
        }
        if(f)
            printf("%d\n",f);
        else
            printf("0\n");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章