1478:metric Matrice
Time limit:1 Sec Memory limit:128 MB
submit:72 solved:37
Submitstatusweb 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 1 J 1 k
Input
The first line of input gives a single integer, 1≤n≤5, the number of test cases. Then follow, the 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, digit, which are the lowest digit of the possible facts on this LIS T:
* 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
HINT
Source
The fifth session of the College Student Program design competition in Henan province
The problem is: give a matrix to see if it is in accordance with the rules. If all of the following four rules are met, output 0.
If the 1 output is not satisfied 1
If the 2 output is not satisfied 2
If the 3 output is not satisfied 3
If the 4 output is not satisfied 4
Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int maxx=1005;int a[105][105];int n;int x1,x2,x3,x4;///conforms to the first type of inline int F1 () {x1=1; for (int i=0;i<n;i++) {if (a[i][i]!=0) {x1=0; return x1; }} return x1;} Whether the second type of inline int F2 () {x2=1 is met; for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {if (i!=j) {if (A[i] [j]<=0) {x2=0; return x2; }}}} return x2;} Whether it conforms to the third type of inline int f3 () {x3=1; for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {if (A[i][j]!=a[j][i]) { x3=0; Return x3; }}} return x3;} Whether it conforms to the fourth type of inline int f4 () {x4=1; for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {for (int k=0;k<n;k++) {if (i!=j&&i!=k&&j!=k) if (A[i][j]+a[j][k]<a[i][k]) {x4=0; return x4; }}}} return x4;} int main () {int t; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {scanf ("%d", &a[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;}
Zheng Light 1478 Metric Matrice (fifth session of Henan Province)