链式前向星

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 const int N = 10010;
 6 
 7 int n, m;
 8 
 9 int cnt = 0;
10 struct node
11 {
12     int to, w, nextt;
13 } e[N << 1]; //坑点 完全图的边数大约是n的二倍 注意题目数据
14 int head[N];
15 
16 inline void init()
17 {
18     memset(head, -1, sizeof head);
19 }
20 
21 inline void add_edge(int u, int v, int w)
22 {
23     e[++cnt].to = v;
24     e[cnt].w = w;
25     e[cnt].nextt = head[u];
26     head[u] = cnt;
27 }
28 
29 signed main()
30 {
31     init();
32     cin >> n >> m;
33     for(int i = 1; i <= m; ++i)
34     {
35         int u, v, w;
36         cin >> u >> v >> w;
37         add_edge(u, v, w);
38     }
39     for(int i = 1; i <= n; ++i)
40     {
41         for(int j = head[i]; j != -1; j = e[j].nextt)
42         {
43             cout << i << " " << e[j].to << " " << e[j].w << endl;
44         }
45     }
46     return 0;
47 }
48 /*
49 input:
50 3 3
51 1 2 3
52 2 3 4
53 1 3 5
54 
55 output:
56 1 3 5
57 1 2 3
58 2 3 4
59 */

 

posted @ 2022-02-25 10:52  std&ice  阅读(51)  评论(0编辑  收藏  举报