原创,未经允许不得转载。

图的建立有两种,邻接矩阵和邻接表。

邻接矩阵适用于图较为密集,(稀疏图太浪费存储空间了),图如果较为稀疏,则使用邻接表为宜,dijkstra算法就是以邻接表为基础的。

有向无权图

#include<iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
#define N 100000+5
vector<int >p[N]; int main()
{
int n,m;
cin>>n>>m;
int start,to;
for (int i=;i<m;i++)
{
cin>>start>>to;
p[start].push_back(to);
}
for (int i=1;i<=n;i++)
{
for (int j=;j<p[i].size();j++)
{
cout<<p[i][j]<<" ";
}cout<<endl;
}cout<<endl;
}

无向无权图的建立:

#include<iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <cstdio>
#include <iterator>
#include <cstring>
using namespace std;
#define N 100000+5
vector<int >p[N]; int main()
{
int n,m;
cin>>n>>m;
int start,to;
for (int i=;i<m;i++)
{
cin>>start>>to;
p[start].push_back(to);
p[to].push_back(start);
}
for (int i=;i<=n;i++)
{
for (int j=;j<p[i].size();j++)
{
cout<<p[i][j]<<" ";
}cout<<endl;
}cout<<endl;
}

有向有权图的建立:

#include<iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <cstdio>
#include <iterator>
#include <sstream>
#include <cmath>
#include <list>
#include <deque>
#include <cstring>
using namespace std;
#define N 100000+5
struct node
{
int to,cost;
};
vector<node >p[N]; int main()
{
int n,m;
cin>>n>>m;
int start,to;
for (int i=;i<m;i++)
{
node c;
cin>>start>>c.to>>c.cost;
p[start].push_back(c);
}
cout<<endl;
for (int i=;i<=n;i++)
{
for (int j=;j<p[i].size();j++)
{
cout<<i<<" "<<p[i][j].to<<" "<<p[i][j].cost<<" "<<endl;
}
}cout<<endl;
}

无向有权图的建立:

vector 邻接表的建立(好笨啊,才懂,可能太困了吧)。。的更多相关文章

  1. SPFA中 正逆邻接表的建立

    正邻接表的建立: 先定义一个结构体: struct node { int r,v,w,next; }Map[]; 每输入一组数据 就通过Add函数加入到邻接表中,上图已经说得很明白了,结合着下面的代码 ...

  2. 三种邻接表存图模板:vector邻接表、数组邻接表、链式前向星

    vector邻接表: ; struct Edge{ int u,v,w; Edge(int _u=0,int _v=0,int _w=0){u=_u,v=_v,w=_w;} }; vector< ...

  3. HUST 1103 校赛 邻接表-拓扑排序

    Description N students were invited to attend a party, every student has some friends, only if someo ...

  4. Hihocoder 之 #1097 : 最小生成树一·Prim算法 (用vector二维 模拟邻接表,进行prim()生成树算法, *【模板】)

    #1097 : 最小生成树一·Prim算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 最近,小Hi很喜欢玩的一款游戏模拟城市开放出了新Mod,在这个Mod中,玩家可 ...

  5. 用邻接表或vector实现存边以及具体如何调用[模板]

    存边: 对于指针实现的邻接表: struct edge{ int from,next,to,w; }E[maxn]; int head[maxn],tot=0;//head初始化为-1: void a ...

  6. HDU 2647 Reward(拓扑排序,vector实现邻接表)

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  7. 从0开始 图论学习 邻接表 STL vector

    邻接表表示 用vector实现 writer:pprp 代码如下: #include <bits/stdc++.h> using namespace std; const int maxn ...

  8. STL中vector怎么实现邻接表

    最近,同期的一位大佬给我出了一道题目,改编自 洛谷 P2783 有机化学之神偶尔会做作弊 这道题好坑啊,普通链表过不了,只能用vector来存边.可能更快一些吧? 所以,我想记录并分享一下vector ...

  9. 邻接表的使用及和vector的比較

    这几天碰到一些对建边要求挺高的题目.而vector不好建边,所以学习了邻接表.. 以下是我对邻接表的一些看法. 邻接表的储存方式 邻接表就是就是每一个节点的一个链表,而且是头插法建的链表,这里我们首先 ...

随机推荐

  1. android studio run 的时候,报the apk file does not exist on disk,

    1.首先 clean rebuild,重启,不能解决的话,再找到这个 然后是这里: 不用填,点ok,ok即可,他喵的,卡我俩小时

  2. python日记---day1

    Life is  short,Test in  python 一.输入输出 1.用print()在括号中加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world' print('h ...

  3. 样式缩写——css技巧(一)

    一.margin和padding缩写 例: .sample-margin1{ margin-top:15px; margin-right:20px; margin-bottom:12px; margi ...

  4. jQuery UI dialog 使用记录

    1 属性 1.11 autoOpen ,这个属性为true的时候dialog被调用的时候自动打开dialog窗口.当属性为false的时候,一开始隐藏窗口,知道.dialog("open&q ...

  5. 出了一个js的题。

    class test { set xx(v){ console.log('i am set'); this.__ok = v; } get xx(){ console.log('i am get'); ...

  6. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

  7. 20165230 2017-2018-2 《Java程序设计》第6周学习总结

    20165230 2017-2018-2 <Java程序设计>第6周学习总结 教材学习内容总结 第八章 常用使用类 String类常用方法 public int length() publ ...

  8. local variables referenced from a Lambda expression must be final or effectively final------理解

    前几天使用lamdba时,报了一个这个错,原因是在lamdba体中使用了一个变量,觉得很奇怪! 今天在读这本书的时候,又看到了这个解释,这里有了更深刻的理解,总结一下: 在jdk1.8之前在使用匿名内 ...

  9. 【逆向知识】开发WinDBG扩展DLL

    如何开发WinDbg扩展DLL WinDbg扩展DLL是一组导出的回调函数,用于实现用户定义的命令.以便从内存转储中提取特定的信息.扩展dll由调试器引擎加载,可以在执行用户模式或内核模式调试时提供自 ...

  10. 【HASPDOG】卸载

    rpm -qa | grep aksusdb rpm -e aksusdb... rm -rf /var/hasplm