4409: 【例18-2】图的存储-邻接矩阵
内存限制:128 MB
时间限制:1.000 S
评测方式:文本比较
命题人:
提交:3
解决:2
题目描述
现希望将第二张图和第三张图 存进计算机。 可以使用邻接矩阵存储一张图: 记 v[i][j] 表示从 i 到 j 的边权。 如果不通可以设置为 0 或者 inf (一个很大的数字)。 无向图的邻接矩阵是对称的, 有向图的邻接矩阵则不对称。
样例输入 复制
4
0 5 2 3
5 0 0 1
2 0 0 4
3 1 4 0
样例输出 复制
edge from point 1 to point 2 with length 5
edge from point 1 to point 3 with length 2
edge from point 1 to point 4 with length 3
edge from point 2 to point 1 with length 5
edge from point 2 to point 4 with length 1
edge from point 3 to point 1 with length 2
edge from point 3 to point 4 with length 4
edge from point 4 to point 1 with length 3
edge from point 4 to point 2 with length 1
edge from point 4 to point 3 with length 4
提示
#include<bits/stdc++.h> using namespace std; #define MAXN 1005 int n; int v[MAXN][MAXN]; int main(){ cin >> n; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> v[i][j]; // 存入每一对点之间的边权 for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (v[i][j] > 0) cout << "edge from point " << i << " to point " << j << " with length " << v[i][j] << '\n'; return 0; }