邻接矩阵实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#define MAXN 10001
int G[MAXN][MAXN], Nv, Ne;
void BuildG() {
int u, v, w;
cin >> Nv;
/*CreateGraph*/
for (int i = 0; i < Nv; i++) {
for (int j = 0; j < Nv; j++) {
G[i][j] = 0;
}
}
cin >> Ne;
for (int i = 0; i < Ne; i++) {
cin >> u >> v >> w;
/*InsertEdge*/
G[u][v] = w;
G[v][u] = w;//针对无向图,双向建边
}
}