nyoj 545 Metric
来源:互联网 发布:生命密码测试软件 编辑:程序博客网 时间:2024/06/05 22:37
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
- 题目意思就是:判断是否满足所给的4个规则,若满足输出0;反正输出 不满足规则号但须是最小的,比方说 不满足1,3 规则 就要输出较小值1。
- 我的思路:依次判断4个规则。
#include<stdio.h>#include<string.h>#define max 30+5int map[max][max];int main(){ int t,n,k,i,j,exist; scanf("%d",&t); while(t--) { scanf("%d",&n); exist=0; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&map[i][j]); if(i==j&&map[i][j]) { exist=1; } } } if(exist) { printf("%d\n",exist); continue; } for(i=1;i<=n;i++) { if(exist)//已经违反规则 break; for(j=1;j<=n;j++) { if(i!=j&&map[i][j]<=0) { exist=2; break; } } } if(exist) { printf("%d\n",exist); continue; } for(i=1;i<=n;i++) { if(exist) break; for(j=1;j<=n;j++) { if(map[i][j]!=map[j][i]) { exist=3; break; } } } if(exist) { printf("%d\n",exist); continue; } for(k=1;k<=n;k++) { if(exist) break; for(i=1;i<=n;i++) { if(exist) break; if(i==k) continue; for(j=1;j<=n;j++) { if(j==k||j==i) continue; if(map[i][k]+map[k][j]<map[i][j]) { exist=4; break; } } } } printf("%d\n",exist); } return 0;}
0 0
- nyoj-545-Metric Matrice
- nyoj 545 Metric
- nyoj 545 Metric Matrice
- NYOJ 545题 Metric Matrice
- NYOJ 545-Metric Matrice【模拟】
- nyoj 545 Metric Matrice【矩阵】水题
- 题目545Metric Matrice
- Metric
- 南阳理工OJ_题目545 Metric Matrice
- Distance metric
- Metric Matrice
- Metric Space
- Metric tensor
- gk metric
- Storm Metric
- Storm Metric
- Metric Manager
- Storm Metric
- Insertion Sort List--LeetCode
- WebView
- kernel panic not syncing : attempted to kill the idle task.
- modbus功能码简述
- 用c实现HASH表创建、插入、查找、删除、打印
- nyoj 545 Metric
- ACM之填写乘法算式
- 【基础】常用的机器学习&数据挖掘知识点
- android api
- //cosf 余弦 正弦 2M_PI / _cellCount 每一个的角度
- 分布式与集群的区别
- 【斐波拉契+数论+同余】【ZOJ3707】Calculate Prime S
- 基类与接口类中的虚析构函数(virtual destructor)
- 题目:去掉最大值、最小值后剩下的个数