vector(实现存图)

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<vector>
 6 #define N 10000
 7 using namespace std;
 8 struct EDGE
 9 {
10     int to;//终点
11     int cost;//边的权值
12 };
13 vector<EDGE>G[N];//G[i]中i表示出发点
14 int m,n;
15 int temp1;//出发点
16 int main()
17 {
18     scanf("%d%d",&n,&m);
19     while(m--)
20     {
21         EDGE e;
22         scanf("%d%d%d",&temp1,&e.to,&e.cost);//输入出发点,终点,边的权值
23         G[temp1].push_back(e);//将数据压入动态数组,表示在这个出发点下引出的边
24         //相当于二维动态数组
25     }
26     for (int i=1; i<=n; i++) //按照出发点的顺序遍历
27     {
28         for(int j=0; j<G[i].size(); j++) //遍历出发点所引出的边
29         {
30             EDGE e=G[i][j];//1以二维数组形式输出
31             printf("from %d to %d,the cost is %d\n",i,e.to,e.cost);
32         }
33     }
34     return 0;
35 }

猜你喜欢

转载自www.cnblogs.com/moomcake/p/9774402.html