利用vector进行图的存储

2018-3-30

图的存储有多种方式,我个人比较常用的就是邻接矩阵(存储方式是用两个数组来表示图,一个一维数组存储图中顶点信息,一个二维数组存储图中的边或者弧的信息),优点就是构建起来比较方便,而且我们也可以比较方便的知道任意两点是否有边无边。缺点是如果图中边比较少这样会比较浪费我们的存储空间。

但是这里我要说的是使用vector进行存图:

#include
#include
#include
using namespace std;

const int N = 100;
struct Edge{
    int to,value;
}e;
vector G[N+1];
int m,n,tmp;

int main(){
    cin>>n>>m;
    for (int i=0;icin>>tmp>>e.to>>e.value;
        G[tmp].push_back(e);
    }
    for (int i=1;i<=n;i++){
        for (int j=0;jcout<<"From "<" to "<", the cost is "<return 0;
}

你可能感兴趣的:(vector)