当前位置:网站首页>关于【链式前向星】的自学理解
关于【链式前向星】的自学理解
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;
}
边栏推荐
猜你喜欢

【软件工程之美 - 专栏笔记】“一问一答”第3期 | 18个软件开发常见问题解决策略

Hal library learning notes-12 SPI

Ml4 self study notes

ABSA1: Attentional Encoder Network for Targeted Sentiment Classification

HAL库学习笔记-11 I2C

基于DAC0832的直流电机控制系统

Open source based on STM32: MHD Bluetooth speaker (including source code +pcb)

EPS32+Platform+Arduino 跑马灯

基于stm32的四针OLED显示

【软件工程之美 - 专栏笔记】23 | 架构师:不想当架构师的程序员不是好程序员
随机推荐
Ml4 self study notes
HAL库学习笔记-10 HAL库外设驱动框架概述
【软件工程之美 - 专栏笔记】28 | 软件工程师的核心竞争力是什么?(下)
STM32 检测信号频率
markdown与Typora
JUC并发知识点
HAL库学习笔记- 8 串口通信之概念
【软件工程之美 - 专栏笔记】“一问一答”第3期 | 18个软件开发常见问题解决策略
【软件工程之美 - 专栏笔记】23 | 架构师:不想当架构师的程序员不是好程序员
低功耗蓝牙5.0芯片nrf52832-QFAA
八大排序-----------快速排序
唯美girls
噪音监测传感系统
Huawei cloud 14 day Hongmeng device development -day7wifi function development
简洁代码实现pdf转word文档
Fasttext learning - text classification
【软件工程之美 - 专栏笔记】22 | 如何为项目做好技术选型?
【软件工程之美 - 专栏笔记】21 | 架构设计:普通程序员也能实现复杂系统?
Hal library learning notes-11 I2C
倾角传感器精度校准检测