图论-链式前向 + SPFA

链式前向星

图的存储一般有两种:邻接矩阵、前向星。

若图是稀疏图,边很少,开二维数组a[][]很浪费;

若点很多(如10000个点)a[10000][10000]又会爆.只能用前向星做.

 

前向星的效率不是很高,优化后为链式前向星,效率有所提升。

 

(一)链式前向星

1. 结构

 

这里用两个东西:

1 结构体数组edge存边,edge[i]表示第i条边,

2 head[i]存以i为起点的第一条边(在edge中的下标)

struct EDGE{
	int next;   //下一条边的存储下标(默认0) 
	int to;     //这条边的终点 
	int w;      //权值 
}; 
EDGE edge[500010];

 

2.增边:若以点i为起点的边新增了一条,在edge中的下标为j.

那么edge[j].next=head[i];然后head[i]=j.

即每次新加的边作为第一条边,最后倒序遍历

void Add(int u, int v, int w) {  //起点u, 终点v, 权值w 
	//cnt为边的计数,从1开始计 
	edge[++cnt].next = head[u];
	edge[cnt].w = w;
	edge[cnt].to = v;
	head[u] = cnt;    //第一条边为当前边 
} 

 

3. 遍历

遍历以st为起点的边

for(int i=head[st]; i!=0; i=edge[i].next)

 

以i开始为第一条边,每次指向下一条(以0为结束标志)  (若下标从0开始,next应初始化-1)

 

 

一个简单的输出有向图熟悉链式前向星:

#include <iostream>
using namespace std;
 
#define MAXM 500010
#define MAXN 10010
 
struct EDGE{
	int next;   //下一条边的存储下标 
	int to;     //这条边的终点 
	int w;      //权值 
}; 
EDGE edge[MAXM];
 
int n, m, cnt;
int head[MAXN];  //head[i]表示以i为起点的第一条边 
 
void Add(int u, int v, int w) {  //起点u, 终点v, 权值w 
	edge[++cnt].next = head[u];
	edge[cnt].w = w;
	edge[cnt].to = v;
	head[u] = cnt;    //第一条边为当前边 
} 
 
void Print() {
	int st;
	cout << "Begin with[Please Input]: \n";
	cin >> st;
	for(int i=head[st]; i!=0; i=edge[i].next) {//i开始为第一条边,每次指向下一条(以0为结束标志)若下标从0开始,next应初始化-1 
		cout << "Start: " << st << endl;
		cout << "End: " << edge[i].to << endl;
		cout << "W: " << edge[i].w << endl << endl; 
	}
}
 
int main() {
	int s, t, w;
	cin >> n >> m;
	for(int i=1; i<=m; i++) {
		cin >> s >> t >> w;
		Add(s, t, w);
	}
	Print(); 
	return 0;
}

 

                                            (二)链式前向星-SPFA

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 
const int MAXN = 200010;
const int MAXM = 500010;
const int ANS_MAX = 2147483647;
 
struct EDGE {
	int next;
	int to;
	ll w;
} edge[MAXM];
 
int n, m, st, ed, cnt, pre[MAXN];
int head[MAXN], num[MAXN];
ll dis[MAXN];
bool vis[MAXN];
queue<int> Q;
 
inline int Read() {  //读入优化 可忽略 
	char c;
	int ans = 0;
	bool Sign = false;
	while(!isdigit(c=getchar()) && c != '-');
	if(c == '-') {Sign = true;c = getchar();}
	do {ans = (ans<<3) + (ans<<1) + (c - '0');} 
	while(isdigit(c=getchar()));
	return Sign ? -ans : ans;
}
 
void Add(int u, int v, ll w) {
	edge[++cnt].next = head[u];
	edge[cnt].to = v;
	edge[cnt].w = w;
	head[u] = cnt;
}
 
void read() {
	int x, y;
	ll w;
	n = Read();
	m = Read();
	st = Read();
	for(int i=1; i<=m; i++) {
		x = Read();
		y = Read();
		w = Read();
		Add(x, y, w);
	}
}
 
bool SPFA(int x) {
	while(!Q.empty()) Q.pop();
	for(int i=1; i<=n; i++) dis[i] = ANS_MAX;
	dis[x] = 0;
	num[x] = 1;
	Q.push(x);
	vis[x] = true;
	while(!Q.empty()) {
		int k = Q.front();
		Q.pop();
		vis[k] = false;
		if(dis[k] == ANS_MAX) continue;
		for(int i=head[k]; i!=0; i=edge[i].next) {
			int j = edge[i].to;
			if(dis[j] > dis[k] + edge[i].w) {
				dis[j] = dis[k] + edge[i].w;
				num[j] = num[k]+1;
				pre[j] = k;
				//if(num[j]>n) return 1;	//判断负环 
				if(!vis[j]) {
					Q.push(j);
					vis[j] = true;
				}
			}
		}
	}
	return 0;
}
 
void print_path(int end) { //打印最短路的路径
	stack <int> path;
	int now = end;
	while(1) { //前驱
		path.push(now);
		if(now == st) break;
		now = pre[now];
	}
	while(!path.empty()) {
		now = path.top();
		path.pop();
		if(path.empty()) printf("%d\n", now);
		else printf("%d-->", now);
	}
}
 
int main() {
	int cas;
	cas = Read();
	while(cas--){
		memset(vis, 0, sizeof(vis));
		memset(num, 0, sizeof(num));
		memset(head, 0, sizeof(head));
		memset(pre, -1, sizeof(pre));
		cnt = 0;
		read();
		SPFA(st);
		//SPFA(1) ? printf("YES\n") : printf("NO\n");
		for(int i=1; i<=n; i++) printf("%lld ", dis[i]);
		printf("\n");
		//for(int i=1; i<=n; i++) print_path(i);
	}
}

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前向星和链式前向星的区别在于实现方式和数据结构的不同。 前向星是一种存储图的边的数据结构,它使用两个数组来存储边的信息。一个数组存储每个顶点的第一条边的索引,另一个数组存储每条边的下一条边的索引。这种方式可以方便地遍历每个顶点的所有边。 链式前向星是一种基于链表的存储方式,它使用链表来存储每个顶点的边。每个顶点都有一个指向第一条边的指针,每条边都有一个指向下一条边的指针。这种方式可以动态地添加和删除边。 总结来说,前向星使用数组存储边的信息,链式前向星使用链表存储边的信息。链式前向星相比前向星更加灵活,可以方便地进行边的插入和删除操作。但是链式前向星的空间复杂度较高,因为需要额外的指针来存储链表的连接关系。 引用\[1\]中提到,链式前向星的整体结构很像邻接表,但是实现方式不同。链式前向星的思想和邻接表一致,只是在实现上有所区别。因此,链式前向星的使用和邻接表相一致,可以用于存储和遍历图的边的信息。 #### 引用[.reference_title] - *1* *2* *3* [链式前向星](https://blog.csdn.net/MuShan_bit/article/details/123882339)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值