邻接矩阵:
1 #include<cstdio> 2 3 using namespace std; 4 5 int map[233][233]; 6 7 int main() 8 { 9 scanf("%d%d",&n,&m); 10 for (int a=1;a<=m;a++) 11 { 12 int s,e,d; 13 scanf("%d%d%d",&s,&e,&d); 14 map[s][e]=map[e][s]=d; 15 } 16 }
边表:
1 #include<cstdio> 2 3 using namespace std; 4 5 int n,m,first[12345],cnt; 6 7 struct edge 8 { 9 int end; 10 int len; 11 int next; 12 }; 13 14 edge bian[123456]; 15 16 void add_edge(int s,int e,int d) 17 { 18 cnt ++; 19 bian[cnt].end = e; bian[cnt].len = d; 20 bian[cnt].next = first[s]; 21 first[s] = cnt; 22 } 23 24 25 int main() 26 { 27 scanf("%d%d",&n,&m); 28 for (int a=1;a<=m;a++) 29 { 30 int s,e,d; 31 scanf("%d%d%d",&s,&e,&d); 32 add_edge(s,e,d); 33 add_edge(e,s,d); 34 } 35 36 for (int p=first[2];p!=0;p=bian[p].next) 37 printf("%d %d %d ",2,bian[p].end,bian[p].len); 38 }