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 ¹ 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
HINT
Source
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 ¹ 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
HINT
Source
第五届河南省大学生程序设计竞赛
题解:这道题是说:给出一个矩阵,看是否是符合规则的。如果均满足下列四种规则,输出0.
如果不满足1输出1
如果不满足2输出2
如果不满足3输出3
如果不满足4输出4
代码:
#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;
///是否符合第一种
inline int f1()
{
x1=1;
for(int i=0;i<n;i++)
{
if(a[i][i]!=0)
{
x1=0;
return x1;
}
}
return x1;
}
///是否符合第二种
inline int f2()
{
x2=1;
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;
}
///是否符合第三种
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;
}
///是否符合第四种
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;
}