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
-
The first line of input gives a single integer, 1 ≤ N ≤ 5, the number of test cases. Then follow, for each test case,
简单模拟
被这题给坑了,写下来提醒自己,看清题和注意条件!
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstring>
#include<stack>
#include<stdlib.h>
#include<ctype.h>
using namespace std;
#define MAXN 1300
#define inf 100000
int graph[35][35],flag[5];
int main()
{
int t,n;
cin>>t;
while(t--)
{
memset(flag,1,sizeof(flag));
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>graph[i][j];
for(int i=0;i<n;i++)
{
if(graph[i][i]!=0)flag[1]=0;
for(int j=0;j<n;j++)
{
if(graph[i][j]<=0&&i!=j)flag[2]=0;
if(graph[i][j]!=graph[j][i])flag[3]=0;
for(int k=0;k<n;k++)
{
if(graph[i][j]+graph[j][k]<graph[i][k]&&i!=j&&j!=k&&k!=i)//(x)
flag[4]=0;
}
}
}
bool kl=0;
for(int i=1;i<5;i++)
if(!flag[i])
{
cout<<i<<endl;
kl=1;
break;
}
if(!kl)
cout<<'0'<<endl;
}
}