当前位置: 代码迷 >> 综合 >> 第五届省赛题 Metric Matrice
  详细解决方案

第五届省赛题 Metric Matrice

热度:31   发布时间:2024-01-16 20:16:05.0

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
题意:

题解:这道题是说:给出一个矩阵,看是否是符合规则的。如果均满足下列四种规则,输出0.

如果不满足1输出1

如果不满足2输出2

如果不满足3输出3

如果不满足4输出4

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int map[35][35];
int n;
int f1()
{int a1=1;for(int i=1;i<=n;i++){if(map[i][i]!=0){a1=0;return a1;}}return a1;
}
int f2()
{int a2=1;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(i!=j)if(map[i][j]<=0){a2=0;return a2;}}}return a2;
}
int f3()
{int a3=1;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){if(map[i][j]!=map[j][i]){a3=0;return a3;}}return a3;
}
int f4()
{int a4=1;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)for(int k=1;k<=n;k++){if(i!=j&&i!=k&&j!=k){if(map[i][j]+map[j][k]<map[i][k]){a4=0;return a4;}}}return a4;
}
int main()
{int t;scanf("%d",&t);while(t--){int i,j;scanf("%d",&n);for(i=1;i<=n;i++)for(j=1;j<=n;j++){scanf("%d",&map[i][j]);}int a1=f1();int a2=f2();int a3=f3();int a4=f4();if(a1==1&&a2==1&&a3==1&&a4==1)printf("0\n");else if(a1==0)printf("1\n");else if(a2==0)printf("2\n");else if(a3==0)printf("3\n");else if(a4==0)printf("4\n");}return 0;
}


  相关解决方案