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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM