0
点赞
收藏
分享

微信扫一扫

第五届河南省赛 zzulioj 1478: Metric Matrice (模拟)

余寿 2023-04-19 阅读 39


1478: Metric Matrice


Time Limit: 1 Sec   Memory Limit: 128 MB

Submit: 70  

Solved: 35


Submit

Status

Web Board


Description


nt 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


Input


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


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


Sample Input


2


4


0 1 2 3


1 0 1 2


2 1 0 1


3 2 1 0


2


0 3


2 0


Sample Output


0


3


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[31][31];
int main()
{
	int t,n,i,j,k;
	scanf("%d",&t);
	while(t--)
	{
		memset(a,0,sizeof(a));
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				scanf("%d",&a[i][j]);
			}
		}
		int flag=0;
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				if(i==j&&a[i][j]!=0)
				{
					flag=1;
					break;
				}
				else if(i!=j&&a[i][j]<=0)
				{
					flag=2;
					break;
				}
				else if(a[i][j]!=a[j][i])
				{
					flag=3;
					break;
				}
				else
				{
					for(k=0;k<n&&k!=i&&k!=j&&i!=j;k++)
					{
						if(a[i][j]+a[j][k]<a[i][k])
						{
							flag=4;
							break;
						}
					}
					if(flag)
						break;
				}
			}
			if(flag)
				break;
		}
		printf("%d\n",flag);
	}
	return 0;
}


Source


第五届河南省大学生程序设计竞赛

举报

相关推荐

0 条评论