首页 > 其他 > 详细

【计蒜课】【数据结构】【邻接矩阵使用的复习】

时间:2019-12-09 11:12:51      阅读:58      评论:0      收藏:0      [点我收藏+]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_N 500

typedef struct Graph {
int mat[MAX_N][MAX_N];
int n;
}Graph;

void init(Graph *g, int len) {
g->n=len;
memset(g->mat,0,sizeof(g->mat));
}

void insert(Graph *g, int a, int x, int y) {
if(x<0 || y<0 || x>=g->n || y>=g->n){
return;
}
if(a==0){
g->mat[x][y]=1;
}
if(a==1){
g->mat[x][y]=1;
g->mat[y][x]=1;
}
}

void output(Graph *g) {
for(int i=0;i<g->n;i++){
for(int j=0;j<g->n;j++){
if(!j){
printf("%d",g->mat[i][j]);
}
if(j){
printf(" %d",g->mat[i][j]);}
}
printf("\n");
}
}

int main() {
int n,m,a,x,y;
scanf("%d %d",&n,&m);
Graph *graph=(Graph*)malloc(sizeof(Graph));
init(graph,n);
for(int i=0;i<m;i++){
scanf("%d %d %d",&a,&x,&y);
insert(graph,a,x,y);
}
output(graph);
free(graph);
return 0;
}

【计蒜课】【数据结构】【邻接矩阵使用的复习】

原文:https://www.cnblogs.com/P201821430045/p/12009702.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!