当前位置:网站首页>关于【链式前向星】的自学理解
关于【链式前向星】的自学理解
2022-07-29 05:24:00 【C2024XSC249】
我们就以下图为例:
根据建边的顺序,我们可以给这些边都创建一个序号,如:
1 2 <---1号
2 3 <---2号
3 4 <---3号
1 3 <---4号
4 1 <---5号
1 5 <---6号
4 5 <---7号
按照《算法竞赛》中给出的思路,我们可以写出链表:
1 -> 5 -> 3 -> 2
2 -> 3
3 -> 4
4 -> 5 -> 1
5
这样,我们可以从后往前地观察,维护一个head[i]
数组,表示输入边中的弧头为i
的最后一条边的编号,即:
head[1] = 6
head[2] = 2
head[3] = 3
head[4] = 7
head[5] = 0(即不存在弧头为5的边)
我们在存边的时候也可以顺便维护to[i]
数组,即第i
条边的弧尾。这个太简单了,就不说了。
最后我们维护next[i]
数组,我们设第i
条边的弧头为x
,则next[i]
表示在第i
条边之前最后一条弧头为x
的边的编号,即:
next[1] = 0
next[2] = 0
next[3] = 0
next[4] = 1
next[5] = 0
next[6] = 4
next[7] = 5
拿到这三个数组后,我们就可以实现加边操作。
void AddEdge (int u, int v, int x) {
to[++ cnt] = v;
w[cnt] = x;
next[cnt] = head[u];
head[u] = cnt;
}
当我们要枚举这个点的邻接点时,只需用for
循环遍历即可。
for (int i = head[x]; i; i = next[i]) {
//...
}
例题:邻接表存储带权的无向图
代码
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 1000 + 5;
int n, m, u, p, x, cnt, to[MAXN], head[MAXN], w[MAXN], next[MAXN];
struct node {
int x, w;
bool operator < (const node x) const {
if (w == x.w) {
return this->x < x.x;
} else {
return w < x.w;
}
}
};
vector <node> v;
void AddEdge (int u, int v, int x) {
to[++ cnt] = v;
w[cnt] = x;
next[cnt] = head[u];
head[u] = cnt;
}
int main () {
scanf ("%d %d", &n, &m);
for (int i = 1; i <= m; i ++) {
scanf ("%d %d %d", &u, &p, &x);
AddEdge (u, p, x);
AddEdge (p, u, x);
}
for (int i = 1; i <= n; i ++) {
v.clear();
for (int j = head[i]; j; j = next[j]) {
v.push_back((node) {
to[j], w[j]});
}
sort (v.begin(), v.end());
for (int i = 0; i < v.size(); i ++) {
printf ("%d ", v[i].x);
}
printf ("\n");
}
return 0;
}
边栏推荐
- HAL库学习笔记-11 I2C
- FPGA based: moving target detection (schematic + source code + hardware selection, available)
- ABSA1: Attentional Encoder Network for Targeted Sentiment Classification
- How to use the pre training language model
- QT learning notes QtSql
- 低成本2.4GHz 无线收发芯片--Ci24R1
- 链表--------------------尾插法
- 封装——super关键字
- 基于51单片机的四路抢答器仿真
- 2.4G频段的无线收发芯片 SI24R1 问题汇总解答
猜你喜欢
【软件工程之美 - 专栏笔记】“一问一答”第2期 | 30个软件开发常见问题解决策略
【RoboMaster】A板接收JY-ME01角度传感器数据--modebus协议&CRC软件校验
传统模型预测控制轨迹跟踪——波浪形轨迹(功能包已经更新)
FPGA based: moving target detection (supplementary simulation results, available)
基于STC51:四轴飞控开源项目原理图与源码(入门级DIY)
LoRa开启物联网新时代-ASR6500S、ASR6501/6502、ASR6505、ASR6601
兼容cc1101/cmt2300-DP4301 SUB-1G 无线收发芯片
智慧能源管理系统解决方案
Reading papers on false news detection (5): a semi supervised learning method for fake news detection in social media
【软件工程之美 - 专栏笔记】30 | 用好源代码管理工具,让你的协作更高效
随机推荐
【软件工程之美 - 专栏笔记】13 | 白天开会,加班写代码的节奏怎么破?
shell工具finalShell
125KHz唤醒功能2.4GHz单发射芯片-Si24R2H
2022 spring recruit - Shanghai an road FPGA post Manager (and Lexin SOC interview)
ABSA1: Attentional Encoder Network for Targeted Sentiment Classification
Dust and noise monitoring system
进程与进程的概念
Si12T和Si14T低功耗电容触摸芯片
HAL库学习笔记-11 I2C
2022 spring recruit - Hesai technology FPGA technology post (one or two sides, collected from: Digital IC workers and FPGA Explorers)
Power electronics: single inverter design (matlab program +ad schematic diagram)
Ml6 self study notes
STM32FF030 替代国产单片机——DP32G030
HAL库学习笔记-12 SPI
【软件工程之美 - 专栏笔记】20 | 如何应对让人头疼的需求变更问题?
【软件工程之美 - 专栏笔记】21 | 架构设计:普通程序员也能实现复杂系统?
HAL库学习笔记-13 I2C和SPI的应用
LoRa开启物联网新时代-ASR6500S、ASR6501/6502、ASR6505、ASR6601
QT learning notes QtSql
智慧能源管理系统解决方案