大家好,又见面了,我是你们的朋友全栈君。
—-
4 5
1 2
1 3
1 4
2 4
4 3
有向图:
#include <cstdio>
const int N = 1005;
int g[N][N];
int main() {
int n, m; //n个点 m条边
scanf("%d%d", &n, &m);
int u, v; //表示2个点u--->v
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
g[u][v] = 1;//有向图 u-->v
}
//输出
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
printf("%d", g[i][j]);
}
printf("\n");
}
return 0;
}
无向图:
#include <cstdio>
const int N = 1005;
int g[N][N];
int main() {
int n, m; //n个点 m条边
scanf("%d%d", &n, &m);
int u, v; //表示2个点u--->v
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
//无向图
g[u][v] = 1;
g[v][u] = 1;
}
//输出
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
printf("%d", g[i][j]);
}
printf("\n");
}
return 0;
}
#include <cstdio>
const int N = 1005;
int g[N][N];
int main() {
int n, m; //n个点 m条边
scanf("%d%d", &n, &m);
int u, v, w; //表示2个点u--->v w代表权值
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &u, &v, &w);
//有向图
g[u][v] = w;
}
//输出
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
printf("%d ", g[i][j]);
}
printf("\n");
}
return 0;
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/146503.html原文链接:https://javaforall.cn