vector存图

本文介绍了一个使用C++实现的图结构,通过结构体和向量来存储边和顶点信息,演示了如何输入边及其权重,并通过双重循环遍历所有边,展示其连接的顶点和成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct edge{
 4     int to;
 5     int s;
 6     edge(int _to,int _s){to=_to,s=_s;}
 7 };
 8 int n,m;
 9 vector<edge>e[1009];
10 
11 
12 int main()
13 {
14     cin>>n>>m;
15     for(int i=1;i<=n;i++)
16     {
17         int a,b,c;
18         cin>>a>>b>>c;
19         e[a].push_back(edge(b,c));
20         e[b].push_back(edge(a,c));
21     }
22     for(int i=1;i<=m;i++)
23     {
24         /*
25         for(vector<edge>::iterator it=e[i].begin();it!=e[i].end();it++)
26         {
27             printf("%d->%d cost:%d\n",i,it->to,it->s);
28         }
29         */
30         for(int j=0;j<e[i].size();j++)
31         {
32             printf("%d->%d cost:%d\n",i,e[i][j].to,e[i][j].s);
33         }
34     }
35     return 0;
36 }

 

转载于:https://www.cnblogs.com/1129-tangqiyuan/p/11219264.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值